Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +110 -0
- requirements.txt +3 -0
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 |
+
print("Initializing session state...")
|
| 51 |
+
if "messages" not in st.session_state:
|
| 52 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "Hey, how can I help you?"}]
|
| 53 |
+
print("Session state initialized.")
|
| 54 |
+
st.title("💬 Weather Chatbot")
|
| 55 |
+
st.caption("🚀 A streamlit chatbot powered by Llama LLM")
|
| 56 |
+
for msg in st.session_state.messages:
|
| 57 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 58 |
+
|
| 59 |
+
if prompt := st.chat_input("Write Something"):
|
| 60 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 61 |
+
st.chat_message("user").write(prompt)
|
| 62 |
+
time.sleep(2)
|
| 63 |
+
response = client.chat.completions.create(
|
| 64 |
+
model="mixtral-8x7b-32768",
|
| 65 |
+
messages=[
|
| 66 |
+
{"role": "system",
|
| 67 |
+
"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"
|
| 68 |
+
}, *st.session_state.messages
|
| 69 |
+
],
|
| 70 |
+
tool_choice = "auto",
|
| 71 |
+
tools = tools,
|
| 72 |
+
max_tokens = 2048,
|
| 73 |
+
)
|
| 74 |
+
reply = response.choices[0].message.content
|
| 75 |
+
tool_calls = response.choices[0].message.tool_calls
|
| 76 |
+
|
| 77 |
+
if tool_calls :
|
| 78 |
+
available_functions = {
|
| 79 |
+
"get_current_weather": get_current_weather
|
| 80 |
+
}
|
| 81 |
+
for tool_call in tool_calls:
|
| 82 |
+
function_name = tool_call.function.name
|
| 83 |
+
function_to_call = available_functions[function_name]
|
| 84 |
+
function_params = json.loads(tool_call.function.arguments)
|
| 85 |
+
# try:
|
| 86 |
+
response = get_current_weather(
|
| 87 |
+
location = function_params.get("location"),
|
| 88 |
+
unit = "metric",
|
| 89 |
+
)
|
| 90 |
+
st.session_state.messages.append(
|
| 91 |
+
{
|
| 92 |
+
"role": "assistant",
|
| 93 |
+
"content": response,
|
| 94 |
+
}
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# except Exception as e:
|
| 98 |
+
# print(f"An error occurred while calling function {function_name}: {e}")
|
| 99 |
+
# response = f"An error occurred while calling function {function_name}: {e}"
|
| 100 |
+
# # st.session_state.messages.append(
|
| 101 |
+
# {
|
| 102 |
+
# "role": "assistant",
|
| 103 |
+
# "content": response,
|
| 104 |
+
# }
|
| 105 |
+
# )
|
| 106 |
+
st.chat_message("assistant").write(response)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 110 |
+
st.chat_message("assistant").write(reply)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
groq
|
| 2 |
+
streamlit
|
| 3 |
+
requests
|