Spaces:
Sleeping
Sleeping
Create function_orchestrator.py
Browse files- function_orchestrator.py +107 -0
function_orchestrator.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# function_orchestrator.py
|
2 |
+
|
3 |
+
import anthropic
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
|
7 |
+
client = anthropic.Client()
|
8 |
+
MODEL_NAME = "claude-3-sonnet-20240229"
|
9 |
+
|
10 |
+
# Define the base URL for the FastAPI service
|
11 |
+
BASE_URL = "https://dwb2023-blackbird-svc.hf.space"
|
12 |
+
|
13 |
+
# Define tools
|
14 |
+
tools = [
|
15 |
+
{
|
16 |
+
"name": "get_user",
|
17 |
+
"description": "Looks up a user by email, phone, or username.",
|
18 |
+
"input_schema": {
|
19 |
+
"type": "object",
|
20 |
+
"properties": {
|
21 |
+
"key": {
|
22 |
+
"type": "string",
|
23 |
+
"enum": ["email", "phone", "username"],
|
24 |
+
"description": "The attribute to search for a user by (email, phone, or username)."
|
25 |
+
},
|
26 |
+
"value": {
|
27 |
+
"type": "string",
|
28 |
+
"description": "The value to match for the specified attribute."
|
29 |
+
}
|
30 |
+
},
|
31 |
+
"required": ["key", "value"]
|
32 |
+
}
|
33 |
+
},
|
34 |
+
# other tools definitions...
|
35 |
+
]
|
36 |
+
|
37 |
+
# Function to process tool calls
|
38 |
+
def process_tool_call(tool_name, tool_input):
|
39 |
+
if tool_name == "get_user":
|
40 |
+
response = requests.post(f"{BASE_URL}/get_user", json=tool_input)
|
41 |
+
elif tool_name == "get_order_by_id":
|
42 |
+
response = requests.post(f"{BASE_URL}/get_order_by_id", json=tool_input)
|
43 |
+
elif tool_name == "get_customer_orders":
|
44 |
+
response = requests.post(f"{BASE_URL}/get_customer_orders", json=tool_input)
|
45 |
+
elif tool_name == "cancel_order":
|
46 |
+
response = requests.post(f"{BASE_URL}/cancel_order", json=tool_input)
|
47 |
+
elif tool_name == "update_user_contact":
|
48 |
+
response = requests.post(f"{BASE_URL}/update_user", json=tool_input)
|
49 |
+
elif tool_name == "get_user_info":
|
50 |
+
response = requests.post(f"{BASE_URL}/get_user_info", json=tool_input)
|
51 |
+
else:
|
52 |
+
return {"error": "Invalid tool name"}
|
53 |
+
|
54 |
+
if response.status_code == 200:
|
55 |
+
return response.json()
|
56 |
+
else:
|
57 |
+
return {"error": response.text}
|
58 |
+
|
59 |
+
# Function to handle interactive chat session
|
60 |
+
def simple_chat():
|
61 |
+
system_prompt = """
|
62 |
+
You are a customer support chat bot for an online retailer called TechNova.
|
63 |
+
Your job is to help users look up their account, orders, and cancel orders.
|
64 |
+
Be helpful and brief in your responses.
|
65 |
+
You have access to a set of tools, but only use them when needed.
|
66 |
+
If you do not have enough information to use a tool correctly, ask a user follow up questions to get the required inputs.
|
67 |
+
Do not call any of the tools unless you have the required data from a user.
|
68 |
+
|
69 |
+
In each conversational turn, you will begin by thinking about your response.
|
70 |
+
Once you're done, you will write a user-facing response.
|
71 |
+
"""
|
72 |
+
user_message = input("\nUser: ")
|
73 |
+
messages = [{"role": "user", "content": user_message}]
|
74 |
+
while True:
|
75 |
+
if messages[-1].get("role") == "assistant":
|
76 |
+
user_message = input("\nUser: ")
|
77 |
+
messages.append({"role": "user", "content": user_message})
|
78 |
+
|
79 |
+
response = client.messages.create(
|
80 |
+
model=MODEL_NAME,
|
81 |
+
max_tokens=4096,
|
82 |
+
tools=tools,
|
83 |
+
messages=messages
|
84 |
+
)
|
85 |
+
messages.append({"role": "assistant", "content": response.content})
|
86 |
+
|
87 |
+
if response.stop_reason == "tool_use":
|
88 |
+
tool_use = response.content[-1]
|
89 |
+
tool_name = tool_use.name
|
90 |
+
tool_input = tool_use.input
|
91 |
+
print(f"======Claude wants to use the {tool_name} tool======")
|
92 |
+
|
93 |
+
tool_result = process_tool_call(tool_name, tool_input)
|
94 |
+
|
95 |
+
messages.append({
|
96 |
+
"role": "user",
|
97 |
+
"content": [{
|
98 |
+
"type": "tool_result",
|
99 |
+
"tool_use_id": tool_use.id,
|
100 |
+
"content": str(tool_result),
|
101 |
+
}],
|
102 |
+
})
|
103 |
+
else:
|
104 |
+
print("\nTechNova Support: " + f"{response.content[0].text}")
|
105 |
+
|
106 |
+
# Start the chat!!
|
107 |
+
simple_chat()
|