-
Notifications
You must be signed in to change notification settings - Fork 195
/
function_calling_bot.py
84 lines (67 loc) · 2.56 KB
/
function_calling_bot.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
"""
Sample bot that demonstrates how to use OpenAI function calling with the Poe API.
"""
from __future__ import annotations
import json
from typing import AsyncIterable
import fastapi_poe as fp
from modal import App, Image, asgi_app
def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
if "tokyo" in location.lower():
return json.dumps({"location": "Tokyo", "temperature": "11", "unit": unit})
elif "san francisco" in location.lower():
return json.dumps(
{"location": "San Francisco", "temperature": "72", "unit": unit}
)
elif "paris" in location.lower():
return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
else:
return json.dumps({"location": location, "temperature": "unknown"})
tools_executables = [get_current_weather]
tools_dict_list = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
tools = [fp.ToolDefinition(**tools_dict) for tools_dict in tools_dict_list]
class GPT35FunctionCallingBot(fp.PoeBot):
async def get_response(
self, request: fp.QueryRequest
) -> AsyncIterable[fp.PartialResponse]:
async for msg in fp.stream_request(
request,
"GPT-3.5-Turbo",
request.access_key,
tools=tools,
tool_executables=tools_executables,
):
yield msg
async def get_settings(self, setting: fp.SettingsRequest) -> fp.SettingsResponse:
return fp.SettingsResponse(server_bot_dependencies={"GPT-3.5-Turbo": 2})
REQUIREMENTS = ["fastapi-poe==0.0.48"]
image = Image.debian_slim().pip_install(*REQUIREMENTS)
app = App("function-calling-poe")
@app.function(image=image)
@asgi_app()
def fastapi_app():
bot = GPT35FunctionCallingBot()
# see https://creator.poe.com/docs/quick-start#configuring-the-access-credentials
# app = fp.make_app(bot, access_key=<YOUR_ACCESS_KEY>, bot_name=<YOUR_BOT_NAME>)
app = fp.make_app(bot, allow_without_key=True)
return app