Spaces:
Runtime error
Runtime error
tianlong12
commited on
Commit
•
ce0c73c
1
Parent(s):
6984080
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import os
|
2 |
import time
|
3 |
import random
|
4 |
-
import json
|
5 |
import asyncio
|
6 |
import requests
|
7 |
from fastapi import FastAPI, HTTPException, Request
|
@@ -42,7 +41,10 @@ async def fetch_response(messages: List[ChatCompletionMessage], model: str):
|
|
42 |
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
|
43 |
}
|
44 |
|
|
|
45 |
conversation = "\n".join([f"{msg.role}: {msg.content}" for msg in messages])
|
|
|
|
|
46 |
conversation += "\n请关注并回复user最近的消息并避免总结对话历史的回答"
|
47 |
|
48 |
data = {
|
@@ -59,44 +61,17 @@ async def fetch_response(messages: List[ChatCompletionMessage], model: str):
|
|
59 |
return response.json()
|
60 |
|
61 |
async def stream_response(content: str):
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
# 发送开始的块
|
66 |
-
yield f"data: {json.dumps({
|
67 |
-
'id': chat_id,
|
68 |
-
'object': 'chat.completion.chunk',
|
69 |
-
'created': int(time.time()),
|
70 |
-
'model': 'gpt-3.5-turbo-0613',
|
71 |
-
'choices': [{
|
72 |
-
'index': 0,
|
73 |
-
'delta': {
|
74 |
-
'content': content
|
75 |
-
},
|
76 |
-
'finish_reason': None
|
77 |
-
}]
|
78 |
-
})}\n\n"
|
79 |
-
|
80 |
-
# 发送结束的块
|
81 |
-
yield f"data: {json.dumps({
|
82 |
-
'id': chat_id,
|
83 |
-
'object': 'chat.completion.chunk',
|
84 |
-
'created': int(time.time()),
|
85 |
-
'model': 'gpt-3.5-turbo-0613',
|
86 |
-
'choices': [{
|
87 |
-
'index': 0,
|
88 |
-
'delta': {},
|
89 |
-
'finish_reason': 'stop'
|
90 |
-
}]
|
91 |
-
})}\n\n"
|
92 |
-
|
93 |
yield 'data: [DONE]\n\n'
|
94 |
|
95 |
-
@app.post("/
|
96 |
async def chat_completions(request: Request):
|
97 |
body = await request.json()
|
98 |
chat_request = ChatCompletionRequest(**body)
|
99 |
|
|
|
100 |
api_response = await fetch_response(chat_request.messages, chat_request.model)
|
101 |
|
102 |
content = api_response.get("response", "")
|
|
|
1 |
import os
|
2 |
import time
|
3 |
import random
|
|
|
4 |
import asyncio
|
5 |
import requests
|
6 |
from fastapi import FastAPI, HTTPException, Request
|
|
|
41 |
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
|
42 |
}
|
43 |
|
44 |
+
# 将消息列表转换为单个字符串,保留对话历史
|
45 |
conversation = "\n".join([f"{msg.role}: {msg.content}" for msg in messages])
|
46 |
+
|
47 |
+
# 添加指导语
|
48 |
conversation += "\n请关注并回复user最近的消息并避免总结对话历史的回答"
|
49 |
|
50 |
data = {
|
|
|
61 |
return response.json()
|
62 |
|
63 |
async def stream_response(content: str):
|
64 |
+
# Send the entire content as a single chunk
|
65 |
+
yield f"data: {{'id': 'chatcmpl-{os.urandom(12).hex()}', 'object': 'chat.completion.chunk', 'created': 1677652288, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'delta': {{'content': '{content}'}}, 'finish_reason': None}]}}\n\n"
|
66 |
+
yield f"data: {{'id': 'chatcmpl-{os.urandom(12).hex()}', 'object': 'chat.completion.chunk', 'created': 1677652288, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'delta': {{}}, 'finish_reason': 'stop'}]}}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
yield 'data: [DONE]\n\n'
|
68 |
|
69 |
+
@app.post("/v1/chat/completions")
|
70 |
async def chat_completions(request: Request):
|
71 |
body = await request.json()
|
72 |
chat_request = ChatCompletionRequest(**body)
|
73 |
|
74 |
+
# 传递整个消息历史到API
|
75 |
api_response = await fetch_response(chat_request.messages, chat_request.model)
|
76 |
|
77 |
content = api_response.get("response", "")
|