Spaces:
Sleeping
Sleeping
budhadityac24
commited on
Commit
•
0f420a2
1
Parent(s):
45d1744
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
# # Function Calling with OpenAI APIs
|
3 |
import requests
|
4 |
import os
|
@@ -48,13 +47,13 @@ def get_current_weather(location):
|
|
48 |
# "type": "function",
|
49 |
# "function": {
|
50 |
# "name": "get_current_weather",
|
51 |
-
# "description": "Get the current weather
|
52 |
# "parameters": {
|
53 |
# "type": "object",
|
54 |
# "properties": {
|
55 |
# "location": {
|
56 |
# "type": "string",
|
57 |
-
# "description": "The city and
|
58 |
# }
|
59 |
# },
|
60 |
# "required": ["location"]
|
@@ -71,13 +70,13 @@ def get_response(input_text,client):
|
|
71 |
"type": "function",
|
72 |
"function": {
|
73 |
"name": "get_current_weather",
|
74 |
-
"description": "Get the
|
75 |
"parameters": {
|
76 |
"type": "object",
|
77 |
"properties": {
|
78 |
"location": {
|
79 |
"type": "string",
|
80 |
-
"description": "The city and
|
81 |
}
|
82 |
},
|
83 |
"required": ["location"]
|
@@ -89,8 +88,12 @@ def get_response(input_text,client):
|
|
89 |
model="mixtral-8x7b-32768",
|
90 |
messages=[
|
91 |
{
|
|
|
|
|
|
|
|
|
92 |
"role": "user",
|
93 |
-
"content": input_text,
|
94 |
}
|
95 |
],
|
96 |
temperature=0,
|
@@ -108,6 +111,10 @@ def get_response(input_text,client):
|
|
108 |
return "No response from Groq"
|
109 |
else:
|
110 |
groq_response = response.choices[0].message
|
|
|
|
|
|
|
|
|
111 |
# print(groq_response)
|
112 |
|
113 |
|
@@ -116,10 +123,7 @@ def get_response(input_text,client):
|
|
116 |
# We can now capture the arguments:
|
117 |
|
118 |
|
119 |
-
args = json.loads(groq_response.tool_calls[0].function.arguments)
|
120 |
-
# print(args)
|
121 |
|
122 |
-
output=get_current_weather(**args)
|
123 |
# print(output)
|
124 |
from groq import Groq
|
125 |
|
@@ -129,7 +133,7 @@ def get_response(input_text,client):
|
|
129 |
messages=[
|
130 |
{
|
131 |
"role": "system",
|
132 |
-
"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. "
|
133 |
},
|
134 |
{
|
135 |
"role": "user",
|
@@ -150,26 +154,29 @@ def get_response(input_text,client):
|
|
150 |
return output
|
151 |
|
152 |
def main():
|
153 |
-
st.title("
|
|
|
154 |
client = Groq(
|
155 |
api_key=os.getenv("GROQ_API_KEY"),
|
156 |
)
|
157 |
# User input
|
158 |
-
st.write("Hi, I am a weather chatbot. Ask me anything!")
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
# Ask me button
|
162 |
-
if st.
|
163 |
-
|
164 |
-
if location:
|
165 |
-
# Get current weather
|
166 |
-
response = get_response(location,client)
|
167 |
-
# Display weather details
|
168 |
-
# st.json(response)
|
169 |
-
st.write(response)
|
170 |
-
else:
|
171 |
-
st.warning("Please enter a city name.")
|
172 |
|
|
|
|
|
|
|
|
|
|
|
173 |
if __name__ == "__main__":
|
174 |
-
main()
|
175 |
-
|
|
|
|
|
1 |
# # Function Calling with OpenAI APIs
|
2 |
import requests
|
3 |
import os
|
|
|
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"]
|
|
|
70 |
"type": "function",
|
71 |
"function": {
|
72 |
"name": "get_current_weather",
|
73 |
+
"description": "Get the real time weather of a given location",
|
74 |
"parameters": {
|
75 |
"type": "object",
|
76 |
"properties": {
|
77 |
"location": {
|
78 |
"type": "string",
|
79 |
+
"description": "The city, state and country, e.g. San Francisco, CA, US"
|
80 |
}
|
81 |
},
|
82 |
"required": ["location"]
|
|
|
88 |
model="mixtral-8x7b-32768",
|
89 |
messages=[
|
90 |
{
|
91 |
+
"role": "system",
|
92 |
+
"content": "You are a helpful assistant. You will asked details regarding the weather. If there is no question with respect to a weather, show a polite error message."
|
93 |
+
},
|
94 |
+
{
|
95 |
"role": "user",
|
96 |
+
"content": input_text+" Use a tool given to you",
|
97 |
}
|
98 |
],
|
99 |
temperature=0,
|
|
|
111 |
return "No response from Groq"
|
112 |
else:
|
113 |
groq_response = response.choices[0].message
|
114 |
+
args = json.loads(groq_response.tool_calls[0].function.arguments)
|
115 |
+
# print(args)
|
116 |
+
|
117 |
+
output=get_current_weather(**args)
|
118 |
# print(groq_response)
|
119 |
|
120 |
|
|
|
123 |
# We can now capture the arguments:
|
124 |
|
125 |
|
|
|
|
|
126 |
|
|
|
127 |
# print(output)
|
128 |
from groq import Groq
|
129 |
|
|
|
133 |
messages=[
|
134 |
{
|
135 |
"role": "system",
|
136 |
+
"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. If the JSON mentions data fetch error, only give a polite error message and tell the user to ask questions related to the weather of a place. "
|
137 |
},
|
138 |
{
|
139 |
"role": "user",
|
|
|
154 |
return output
|
155 |
|
156 |
def main():
|
157 |
+
st.title("WeatherWizard")
|
158 |
+
st.caption("Ask about any location's weather in plain English, get answers faster than you can say umbrella")
|
159 |
client = Groq(
|
160 |
api_key=os.getenv("GROQ_API_KEY"),
|
161 |
)
|
162 |
# User input
|
163 |
+
# st.write("Hi, I am a weather chatbot. Ask me anything!")
|
164 |
+
if "messages" not in st.session_state:
|
165 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
166 |
+
|
167 |
+
for msg in st.session_state.messages:
|
168 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
169 |
+
|
170 |
+
# input_text = st.text_input("Type in your question")
|
171 |
|
172 |
# Ask me button
|
173 |
+
if prompt := st.chat_input():
|
174 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
177 |
+
st.chat_message("user").write(prompt)
|
178 |
+
response = get_response(prompt,client)
|
179 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
180 |
+
st.chat_message("assistant").write(response)
|
181 |
if __name__ == "__main__":
|
182 |
+
main()
|
|