tianlong12
commited on
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
8 |
+
from fastapi.responses import StreamingResponse
|
9 |
+
from pydantic import BaseModel
|
10 |
+
from typing import List, Optional, Union
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
class ChatCompletionMessage(BaseModel):
|
15 |
+
role: str
|
16 |
+
content: str
|
17 |
+
|
18 |
+
class ChatCompletionRequest(BaseModel):
|
19 |
+
model: str
|
20 |
+
messages: List[ChatCompletionMessage]
|
21 |
+
temperature: Optional[float] = 1.0
|
22 |
+
max_tokens: Optional[int] = None
|
23 |
+
stream: Optional[bool] = False
|
24 |
+
|
25 |
+
class ChatCompletionResponse(BaseModel):
|
26 |
+
id: str
|
27 |
+
object: str
|
28 |
+
created: int
|
29 |
+
model: str
|
30 |
+
choices: List[dict]
|
31 |
+
usage: dict
|
32 |
+
|
33 |
+
def generate_random_ip():
|
34 |
+
return f"{random.randint(1,255)}.{random.randint(0,255)}.{random.randint(0,255)}.{random.randint(0,255)}"
|
35 |
+
|
36 |
+
async def fetch_response(messages: List[ChatCompletionMessage], model: str):
|
37 |
+
your_api_url = "https://chatpro.ai-pro.org/api/ask/openAI"
|
38 |
+
headers = {
|
39 |
+
"content-type": "application/json",
|
40 |
+
"X-Forwarded-For": generate_random_ip(),
|
41 |
+
"origin": "https://chatpro.ai-pro.org",
|
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 = {
|
49 |
+
"text": conversation,
|
50 |
+
"endpoint": "openAI",
|
51 |
+
"model": model
|
52 |
+
}
|
53 |
+
|
54 |
+
response = requests.post(your_api_url, headers=headers, json=data)
|
55 |
+
|
56 |
+
if response.status_code != 200:
|
57 |
+
raise HTTPException(status_code=response.status_code, detail="Error from upstream API")
|
58 |
+
|
59 |
+
return response.json()
|
60 |
+
|
61 |
+
async def stream_response(content: str):
|
62 |
+
chunk_size = len(content) # 将整个内容作为一个块发送
|
63 |
+
chat_id = f"chatcmpl-{os.urandom(12).hex()}"
|
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("/hf/v1/chat/completions")
|
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", "")
|
103 |
+
|
104 |
+
if chat_request.stream:
|
105 |
+
return StreamingResponse(stream_response(content), media_type="text/event-stream")
|
106 |
+
else:
|
107 |
+
openai_response = ChatCompletionResponse(
|
108 |
+
id="chatcmpl-" + os.urandom(12).hex(),
|
109 |
+
object="chat.completion",
|
110 |
+
created=int(time.time()),
|
111 |
+
model=chat_request.model,
|
112 |
+
choices=[
|
113 |
+
{
|
114 |
+
"index": 0,
|
115 |
+
"message": {
|
116 |
+
"role": "assistant",
|
117 |
+
"content": content
|
118 |
+
},
|
119 |
+
"finish_reason": "stop"
|
120 |
+
}
|
121 |
+
],
|
122 |
+
usage={
|
123 |
+
"prompt_tokens": sum(len(msg.content) for msg in chat_request.messages),
|
124 |
+
"completion_tokens": len(content),
|
125 |
+
"total_tokens": sum(len(msg.content) for msg in chat_request.messages) + len(content)
|
126 |
+
}
|
127 |
+
)
|
128 |
+
return openai_response
|