Skip to content

Commit 88cff11

Browse files
authored
Update app.py
0.4 ollama function calling
1 parent 476ba1c commit 88cff11

File tree

1 file changed

+12
-91
lines changed

1 file changed

+12
-91
lines changed

app.py

+12-91
Original file line numberDiff line numberDiff line change
@@ -57,81 +57,9 @@
5757
)
5858
logger.info("Let's review the packages and their versions")
5959

60+
6061
# Define tools schema
61-
tools = [
62-
{
63-
"type": "function",
64-
"function": {
65-
"name": "send_airtime",
66-
"description": "Send airtime to a phone number using the Africa's Talking API",
67-
"parameters": {
68-
"type": "object",
69-
"properties": {
70-
"phone_number": {
71-
"type": "string",
72-
"description": "The phone number in international format",
73-
},
74-
"currency_code": {
75-
"type": "string",
76-
"description": "The 3-letter ISO currency code",
77-
},
78-
"amount": {
79-
"type": "string",
80-
"description": "The amount of airtime to send",
81-
},
82-
},
83-
"required": ["phone_number", "currency_code", "amount"],
84-
},
85-
},
86-
},
87-
{
88-
"type": "function",
89-
"function": {
90-
"name": "send_message",
91-
"description": "Send a message to a phone number using the Africa's Talking API",
92-
"parameters": {
93-
"type": "object",
94-
"properties": {
95-
"phone_number": {
96-
"type": "string",
97-
"description": "The phone number in international format",
98-
},
99-
"message": {
100-
"type": "string",
101-
"description": "The message to send",
102-
},
103-
"username": {
104-
"type": "string",
105-
"description": "The username for the Africa's Talking account",
106-
},
107-
},
108-
"required": ["phone_number", "message", "username"],
109-
},
110-
},
111-
},
112-
{
113-
"type": "function",
114-
"function": {
115-
"name": "search_news",
116-
"description": "Search for news articles using DuckDuckGo News API",
117-
"parameters": {
118-
"type": "object",
119-
"properties": {
120-
"query": {
121-
"type": "string",
122-
"description": "The search query for news articles",
123-
},
124-
"max_results": {
125-
"type": "integer",
126-
"description": "The maximum number of news articles to retrieve",
127-
"default": 5,
128-
},
129-
},
130-
"required": ["query"],
131-
},
132-
},
133-
},
134-
]
62+
tools = [send_airtime, send_message, search_news]
13563

13664

13765
@with_langtrace_root_span()
@@ -180,29 +108,22 @@ async def process_user_message(message: str, history: list) -> str:
180108
)
181109
logger.debug("Model response: %s", response["message"])
182110

111+
available_functions = {
112+
'add_two_numbers': add_two_numbers,
113+
'send_airtime': send_airtime,
114+
'send_message': send_message,
115+
'search_news': search_news,
116+
}
117+
183118
if model_message.get("tool_calls"):
184119
for tool in model_message["tool_calls"]:
185120
tool_name = tool["function"]["name"]
186121
arguments = tool["function"]["arguments"]
187122
logger.info("Tool call detected: %s", tool_name)
188123

189-
if tool_name == "send_airtime":
190-
logger.info("Calling send_airtime with arguments: %s", arguments)
191-
function_response = send_airtime(
192-
arguments["phone_number"],
193-
arguments["currency_code"],
194-
arguments["amount"],
195-
)
196-
elif tool_name == "send_message":
197-
logger.info("Calling send_message with arguments: %s", arguments)
198-
function_response = send_message(
199-
arguments["phone_number"],
200-
arguments["message"],
201-
arguments["username"],
202-
)
203-
elif tool_name == "search_news":
204-
logger.info("Calling search_news with arguments: %s", arguments)
205-
function_response = search_news(arguments["query"])
124+
function_to_call = available_functions.get(tool_name)
125+
if function_to_call:
126+
function_response = function_to_call(**arguments)
206127
else:
207128
function_response = json.dumps({"error": "Unknown function"})
208129

0 commit comments

Comments
 (0)