-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_call.py
450 lines (373 loc) · 14.5 KB
/
function_call.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""
Function calling example using ollama to send airtime to a phone number
using the Africa's Talking API.
The user provides a query like
"Send airtime to +254712345678 with an amount of 10 in currency KES",
and the model decides to use the `send_airtime` function to send
airtime to the provided phone number.
The user can also provide a query like
"Send a message to +254712345678 with the message
'Hello there', using the username 'username'",
and the model decides to use the `send_message`
function to send a message to the provided phone number.
Credentials for the Africa's Talking API are loaded from
environment variables `AT_USERNAME` and `AT_API_KEY`.
Credit: https://www.youtube.com/watch?v=i0tsVzRbsNU
You'll be prompted your computer password for codecarbon to track emissions.
"""
import os
import json
import logging
from importlib.metadata import version
import asyncio
import africastalking
import ollama
# from codecarbon import EmissionsTracker # Import the EmissionsTracker
from duckduckgo_search import DDGS
# Set up the logger
logger = logging.getLogger(__name__)
# Set up logging
logging.basicConfig(level=logging.DEBUG)
# Logging format
formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
# Set up the file handler & stream handler
file_handler = logging.FileHandler("func_calling.log")
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
# Add the file handler to the logger
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
# Flush logs
for handler in logger.handlers:
handler.flush()
# Log the start of the script
logger.info(
"Starting the function calling script to send airtime and messages using the "
"Africa's Talking API"
)
logger.info("Let's review the packages and their versions")
# log versions of the libraries
pkgs = ["africastalking", "ollama"]
for pkg in pkgs:
try:
logger.debug("%s version: %s", pkg, version(pkg))
except Exception as e:
logger.error("Failed to import %s: %s", pkg, str(e))
# Flush logs again
# this is to ensure that the logs are written to the file
for handler in logger.handlers:
handler.flush()
# Set the region explicitly to East Africa
os.environ["CODECARBON_REGION"] = "africa_east"
# Mask phone number and API key for the logs
def mask_phone_number(phone_number):
"""Hide the first digits of a phone number.
Only the last 4 digits will be visible.
Why do we need to mask the phone number?
- This is information that can be used to
identify a person. PIIs (Personally Identifiable Information)
should be protected.
Parameters
----------
phone_number : str : The phone number to mask.
Returns
-------
str : The masked phone number.
Examples
--------
mask_phone_number("+254712345678")
"""
return "x" * (len(phone_number) - 4) + phone_number[-4:]
def mask_api_key(api_key):
"""Hide the first digits of an API key. Only the last 4 digits will be visible.
Why do we need to mask the API key?
- To prevent unauthorized access to your account.
Parameters
----------
api_key : str : The API key to mask.
Returns
-------
str : The masked API key.
Examples
--------
mask_api_key("123456")
"""
return "x" * (len(api_key) - 4) + api_key[-4:]
# Function to send airtime using Africa's Talking API
def send_airtime(phone_number: str, currency_code: str, amount: str, **kwargs) -> str:
"""Allows you to send airtime to a phone number.
Parameters
----------
phone_number: str : The phone number to send airtime to.
It should be in the international format.
eg. +254712345678 (Kenya) -
+254 is the country code. 712345678 is the phone number.
currency_code: str :
The 3-letter ISO currency code. eg. KES for Kenya Shillings.
amount: str :
The amount of airtime to send. It should be a string. eg. "10"
That means you'll send airtime worth 10 currency units.
Returns
-------
None
Examples
--------
send_airtime("+254712345678", "KES", "10")
"""
# Load credentials from environment variables
username = os.getenv("AT_USERNAME")
api_key = os.getenv("AT_API_KEY")
logger.info("Loaded the credentials: %s %s", username, mask_api_key(api_key))
# Initialize the SDK
africastalking.initialize(username, api_key)
# Get the airtime service
airtime = africastalking.Airtime
# Mask the phone number for logging
masked_number = mask_phone_number(phone_number)
logger.info("Sending airtime to %s", masked_number)
try:
# Send airtime
responses = airtime.send(
phone_number=phone_number, amount=amount, currency_code=currency_code
)
logger.debug("The response from sending airtime: %s", responses)
return json.dumps(responses)
except Exception as e:
logger.error("Encountered an error while sending airtime: %s", str(e))
return json.dumps({"error": str(e)})
def send_message(phone_number: str, message: str, username: str, **kwargs) -> None:
"""Allows you to send a message to a phone number.
Parameters
----------
phone_number: str :
The phone number to send the message to.
It should be in the international format.
eg. +254712345678 (Kenya) -
+254 is the country code. 712345678 is the phone number.
message: str : The message to send. It should be a string.
eg. "Hello, this is a test message"
username: str : The username to use for sending the message.
this is the username you used to sign up for the Africa's Talking account.
Returns
-------
None
Examples
--------
send_message("+254712345678", "Hello there", "jak2")
"""
# Load API key from environment variables
api_key = os.getenv("AT_API_KEY")
logger.info("Loaded the API key: %s", mask_api_key(api_key))
# Initialize the SDK
africastalking.initialize(username, api_key)
# Get the SMS service
sms = africastalking.SMS
# Mask the phone number for logging
masked_number = mask_phone_number(phone_number)
logger.info("Sending message to %s", masked_number)
try:
# Send the message
response = sms.send(message, [phone_number])
logger.debug("Message sent to %s. Response: %s", masked_number, response)
return json.dumps(response)
except Exception as e:
logger.error("Encountered an error while sending the message: %s", str(e))
return json.dumps({"error": str(e)})
def search_news(query: str, **kwargs) -> str:
"""Search for news using DuckDuckGo search engine based on the query provided.
Parameters
----------
query: str : The query to search for.
Returns
-------
str : The search results.
Examples
--------
search_news("Python programming")
"""
logging.info("Searching for news based on the query: %s", query)
ddgs = DDGS()
results = ddgs.news(
keywords=query,
region="wt-wt",
safesearch="off",
timelimit="d",
max_results=5,
**kwargs
)
logger.debug("The search results are: %s", results)
return json.dumps(results)
# Asynchronous function to handle the conversation with the model
async def run(model: str, user_input: str):
"""Run the conversation with the model.
Parameters
----------
model : str : The model to use for the conversation.
user_input : str : The user query to start the conversation.
Returns
-------
None
Examples
--------
asyncio.run(run("llama3.1",
"Send airtime to +254712345678 with an amount of 10 in currency KES"))
"""
client = ollama.AsyncClient()
# Initialize conversation with a user query
messages = [
{
"role": "user",
"content": user_input,
}
]
# First API call: Send the query and function description to the model
response = await client.chat(
model=model,
messages=messages,
# keep_alive=True,
tools=[
{
"type": "function",
"function": {
"name": "send_airtime",
"description": "Send airtime to a phone number using the Africa's Talking API",
"parameters": {
"type": "object",
"properties": {
"phone_number": {
"type": "string",
"description": "The phone number in international format",
},
"currency_code": {
"type": "string",
"description": "The 3-letter ISO currency code",
},
"amount": {
"type": "string",
"description": "The amount of airtime to send",
},
},
"required": ["phone_number", "currency_code", "amount"],
},
},
},
{
"type": "function",
"function": {
"name": "send_message",
"description": "Send a message to a phone number using the Africa's Talking API",
"parameters": {
"type": "object",
"properties": {
"phone_number": {
"type": "string",
"description": "The phone number in international format",
},
"message": {
"type": "string",
"description": "The message to send",
},
"username": {
"type": "string",
"description": "The username for the Africa's Talking account",
},
},
"required": ["phone_number", "message", "username"],
},
},
},
{
"type": "function",
"function": {
"name": "search_news",
"description": "Search for news articles using DuckDuckGo News API",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query for news articles",
},
"max_results": {
"type": "integer",
"description": "The maximum number of news articles to retrieve",
"default": 5,
},
},
"required": ["query"],
},
},
},
],
)
# Add the model's response to the conversation history
messages.append(response["message"])
# Check if the model decided to use the provided function
if not response["message"].get("tool_calls"):
logger.debug("The model didn't use the function. Its response was:")
logger.debug(response["message"]["content"])
return None
if response["message"].get("tool_calls"):
# Dictionary of available functions
available_functions = {
"send_airtime": send_airtime,
"send_message": send_message,
"search_news": search_news,
}
for tool in response["message"]["tool_calls"]:
# Get the function to call based on the tool name
function_to_call = available_functions[tool["function"]["name"]]
logger.debug("function to call: %s", function_to_call)
# Call the function with the provided arguments
if tool["function"]["name"] == "send_airtime":
function_response = function_to_call(
tool["function"]["arguments"]["phone_number"],
tool["function"]["arguments"]["currency_code"],
tool["function"]["arguments"]["amount"],
)
elif tool["function"]["name"] == "send_message":
function_response = function_to_call(
tool["function"]["arguments"]["phone_number"],
tool["function"]["arguments"]["message"],
tool["function"]["arguments"]["username"],
)
logger.debug("function response: %s", function_response)
elif tool["function"]["name"] == "search_news":
function_response = function_to_call(
tool["function"]["arguments"]["query"],
max_results=tool["function"]["arguments"].get("max_results", 5),
)
logger.debug("function response: %s", function_response)
# Add the function response to the conversation history
messages.append(
{
"role": "tool",
"content": function_response,
}
)
# Main loop to get user input and run the conversation
if __name__ == "__main__":
while True:
user_prompt = input(
"\n Hi, this is a 📱 Multi-Service Communication Interface 🌍: you can send airtime and messages using this interface for example \n\n"
"Send airtime to +254712345678 with an amount of 10 in currency KES \n\n"
"Send a message to +254712345678 with the message 'Hello there', using the username 'your_username'\n\n"
"You can also search for news by providing a query like 'Python programming'\n\n"
"=> "
)
if not user_prompt:
logger.info("No input provided. Exiting...")
break
elif user_prompt.lower() == "exit":
break
# Run the asynchronous function with tracker
# with EmissionsTracker(
# measure_power_secs=15,
# tracking_mode="offline",
# output_dir="carbon_output",
# project_name="function_call",
# experiment_name="send_airtime_and_messages",
# ) as tracker:
asyncio.run(run("qwen2.5:0.5b", user_input=user_prompt))
# tracker.stop()