Niansuh commited on
Commit
9bb866e
·
verified ·
1 Parent(s): a3d8a0f

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +5 -76
api/utils.py CHANGED
@@ -5,8 +5,8 @@ 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
12
  from api.models import ChatRequest
@@ -15,30 +15,7 @@ from api.logger import setup_logger
15
 
16
  logger = setup_logger(__name__)
17
 
18
- def create_chat_completion_data(
19
- content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
20
- ) -> Dict[str, Any]:
21
- return {
22
- "id": f"chatcmpl-{uuid.uuid4()}",
23
- "object": "chat.completion.chunk",
24
- "created": timestamp,
25
- "model": model,
26
- "choices": [
27
- {
28
- "index": 0,
29
- "delta": {"content": content, "role": "assistant"},
30
- "finish_reason": finish_reason,
31
- }
32
- ],
33
- "usage": None,
34
- }
35
-
36
-
37
- def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(security)):
38
- if credentials.credentials != APP_SECRET:
39
- raise HTTPException(status_code=403, detail="Invalid APP_SECRET")
40
- return credentials.credentials
41
-
42
 
43
  def message_to_dict(message):
44
  if isinstance(message.content, str):
@@ -59,7 +36,6 @@ def message_to_dict(message):
59
  else:
60
  return {"role": message.role, "content": message.content}
61
 
62
-
63
  async def process_streaming_response(request: ChatRequest):
64
  json_data = {
65
  "messages": [message_to_dict(msg) for msg in request.messages],
@@ -106,54 +82,7 @@ async def process_streaming_response(request: ChatRequest):
106
  yield "data: [DONE]\n\n"
107
  except httpx.HTTPStatusError as e:
108
  logger.error(f"HTTP error occurred: {e}")
109
- raise HTTPException(status_code=e.response.status_code, detail=str(e))
110
  except httpx.RequestError as e:
111
  logger.error(f"Error occurred during request: {e}")
112
- raise HTTPException(status_code=500, detail=str(e))
113
-
114
-
115
- async def process_non_streaming_response(request: ChatRequest):
116
- json_data = {
117
- "messages": [message_to_dict(msg) for msg in request.messages],
118
- "previewToken": None,
119
- "userId": None,
120
- "codeModelMode": True,
121
- "agentMode": AGENT_MODE.get(request.model, {}),
122
- "trendingAgentMode": TRENDING_AGENT_MODE.get(request.model, {}),
123
- "isMicMode": False,
124
- "userSystemPrompt": None,
125
- "maxTokens": request.max_tokens,
126
- "playgroundTopP": request.top_p,
127
- "playgroundTemperature": request.temperature,
128
- "isChromeExt": False,
129
- "githubToken": None,
130
- "clickedAnswer2": False,
131
- "clickedAnswer3": False,
132
- "clickedForceWebSearch": False,
133
- "visitFromDelta": False,
134
- "mobileClient": False,
135
- "userSelectedModel": MODEL_MAPPING.get(request.model, request.model),
136
- }
137
- full_response = ""
138
- async with httpx.AsyncClient() as client:
139
- async with client.stream(
140
- method="POST", url=f"{BASE_URL}/api/chat", headers=headers, json=json_data
141
- ) as response:
142
- async for chunk in response.aiter_text():
143
- full_response += chunk
144
- if full_response.startswith("$@$v=undefined-rv1$@$"):
145
- full_response = full_response[21:]
146
- return {
147
- "id": f"chatcmpl-{uuid.uuid4()}",
148
- "object": "chat.completion",
149
- "created": int(datetime.now().timestamp()),
150
- "model": request.model,
151
- "choices": [
152
- {
153
- "index": 0,
154
- "message": {"role": "assistant", "content": full_response},
155
- "finish_reason": "stop",
156
- }
157
- ],
158
- "usage": None,
159
- }
 
5
 
6
  import httpx
7
  from api.config import MODEL_MAPPING, AGENT_MODE, TRENDING_AGENT_MODE, headers
8
+ from fastapi import HTTPException
9
+ from api.auth import verify_app_secret # Correctly import verify_app_secret
10
 
11
  from api.config import APP_SECRET, BASE_URL
12
  from api.models import ChatRequest
 
15
 
16
  logger = setup_logger(__name__)
17
 
18
+ # Remove redundant definition of verify_app_secret
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def message_to_dict(message):
21
  if isinstance(message.content, str):
 
36
  else:
37
  return {"role": message.role, "content": message.content}
38
 
 
39
  async def process_streaming_response(request: ChatRequest):
40
  json_data = {
41
  "messages": [message_to_dict(msg) for msg in request.messages],
 
82
  yield "data: [DONE]\n\n"
83
  except httpx.HTTPStatusError as e:
84
  logger.error(f"HTTP error occurred: {e}")
85
+ raise HTTPException(status_code=e.response.status_code, detail="External service error")
86
  except httpx.RequestError as e:
87
  logger.error(f"Error occurred during request: {e}")
88
+ raise HTTPException(status_code=500, detail="Request to external service failed")