-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·111 lines (81 loc) · 3.56 KB
/
Copy pathmain.py
File metadata and controls
executable file
·111 lines (81 loc) · 3.56 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
import uvicorn
import json
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
from loguru import logger
from openai import AsyncOpenAI
from config import HELICONE_API_KEY, SYSTEM_PROMPT, TOOLS
from functions import run_command, save_code, search, fetch_page
app = FastAPI()
openai_client = AsyncOpenAI(
base_url="https://oai.helicone.ai/v1",
default_headers={
"Helicone-Auth": f"Bearer {HELICONE_API_KEY}"
}
)
@app.get("/")
async def index():
with open("index.html", "r", encoding="UTF-8") as f:
html = f.read()
return HTMLResponse(html)
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
chat_history = [{
"role": "system",
"content": SYSTEM_PROMPT
}]
try:
while True:
data_frontend = await websocket.receive_text()
user_input = data_frontend.strip()
logger.debug(f"Сообщение от фронта: {user_input}")
chat_history.append({
"role": "user",
"content": user_input
})
while True:
ai_response = await openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=chat_history,
tools=TOOLS,
tool_choice="auto",
parallel_tool_calls=True
)
ai_message = ai_response.choices[0].message
chat_history.append(ai_message.to_dict())
if not ai_message.tool_calls:
await websocket.send_text(json.dumps({
"role": "assistant",
"content": ai_message.content
}))
break
for tool_call in ai_message.tool_calls:
func_name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
result = ""
try:
if func_name == "run_command":
if "input_str" in args:
result = run_command(args["command"], args["input_str"])
else:
result = run_command(args["command"])
elif func_name == "save_code":
result = save_code(args["code"], args["filename"])
elif func_name == "search":
result = search(args["query"])
elif func_name == "fetch_page":
result = await fetch_page(args["url"])
else:
result = f"Неизвестная функция {func_name}"
except Exception as e:
result = f"Ошибка вызова функции {func_name} {str(e)}"
chat_history.append({
"role": "tool",
"content": result,
"tool_call_id": tool_call.id
})
except WebSocketDisconnect:
logger.error("Клиент отсоединил соединение")
if __name__ == "__main__":
uvicorn.run("main:app", host="localhost", port=8000, reload=False)