Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from groq import Groq
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import json
|
7 |
+
import time
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
client = Groq(
|
12 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
13 |
+
)
|
14 |
+
api_key = os.getenv("OPENWEATHERMAP_API_KEY")
|
15 |
+
def get_current_weather(location, unit):
|
16 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units={unit}"
|
17 |
+
response = requests.get(url)
|
18 |
+
if response.status_code == 200:
|
19 |
+
data = response.json()
|
20 |
+
return (f"The current weather in {json.dumps(data['name'])} is {json.dumps(data['weather'][0]['description'])}. The temperature is {json.dumps(data['main']['temp'])} degrees.")
|
21 |
+
else:
|
22 |
+
return (f"Error: {response.status_code} - {response.reason}")
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
tools = [
|
27 |
+
{
|
28 |
+
"type": "function",
|
29 |
+
"function": {
|
30 |
+
"name": "get_current_weather",
|
31 |
+
"description": "Get the current weather or temperature in a given location",
|
32 |
+
"parameters": {
|
33 |
+
"type": "object",
|
34 |
+
"properties": {
|
35 |
+
"location": {
|
36 |
+
"type": "string",
|
37 |
+
"description": "The city and state, e.g. San Francisco, CA",
|
38 |
+
},
|
39 |
+
"unit": {
|
40 |
+
"type": "string",
|
41 |
+
"enum": ["metric", "imperial"],
|
42 |
+
},
|
43 |
+
},
|
44 |
+
|
45 |
+
"required": ["location"],
|
46 |
+
},
|
47 |
+
},
|
48 |
+
}
|
49 |
+
]
|
50 |
+
st.title("💬 Weather Chatbot")
|
51 |
+
st.caption("🚀 A streamlit chatbot powered by Llama LLM")
|
52 |
+
if "messages" not in st.session_state:
|
53 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
54 |
+
|
55 |
+
for msg in st.session_state.messages:
|
56 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
57 |
+
|
58 |
+
if prompt := st.chat_input("Write Something"):
|
59 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
60 |
+
st.chat_message("user").write(prompt)
|
61 |
+
time.sleep(2)
|
62 |
+
response = client.chat.completions.create(
|
63 |
+
model="mixtral-8x7b-32768",
|
64 |
+
messages=[
|
65 |
+
{"role": "system",
|
66 |
+
"content": "You are a helpful assistant? Incase the user asks for weather update you trigger the function get_current_weather() to get the current weather. Do not use any historical data"
|
67 |
+
}, *st.session_state.messages
|
68 |
+
],
|
69 |
+
tool_choice = "auto",
|
70 |
+
tools = tools,
|
71 |
+
max_tokens = 2048,
|
72 |
+
)
|
73 |
+
reply = response.choices[0].message.content
|
74 |
+
tool_calls = response.choices[0].message.tool_calls
|
75 |
+
|
76 |
+
if tool_calls :
|
77 |
+
available_functions = {
|
78 |
+
"get_current_weather": get_current_weather
|
79 |
+
}
|
80 |
+
for tool_call in tool_calls:
|
81 |
+
function_name = tool_call.function.name
|
82 |
+
function_to_call = available_functions[function_name]
|
83 |
+
function_params = json.loads(tool_call.function.arguments)
|
84 |
+
# try:
|
85 |
+
response = get_current_weather(
|
86 |
+
location = function_params.get("location"),
|
87 |
+
unit = "metric",
|
88 |
+
)
|
89 |
+
st.session_state.messages.append(
|
90 |
+
{
|
91 |
+
"role": "assistant",
|
92 |
+
"content": response,
|
93 |
+
}
|
94 |
+
)
|
95 |
+
|
96 |
+
# except Exception as e:
|
97 |
+
# print(f"An error occurred while calling function {function_name}: {e}")
|
98 |
+
# response = f"An error occurred while calling function {function_name}: {e}"
|
99 |
+
# # st.session_state.messages.append(
|
100 |
+
# {
|
101 |
+
# "role": "assistant",
|
102 |
+
# "content": response,
|
103 |
+
# }
|
104 |
+
# )
|
105 |
+
st.chat_message("assistant").write(response)
|
106 |
+
|
107 |
+
|
108 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
109 |
+
st.chat_message("assistant").write(reply)
|
110 |
+
|