dwb2023 commited on
Commit
48e7fd1
·
verified ·
1 Parent(s): 81936bb

Update function_orchestrator.py

Browse files
Files changed (1) hide show
  1. function_orchestrator.py +110 -16
function_orchestrator.py CHANGED
@@ -1,14 +1,13 @@
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 = [
@@ -31,7 +30,89 @@ tools = [
31
  "required": ["key", "value"]
32
  }
33
  },
34
- # other tools definitions...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  ]
36
 
37
  # Function to process tool calls
@@ -72,36 +153,49 @@ def simple_chat():
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()
 
 
 
1
  import anthropic
2
  import json
3
+ import re
4
  import requests
5
 
6
  client = anthropic.Client()
7
  MODEL_NAME = "claude-3-sonnet-20240229"
8
 
9
  # Define the base URL for the FastAPI service
10
+ BASE_URL = "http://127.0.0.1:8000"
11
 
12
  # Define tools
13
  tools = [
 
30
  "required": ["key", "value"]
31
  }
32
  },
33
+ {
34
+ "name": "get_order_by_id",
35
+ "description": "Retrieves the details of a specific order based on the order ID.",
36
+ "input_schema": {
37
+ "type": "object",
38
+ "properties": {
39
+ "order_id": {
40
+ "type": "string",
41
+ "description": "The unique identifier for the order."
42
+ }
43
+ },
44
+ "required": ["order_id"]
45
+ }
46
+ },
47
+ {
48
+ "name": "get_customer_orders",
49
+ "description": "Retrieves the list of orders belonging to a user based on a user's customer id.",
50
+ "input_schema": {
51
+ "type": "object",
52
+ "properties": {
53
+ "customer_id": {
54
+ "type": "string",
55
+ "description": "The customer_id belonging to the user"
56
+ }
57
+ },
58
+ "required": ["customer_id"]
59
+ }
60
+ },
61
+ {
62
+ "name": "cancel_order",
63
+ "description": "Cancels an order based on a provided order_id. Only orders that are 'processing' can be cancelled.",
64
+ "input_schema": {
65
+ "type": "object",
66
+ "properties": {
67
+ "order_id": {
68
+ "type": "string",
69
+ "description": "The order_id pertaining to a particular order"
70
+ }
71
+ },
72
+ "required": ["order_id"]
73
+ }
74
+ },
75
+ {
76
+ "name": "update_user_contact",
77
+ "description": "Updates a user's email and/or phone number.",
78
+ "input_schema": {
79
+ "type": "object",
80
+ "properties": {
81
+ "user_id": {
82
+ "type": "string",
83
+ "description": "The ID of the user"
84
+ },
85
+ "email": {
86
+ "type": "string",
87
+ "description": "The new email address of the user"
88
+ },
89
+ "phone": {
90
+ "type": "string",
91
+ "description": "The new phone number of the user"
92
+ }
93
+ },
94
+ "required": ["user_id"]
95
+ }
96
+ },
97
+ {
98
+ "name": "get_user_info",
99
+ "description": "Retrieves a user's information along with their order history based on email, phone, or username.",
100
+ "input_schema": {
101
+ "type": "object",
102
+ "properties": {
103
+ "key": {
104
+ "type": "string",
105
+ "enum": ["email", "phone", "username"],
106
+ "description": "The attribute to search for a user by (email, phone, or username)."
107
+ },
108
+ "value": {
109
+ "type": "string",
110
+ "description": "The value to match for the specified attribute."
111
+ }
112
+ },
113
+ "required": ["key", "value"]
114
+ }
115
+ }
116
  ]
117
 
118
  # Function to process tool calls
 
153
  user_message = input("\nUser: ")
154
  messages = [{"role": "user", "content": user_message}]
155
  while True:
156
+ #If the last message is from the assistant, get another input from the user
157
  if messages[-1].get("role") == "assistant":
158
  user_message = input("\nUser: ")
159
  messages.append({"role": "user", "content": user_message})
160
 
161
+ #Send a request to Claude
162
  response = client.messages.create(
163
  model=MODEL_NAME,
164
  max_tokens=4096,
165
  tools=tools,
166
  messages=messages
167
  )
168
+ # Update messages to include Claude's response
169
+ messages.append(
170
+ {"role": "assistant", "content": response.content}
171
+ )
172
 
173
+ #If Claude stops because it wants to use a tool:
174
  if response.stop_reason == "tool_use":
175
+ tool_use = response.content[-1] #Naive approach assumes only 1 tool is called at a time
176
  tool_name = tool_use.name
177
  tool_input = tool_use.input
178
  print(f"======Claude wants to use the {tool_name} tool======")
179
 
180
+ #Actually run the underlying tool functionality on our db
181
  tool_result = process_tool_call(tool_name, tool_input)
182
 
183
+ #Add our tool_result message:
184
+ messages.append(
185
+ {
186
+ "role": "user",
187
+ "content": [
188
+ {
189
+ "type": "tool_result",
190
+ "tool_use_id": tool_use.id,
191
+ "content": str(tool_result),
192
+ }
193
+ ],
194
+ },
195
+ )
196
+ else:
197
+ #If Claude does NOT want to use a tool, just print out the text reponse
198
+ print("\nTechNova Support: " + f"{response.content[0].text}" )
199
 
200
  # Start the chat!!
201
  simple_chat()