-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
106 lines (93 loc) · 2.77 KB
/
functions.py
File metadata and controls
106 lines (93 loc) · 2.77 KB
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
import requests
import os
import json
from datetime import datetime
def get_current_weather(latitude, longitude):
"""Get the current weather in a given latitude and longitude"""
base = "https://api.openweathermap.org/data/2.5/weather"
key = os.environ['WEATHERMAP_API_KEY']
request_url = f"{base}?lat={latitude}&lon={longitude}&appid={key}&units=metric"
response = requests.get(request_url)
body = response.json()
if not "main" in body:
return json.dumps({
"latitude": latitude,
"longitude": longitude,
"error": body
})
else:
return json.dumps({
"latitude": latitude,
"longitude": longitude,
**body["main"]
})
# Function to get the current time
def get_current_time():
"""Get the current time in a simple 12-hour format."""
now = datetime.now()
return json.dumps({
"current_time": now.strftime('%I:%M%p')
})
def end_conversation(transcriber):
"""End the current conversation and close the channel."""
print("Clearing conversation history and closing channel")
# Clear conversation history
transcriber.current_conversation = None
# Close the channel
transcriber.channel_open = False
# Play close channel sound
transcriber.close_channel()
return json.dumps({
"message": "The conversation has been ended.",
"action": "end_conversation"
})
definitions = [
# get current time
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "Get the current time in a simple 12-hour format (e.g., '11:58AM').",
"parameters": {
"type": "object",
"properties": {},
"required": [],
},
},
},
# get current weather
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given latitude and longitude",
"parameters": {
"type": "object",
"properties": {
"latitude": {
"type": "number",
"description": "The latitude of a place",
},
"longitude": {
"type": "number",
"description": "The longitude of a place",
},
},
"required": ["latitude", "longitude"],
},
},
},
{
"type": "function",
"function": {
"name": "end_conversation",
"description": "End the current conversation and close the channel.",
"parameters": {
"type": "object",
"properties": {},
"required": [],
},
},
}
]
functions_json = json.dumps(definitions)