Update api/utils.py
Browse files- api/utils.py +2 -94
api/utils.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
from datetime import datetime
|
2 |
-
from http.client import HTTPException
|
3 |
import json
|
4 |
from typing import Any, Dict, Optional
|
5 |
import uuid
|
6 |
|
7 |
import httpx
|
8 |
from api.config import MODEL_MAPPING, AGENT_MODE, TRENDING_AGENT_MODE, headers
|
9 |
-
from fastapi import Depends,
|
10 |
from fastapi.security import HTTPAuthorizationCredentials
|
11 |
|
12 |
from api.config import APP_SECRET, BASE_URL
|
@@ -16,51 +15,7 @@ from api.logger import setup_logger
|
|
16 |
|
17 |
logger = setup_logger(__name__)
|
18 |
|
19 |
-
|
20 |
-
def create_chat_completion_data(
|
21 |
-
content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
|
22 |
-
) -> Dict[str, Any]:
|
23 |
-
return {
|
24 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
25 |
-
"object": "chat.completion.chunk",
|
26 |
-
"created": timestamp,
|
27 |
-
"model": model,
|
28 |
-
"choices": [
|
29 |
-
{
|
30 |
-
"index": 0,
|
31 |
-
"delta": {"content": content, "role": "assistant"},
|
32 |
-
"finish_reason": finish_reason,
|
33 |
-
}
|
34 |
-
],
|
35 |
-
"usage": None,
|
36 |
-
}
|
37 |
-
|
38 |
-
|
39 |
-
def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
40 |
-
if credentials.credentials != APP_SECRET:
|
41 |
-
raise HTTPException(status_code=403, detail="Invalid APP_SECRET")
|
42 |
-
return credentials.credentials
|
43 |
-
|
44 |
-
|
45 |
-
def message_to_dict(message):
|
46 |
-
if isinstance(message.content, str):
|
47 |
-
result = {"role": message.role, "content": message.content}
|
48 |
-
if hasattr(message, 'data'):
|
49 |
-
result['data'] = message.data
|
50 |
-
return result
|
51 |
-
elif isinstance(message.content, list) and len(message.content) == 2:
|
52 |
-
return {
|
53 |
-
"role": message.role,
|
54 |
-
"content": message.content[0]["text"],
|
55 |
-
"data": {
|
56 |
-
"imageBase64": message.content[1]["image_url"]["url"],
|
57 |
-
"fileText": "",
|
58 |
-
"title": "snapshoot",
|
59 |
-
},
|
60 |
-
}
|
61 |
-
else:
|
62 |
-
return {"role": message.role, "content": message.content}
|
63 |
-
|
64 |
|
65 |
async def process_streaming_response(request: ChatRequest):
|
66 |
json_data = {
|
@@ -112,50 +67,3 @@ async def process_streaming_response(request: ChatRequest):
|
|
112 |
except httpx.RequestError as e:
|
113 |
logger.error(f"Error occurred during request: {e}")
|
114 |
raise HTTPException(status_code=500, detail=str(e))
|
115 |
-
|
116 |
-
|
117 |
-
async def process_non_streaming_response(request: ChatRequest):
|
118 |
-
json_data = {
|
119 |
-
"messages": [message_to_dict(msg) for msg in request.messages],
|
120 |
-
"previewToken": None,
|
121 |
-
"userId": None,
|
122 |
-
"codeModelMode": True,
|
123 |
-
"agentMode": AGENT_MODE.get(request.model, {}),
|
124 |
-
"trendingAgentMode": TRENDING_AGENT_MODE.get(request.model, {}),
|
125 |
-
"isMicMode": False,
|
126 |
-
"userSystemPrompt": None,
|
127 |
-
"maxTokens": request.max_tokens,
|
128 |
-
"playgroundTopP": request.top_p,
|
129 |
-
"playgroundTemperature": request.temperature,
|
130 |
-
"isChromeExt": False,
|
131 |
-
"githubToken": None,
|
132 |
-
"clickedAnswer2": False,
|
133 |
-
"clickedAnswer3": False,
|
134 |
-
"clickedForceWebSearch": False,
|
135 |
-
"visitFromDelta": False,
|
136 |
-
"mobileClient": False,
|
137 |
-
"userSelectedModel": MODEL_MAPPING.get(request.model, request.model),
|
138 |
-
}
|
139 |
-
full_response = ""
|
140 |
-
async with httpx.AsyncClient() as client:
|
141 |
-
async with client.stream(
|
142 |
-
method="POST", url=f"{BASE_URL}/api/chat", headers=headers, json=json_data
|
143 |
-
) as response:
|
144 |
-
async for chunk in response.aiter_text():
|
145 |
-
full_response += chunk
|
146 |
-
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
147 |
-
full_response = full_response[21:]
|
148 |
-
return {
|
149 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
150 |
-
"object": "chat.completion",
|
151 |
-
"created": int(datetime.now().timestamp()),
|
152 |
-
"model": request.model,
|
153 |
-
"choices": [
|
154 |
-
{
|
155 |
-
"index": 0,
|
156 |
-
"message": {"role": "assistant", "content": full_response},
|
157 |
-
"finish_reason": "stop",
|
158 |
-
}
|
159 |
-
],
|
160 |
-
"usage": None,
|
161 |
-
}
|
|
|
1 |
from datetime import datetime
|
|
|
2 |
import json
|
3 |
from typing import Any, Dict, Optional
|
4 |
import uuid
|
5 |
|
6 |
import httpx
|
7 |
from api.config import MODEL_MAPPING, AGENT_MODE, TRENDING_AGENT_MODE, headers
|
8 |
+
from fastapi import Depends, HTTPException # Corrected import
|
9 |
from fastapi.security import HTTPAuthorizationCredentials
|
10 |
|
11 |
from api.config import APP_SECRET, BASE_URL
|
|
|
15 |
|
16 |
logger = setup_logger(__name__)
|
17 |
|
18 |
+
# ... [Other code remains the same] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
async def process_streaming_response(request: ChatRequest):
|
21 |
json_data = {
|
|
|
67 |
except httpx.RequestError as e:
|
68 |
logger.error(f"Error occurred during request: {e}")
|
69 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|