-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathindex.py
More file actions
172 lines (135 loc) · 6.05 KB
/
index.py
File metadata and controls
172 lines (135 loc) · 6.05 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
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
import os
import json
import uuid
from typing import List
from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam
from pydantic import BaseModel
from dotenv import load_dotenv
from fastapi import FastAPI, Query
from fastapi.responses import StreamingResponse
from openai import AsyncOpenAI
from .utils.prompt import ClientMessage, convert_to_openai_messages
from .utils.tools import get_current_weather
load_dotenv(".env.local")
app = FastAPI()
client = AsyncOpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
class Request(BaseModel):
messages: List[ClientMessage]
available_tools = {
"get_current_weather": get_current_weather,
}
async def stream_text(messages: List[ChatCompletionMessageParam], protocol: str = 'data'):
message_id = f"msg_{uuid.uuid4().hex}"
yield f'data: {json.dumps({"type": "start", "messageId": message_id})}\n\n'
conversation_messages = list(messages)
while True:
text_id = f"text_{uuid.uuid4().hex}"
text_started = False
draft_tool_calls = []
draft_tool_calls_index = -1
stream = await client.chat.completions.create(
messages=conversation_messages,
model="gpt-4o",
stream=True,
tools=[{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather at a location",
"parameters": {
"type": "object",
"properties": {
"latitude": {
"type": "number",
"description": "The latitude of the location",
},
"longitude": {
"type": "number",
"description": "The longitude of the location",
},
},
"required": ["latitude", "longitude"],
},
},
}]
)
finish_reason = None
async for chunk in stream:
for choice in chunk.choices:
if choice.delta.tool_calls:
for tool_call in choice.delta.tool_calls:
id = tool_call.id
name = tool_call.function.name
arguments = tool_call.function.arguments
if (id is not None):
draft_tool_calls_index += 1
draft_tool_calls.append(
{"id": id, "name": name, "arguments": ""})
yield f'data: {json.dumps({"type": "tool-input-start", "toolCallId": id, "toolName": name})}\n\n'
if arguments:
draft_tool_calls[draft_tool_calls_index]["arguments"] += arguments
yield f'data: {json.dumps({"type": "tool-input-delta", "toolCallId": draft_tool_calls[draft_tool_calls_index]["id"], "inputTextDelta": arguments})}\n\n'
if choice.delta.content:
if not text_started:
yield f'data: {json.dumps({"type": "text-start", "id": text_id})}\n\n'
text_started = True
yield f'data: {json.dumps({"type": "text-delta", "id": text_id, "delta": choice.delta.content})}\n\n'
if choice.finish_reason:
finish_reason = choice.finish_reason
if text_started:
yield f'data: {json.dumps({"type": "text-end", "id": text_id})}\n\n'
text_started = False
if finish_reason == "tool_calls":
tool_calls_for_message = [
{
"id": tc["id"],
"type": "function",
"function": {
"name": tc["name"],
"arguments": tc["arguments"]
}
}
for tc in draft_tool_calls
]
conversation_messages.append({
"role": "assistant",
"tool_calls": tool_calls_for_message
})
for tool_call in draft_tool_calls:
parsed_args = json.loads(tool_call["arguments"])
yield f'data: {json.dumps({"type": "tool-input-available", "toolCallId": tool_call["id"], "toolName": tool_call["name"], "input": parsed_args})}\n\n'
tool_result = available_tools[tool_call["name"]](**parsed_args)
yield f'data: {json.dumps({"type": "tool-output-available", "toolCallId": tool_call["id"], "output": tool_result})}\n\n'
conversation_messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps(tool_result)
})
yield f'data: {json.dumps({"type": "finish-step"})}\n\n'
continue
elif finish_reason == "stop":
break
yield f'data: {json.dumps({"type": "finish"})}\n\n'
yield f'data: [DONE]\n\n'
@app.post("/api/chat")
async def handle_chat_data(request: Request, protocol: str = Query('data')):
try:
messages = request.messages
openai_messages = convert_to_openai_messages(messages)
return StreamingResponse(
stream_text(openai_messages, protocol),
media_type="text/event-stream",
headers={
'x-vercel-ai-ui-message-stream': 'v1',
'Cache-Control': 'no-cache, no-transform',
'X-Accel-Buffering': 'no',
'Connection': 'keep-alive',
'Content-Type': 'text/event-stream',
}
)
except Exception as e:
import traceback
traceback.print_exc()
raise