Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,10 @@ import gradio as gr
|
|
| 4 |
import chromadb
|
| 5 |
import uuid
|
| 6 |
from pprint import pprint
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
#----------
|
|
@@ -135,6 +139,94 @@ collection.add(
|
|
| 135 |
result = collection.get(include=["documents", "metadatas", "embeddings"])
|
| 136 |
pprint(result)
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
#----------
|
| 140 |
# System Message
|
|
@@ -200,13 +292,30 @@ def respond_ai(message, history):
|
|
| 200 |
|
| 201 |
messages.append({"role": "user", "content": message})
|
| 202 |
|
| 203 |
-
|
| 204 |
llm_response = client.chat.completions.create(
|
| 205 |
model="gpt-4o-mini",
|
| 206 |
messages=messages,
|
|
|
|
| 207 |
)
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
|
| 212 |
#----------
|
|
|
|
| 4 |
import chromadb
|
| 5 |
import uuid
|
| 6 |
from pprint import pprint
|
| 7 |
+
import json
|
| 8 |
+
import requests
|
| 9 |
+
import random
|
| 10 |
+
|
| 11 |
|
| 12 |
|
| 13 |
#----------
|
|
|
|
| 139 |
result = collection.get(include=["documents", "metadatas", "embeddings"])
|
| 140 |
pprint(result)
|
| 141 |
|
| 142 |
+
#----------
|
| 143 |
+
# Tools
|
| 144 |
+
#----------
|
| 145 |
+
tools = []
|
| 146 |
+
|
| 147 |
+
pushover_user = os.getenv("PUSHOVER_USER")
|
| 148 |
+
pushover_token = os.getenv("PUSHOVER_TOKEN")
|
| 149 |
+
pushover_url = "https://api.pushover.net/1/messages.json"
|
| 150 |
+
|
| 151 |
+
#Create send_notification function
|
| 152 |
+
def send_notification(message: str):
|
| 153 |
+
payload = {"user": pushover_user, "token": pushover_token, "message": message}
|
| 154 |
+
requests.post(pushover_url, data=payload)
|
| 155 |
+
|
| 156 |
+
#Describe Pushover as an LLM tool
|
| 157 |
+
send_notification_function = {
|
| 158 |
+
"name": "send_notification",
|
| 159 |
+
"description": "Sends a push notification to the real world verion of you via Pushover. Use this if the user needs to alert the real world version of you about important events.",
|
| 160 |
+
"parameters": {
|
| 161 |
+
"type": "object",
|
| 162 |
+
"properties": {
|
| 163 |
+
"message": {
|
| 164 |
+
"type": "string",
|
| 165 |
+
"description": "The notification message to send to the user's device"
|
| 166 |
+
}
|
| 167 |
+
},
|
| 168 |
+
"required": ["message"]
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
#Add Pushover to tools
|
| 173 |
+
tools.append({"type":"function", "function":send_notification_function})
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
#Simulates DIce rolling tool
|
| 177 |
+
def dice_roll():
|
| 178 |
+
result = random.randint(1,6)
|
| 179 |
+
return result
|
| 180 |
+
|
| 181 |
+
#Describe function for the LLM
|
| 182 |
+
roll_dice_function = {
|
| 183 |
+
"name": "dice_roll",
|
| 184 |
+
"description": "Simulates rolling a 6-sided die and returns the result. Use this when the user wants to roll a die or to give the user a random number",
|
| 185 |
+
"parameters": {
|
| 186 |
+
"type": "object",
|
| 187 |
+
"properties": {},
|
| 188 |
+
"required": []
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
#Add function to list of tools of LLM
|
| 193 |
+
tools.append({"type":"function", "function":roll_dice_function})
|
| 194 |
+
|
| 195 |
+
#----------
|
| 196 |
+
# Tool Handler
|
| 197 |
+
#----------
|
| 198 |
+
def handle_tool_call(tool_calls):
|
| 199 |
+
tool_results = []
|
| 200 |
+
|
| 201 |
+
for tool_call in tool_calls:
|
| 202 |
+
function_name = tool_call.function.name
|
| 203 |
+
args = json.loads(tool_call.function.arguments)
|
| 204 |
+
|
| 205 |
+
#Route to the approperiate function based on function_name
|
| 206 |
+
if function_name == "send_notification":
|
| 207 |
+
send_notification(args["message"])
|
| 208 |
+
content = f"Notification sent: {args['message']}"
|
| 209 |
+
elif function_name == "dice_roll":
|
| 210 |
+
content = f"Rolled: {dice_roll()}"
|
| 211 |
+
#elif function_name == "insert_function_name_3":
|
| 212 |
+
# content = insert_function_name_3(args["message"])
|
| 213 |
+
#...
|
| 214 |
+
else:
|
| 215 |
+
content = f"Unknown function: {function_name}"
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
tool_call_result = {
|
| 219 |
+
"role": "tool",
|
| 220 |
+
"tool_call_id": tool_call.id,
|
| 221 |
+
"content": content
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
tool_results.append(tool_call_result)
|
| 225 |
+
|
| 226 |
+
return tool_results
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
|
| 230 |
|
| 231 |
#----------
|
| 232 |
# System Message
|
|
|
|
| 292 |
|
| 293 |
messages.append({"role": "user", "content": message})
|
| 294 |
|
| 295 |
+
# Call LLM
|
| 296 |
llm_response = client.chat.completions.create(
|
| 297 |
model="gpt-4o-mini",
|
| 298 |
messages=messages,
|
| 299 |
+
tools=tools
|
| 300 |
)
|
| 301 |
|
| 302 |
+
assistant_message = llm_response.choices[0].message
|
| 303 |
+
|
| 304 |
+
# Check if model wants to call a tool
|
| 305 |
+
while assistant_message.tool_calls:
|
| 306 |
+
pprint(assistant_message.tool_calls)
|
| 307 |
+
tool_result = handle_tool_call(assistant_message.tool_calls)
|
| 308 |
+
messages.append(assistant_message)
|
| 309 |
+
messages.extend(tool_result)
|
| 310 |
+
|
| 311 |
+
llm_response = client.chat.completions.create(
|
| 312 |
+
model="gpt-4o-mini",
|
| 313 |
+
messages=messages,
|
| 314 |
+
tools=tools
|
| 315 |
+
)
|
| 316 |
+
assistant_message = llm_response.choices[0].message
|
| 317 |
+
|
| 318 |
+
return assistant_message.content
|
| 319 |
|
| 320 |
|
| 321 |
#----------
|