Spaces:
Sleeping
Sleeping
budhadityac24
commited on
Commit
•
ca6ee31
1
Parent(s):
51433bf
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# # Function Calling with OpenAI APIs
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import streamlit as st
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
from groq import Groq
|
10 |
+
|
11 |
+
client = Groq(
|
12 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
13 |
+
)
|
14 |
+
|
15 |
+
st.title("Weather App with Chat Interface")
|
16 |
+
|
17 |
+
input_text = st.text_input("Hi, I am a weather chatbot. Ask me anything!")
|
18 |
+
|
19 |
+
if st.button("Ask me"):
|
20 |
+
if not input_text:
|
21 |
+
st.error("Please enter a location!")
|
22 |
+
|
23 |
+
# ### Define Dummy Function
|
24 |
+
|
25 |
+
# Defines a dummy function to get the current weather
|
26 |
+
def get_current_weather(location):
|
27 |
+
url = f'https://api.openweathermap.org/data/2.5/weather?q={location}&appid={os.getenv("OPENWEATHER_API_KEY")}'
|
28 |
+
response = requests.get(url)
|
29 |
+
data=response.json()
|
30 |
+
if data['cod'] == 200:
|
31 |
+
return data
|
32 |
+
else:
|
33 |
+
return json.dumps({"city": location, "weather": "Data Fetch Error", "temperature": "N/A"})
|
34 |
+
|
35 |
+
# print(get_current_weather("London"))
|
36 |
+
# ### Define Functions
|
37 |
+
#
|
38 |
+
# As demonstrated in the OpenAI documentation, here is a simple example of how to define the functions that are going to be part of the request.
|
39 |
+
#
|
40 |
+
# The descriptions are important because these are passed directly to the LLM and the LLM will use the description to determine whether to use the functions or how to use/call.
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
# # define a function as tools
|
45 |
+
tools = [
|
46 |
+
{
|
47 |
+
"type": "function",
|
48 |
+
"function": {
|
49 |
+
"name": "get_current_weather",
|
50 |
+
"description": "Get the current weather in a given location",
|
51 |
+
"parameters": {
|
52 |
+
"type": "object",
|
53 |
+
"properties": {
|
54 |
+
"location": {
|
55 |
+
"type": "string",
|
56 |
+
"description": "The city and state, e.g. San Francisco, CA"
|
57 |
+
}
|
58 |
+
},
|
59 |
+
"required": ["location"]
|
60 |
+
}
|
61 |
+
}
|
62 |
+
},
|
63 |
+
]
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
response = client.chat.completions.create(
|
68 |
+
model="mixtral-8x7b-32768",
|
69 |
+
messages=[
|
70 |
+
{
|
71 |
+
"role": "user",
|
72 |
+
"content": input_text,
|
73 |
+
}
|
74 |
+
],
|
75 |
+
temperature=0,
|
76 |
+
max_tokens=300,
|
77 |
+
tools=tools,
|
78 |
+
tool_choice="auto"
|
79 |
+
)
|
80 |
+
# print(response)
|
81 |
+
# print(response.choices[0].message.content)
|
82 |
+
|
83 |
+
# print(response['choices'][0]['message']['tool_calls'][0]['function']['arguments'])
|
84 |
+
|
85 |
+
groq_response = response.choices[0].message
|
86 |
+
# print(groq_response)
|
87 |
+
|
88 |
+
|
89 |
+
# response.tool_calls[0].function.arguments
|
90 |
+
|
91 |
+
# We can now capture the arguments:
|
92 |
+
|
93 |
+
|
94 |
+
args = json.loads(groq_response.tool_calls[0].function.arguments)
|
95 |
+
# print(args)
|
96 |
+
|
97 |
+
output=get_current_weather(**args)
|
98 |
+
# print(output)
|
99 |
+
from groq import Groq
|
100 |
+
|
101 |
+
client = Groq()
|
102 |
+
completion = client.chat.completions.create(
|
103 |
+
model="mixtral-8x7b-32768",
|
104 |
+
messages=[
|
105 |
+
{
|
106 |
+
"role": "system",
|
107 |
+
"content": "You are a helpful assistant. You are given the weather details in json format. Read the data and give a brief description of the weather and then answer the question. All temperatures are in kelvin. Only mention details about the weather. "
|
108 |
+
},
|
109 |
+
{
|
110 |
+
"role": "user",
|
111 |
+
"content": json.dumps(output)
|
112 |
+
}
|
113 |
+
],
|
114 |
+
temperature=0.25,
|
115 |
+
max_tokens=200,
|
116 |
+
top_p=1,
|
117 |
+
stream=True,
|
118 |
+
stop=None,
|
119 |
+
)
|
120 |
+
|
121 |
+
st.write("Response:")
|
122 |
+
for chunk in completion:
|
123 |
+
st.write(chunk.choices[0].delta.content or "", end="")
|