Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -92,61 +92,49 @@ def search_web(query):
|
|
92 |
except Exception as e:
|
93 |
return f"An error occurred: {e}"
|
94 |
|
95 |
-
|
96 |
-
# Step 1: send the conversation and available functions to the model
|
97 |
-
messages = [
|
98 |
-
{
|
99 |
-
"role": "system",
|
100 |
-
"content": "You are a function calling LLM that uses the data extracted from the get_game_score function to answer questions around NBA game scores. Include the team and their opponent in your response."
|
101 |
-
},
|
102 |
-
{
|
103 |
-
"role": "user",
|
104 |
-
"content": user_prompt,
|
105 |
-
}
|
106 |
-
]
|
107 |
-
tools = [
|
108 |
-
{
|
109 |
-
"type": "internet",
|
110 |
-
"internet": {
|
111 |
-
"allow": ["search_web"],
|
112 |
-
"description": "Search the web for information.",
|
113 |
-
"parameters": {
|
114 |
-
"type": "object",
|
115 |
-
"properties": {
|
116 |
-
"query": {
|
117 |
-
"type": "string",
|
118 |
-
"description": "The search query.",
|
119 |
-
}
|
120 |
-
},
|
121 |
-
"required": ["query"],
|
122 |
-
},
|
123 |
-
},
|
124 |
-
}
|
125 |
-
]
|
126 |
-
response = client.chat.completions.create(
|
127 |
-
model=model_option,
|
128 |
-
messages=messages,
|
129 |
-
tools=tools,
|
130 |
-
tool_choice="auto",
|
131 |
-
max_tokens=max_tokens
|
132 |
-
)
|
133 |
-
|
134 |
-
for chunk in response:
|
135 |
-
if chunk.choices[0].delta.content:
|
136 |
-
yield chunk.choices[0].delta.content
|
137 |
|
138 |
-
if prompt := st.
|
139 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
140 |
|
141 |
with st.chat_message("user", avatar="🕺"):
|
142 |
st.markdown(prompt)
|
143 |
|
144 |
try:
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
except Exception as e:
|
152 |
st.error(e, icon="🚨")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
except Exception as e:
|
93 |
return f"An error occurred: {e}"
|
94 |
|
95 |
+
full_response = None # Initialize full_response to None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
if prompt := st.chat_input("Enter your prompt here..."):
|
98 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
99 |
|
100 |
with st.chat_message("user", avatar="🕺"):
|
101 |
st.markdown(prompt)
|
102 |
|
103 |
try:
|
104 |
+
if "search for" in prompt.lower():
|
105 |
+
query = prompt.lower().replace("search for", "").strip()
|
106 |
+
search_results = search_web(query)
|
107 |
+
formatted_results = "\n\n".join([f"Title: {result['title']}\nURL: {result['url']}\nSnippet: {result['snippet']}" for result in search_results])
|
108 |
+
st.session_state.messages.append({"role": "assistant", "content": formatted_results})
|
109 |
+
else:
|
110 |
+
chat_completion = client.chat.completions.create(
|
111 |
+
model=model_option,
|
112 |
+
messages=[
|
113 |
+
{
|
114 |
+
"role": "system",
|
115 |
+
"content": "You are a helpful assistant that can access the web.",
|
116 |
+
},
|
117 |
+
{"role": m["role"], "content": m["content"]}
|
118 |
+
for m in st.session_state.messages
|
119 |
+
],
|
120 |
+
max_tokens=max_tokens,
|
121 |
+
stream=True,
|
122 |
+
)
|
123 |
+
|
124 |
+
with st.chat_message("assistant", avatar="🤖"):
|
125 |
+
chat_responses_generator = generate_chat_responses(chat_completion)
|
126 |
+
full_response = st.write_stream(chat_responses_generator)
|
127 |
except Exception as e:
|
128 |
st.error(e, icon="🚨")
|
129 |
+
|
130 |
+
# Check if full_response is defined before using it
|
131 |
+
if full_response is not None:
|
132 |
+
if isinstance(full_response, str):
|
133 |
+
st.session_state.messages.append(
|
134 |
+
{"role": "assistant", "content": full_response}
|
135 |
+
)
|
136 |
+
else:
|
137 |
+
combined_response = "\n".join(str(item) for item in full_response)
|
138 |
+
st.session_state.messages.append(
|
139 |
+
{"role": "assistant", "content": combined_response}
|
140 |
+
)
|