Niansuh commited on
Commit
0a9027d
·
verified ·
1 Parent(s): 532a10c

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +8 -71
api/utils.py CHANGED
@@ -11,6 +11,7 @@ from api import validate
11
  from api.config import (
12
  APP_SECRET,
13
  BASE_URL,
 
14
  AGENT_MODE,
15
  TRENDING_AGENT_MODE,
16
  MODEL_PREFIXES,
@@ -74,6 +75,10 @@ async def process_streaming_response(request: ChatRequest):
74
  hid = validate.getHid()
75
  logger.info(f"Using hid: {hid} for model: {request.model}")
76
 
 
 
 
 
77
  json_data = {
78
  "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
79
  "previewToken": None,
@@ -93,7 +98,7 @@ async def process_streaming_response(request: ChatRequest):
93
  "clickedForceWebSearch": False,
94
  "visitFromDelta": False,
95
  "mobileClient": False,
96
- "userSelectedModel": request.model, # Use the requested model directly
97
  "validated": hid
98
  }
99
 
@@ -110,7 +115,7 @@ async def process_streaming_response(request: ChatRequest):
110
  async for line in response.aiter_lines():
111
  timestamp = int(datetime.now().timestamp())
112
  if line:
113
- content = line # Do not modify content
114
  if "https://www.blackbox.ai" in content:
115
  validate.getHid(True)
116
  content = "hid已刷新,重新对话即可"
@@ -119,7 +124,6 @@ async def process_streaming_response(request: ChatRequest):
119
  break
120
  if content.startswith("$@$v=undefined-rv1$@$"):
121
  content = content[21:]
122
- # Do not strip model prefix or modify content
123
  yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
124
 
125
  yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
@@ -131,71 +135,4 @@ async def process_streaming_response(request: ChatRequest):
131
  logger.error(f"Error occurred during request: {e}")
132
  raise HTTPException(status_code=500, detail=str(e))
133
 
134
- async def process_non_streaming_response(request: ChatRequest):
135
- agent_mode = AGENT_MODE.get(request.model, {})
136
- trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
137
- model_prefix = MODEL_PREFIXES.get(request.model, "")
138
-
139
- hid = validate.getHid()
140
- logger.info(f"Using hid: {hid} for model: {request.model}")
141
-
142
- json_data = {
143
- "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
144
- "previewToken": None,
145
- "userId": None,
146
- "codeModelMode": True,
147
- "agentMode": agent_mode,
148
- "trendingAgentMode": trending_agent_mode,
149
- "isMicMode": False,
150
- "userSystemPrompt": None,
151
- "maxTokens": request.max_tokens,
152
- "playgroundTopP": request.top_p,
153
- "playgroundTemperature": request.temperature,
154
- "isChromeExt": False,
155
- "githubToken": None,
156
- "clickedAnswer2": False,
157
- "clickedAnswer3": False,
158
- "clickedForceWebSearch": False,
159
- "visitFromDelta": False,
160
- "mobileClient": False,
161
- "userSelectedModel": request.model, # Use the requested model directly
162
- "validated": hid
163
- }
164
- full_response = ""
165
- async with httpx.AsyncClient() as client:
166
- try:
167
- async with client.stream(
168
- method="POST", url=f"{BASE_URL}/api/chat", headers=headers, json=json_data
169
- ) as response:
170
- response.raise_for_status()
171
- async for chunk in response.aiter_text():
172
- full_response += chunk
173
- except httpx.HTTPStatusError as e:
174
- logger.error(f"HTTP error occurred: {e}")
175
- raise HTTPException(status_code=e.response.status_code, detail=str(e))
176
- except httpx.RequestError as e:
177
- logger.error(f"Error occurred during request: {e}")
178
- raise HTTPException(status_code=500, detail=str(e))
179
-
180
- if "https://www.blackbox.ai" in full_response:
181
- validate.getHid(True)
182
- full_response = "hid已刷新,重新对话即可"
183
- logger.info("hid refreshed due to response content")
184
-
185
- if full_response.startswith("$@$v=undefined-rv1$@$"):
186
- full_response = full_response[21:]
187
- # Do not strip model prefix or modify content
188
- return {
189
- "id": f"chatcmpl-{uuid.uuid4()}",
190
- "object": "chat.completion",
191
- "created": int(datetime.now().timestamp()),
192
- "model": request.model,
193
- "choices": [
194
- {
195
- "index": 0,
196
- "message": {"role": "assistant", "content": full_response},
197
- "finish_reason": "stop",
198
- }
199
- ],
200
- "usage": None,
201
- }
 
11
  from api.config import (
12
  APP_SECRET,
13
  BASE_URL,
14
+ MODEL_MAPPING,
15
  AGENT_MODE,
16
  TRENDING_AGENT_MODE,
17
  MODEL_PREFIXES,
 
75
  hid = validate.getHid()
76
  logger.info(f"Using hid: {hid} for model: {request.model}")
77
 
78
+ user_selected_model = MODEL_MAPPING.get(request.model, request.model)
79
+ logger.info(f"Processing request for model: {request.model}")
80
+ logger.info(f"Using userSelectedModel: {user_selected_model}")
81
+
82
  json_data = {
83
  "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
84
  "previewToken": None,
 
98
  "clickedForceWebSearch": False,
99
  "visitFromDelta": False,
100
  "mobileClient": False,
101
+ "userSelectedModel": user_selected_model,
102
  "validated": hid
103
  }
104
 
 
115
  async for line in response.aiter_lines():
116
  timestamp = int(datetime.now().timestamp())
117
  if line:
118
+ content = line
119
  if "https://www.blackbox.ai" in content:
120
  validate.getHid(True)
121
  content = "hid已刷新,重新对话即可"
 
124
  break
125
  if content.startswith("$@$v=undefined-rv1$@$"):
126
  content = content[21:]
 
127
  yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
128
 
129
  yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
 
135
  logger.error(f"Error occurred during request: {e}")
136
  raise HTTPException(status_code=500, detail=str(e))
137
 
138
+ # Similar updates for process_non_streaming_response