bibibi12345 commited on
Commit
63f2284
·
1 Parent(s): ae4042c

added thinking support

Browse files
Files changed (2) hide show
  1. __pycache__/main.cpython-312.pyc +0 -0
  2. main.py +69 -23
__pycache__/main.cpython-312.pyc ADDED
Binary file (14 kB). View file
 
main.py CHANGED
@@ -65,7 +65,7 @@ MODEL_MAPPING = {
65
  BASE_URL = "https://pa002.abacus.ai"
66
 
67
  TIMEOUT = 30.0 # 请求超时时间(秒)
68
- MAX_RETRIES = 3 # 最大重试次数
69
  RETRY_DELAY = 1 # 重试延迟(秒)
70
 
71
  @app.get("/hf/v1/models")
@@ -161,37 +161,48 @@ async def chat_completions(request: Request, chat_request: ChatRequest):
161
  # 创建会话ID
162
  # conversation_id = str(uuid.uuid4())[:-9]
163
  # conversation_id = "d166251ca"
164
- conversation_id = "7f0f8d76d"
165
  # external_id = str(uuid.uuid4())[:-9]
166
  external_id = "f6ca7aa14"
167
 
168
  # 处理消息
169
  full_message = process_messages(chat_request.messages)
170
 
171
- # 准备请求数据
172
- request_data = {
173
- "requestId": str(uuid.uuid4()),
174
- "deploymentConversationId": conversation_id,
175
- "message": full_message,
176
- "isDesktop": True,
177
- "chatConfig": {
178
- "timezone": "America/Los_Angeles",
179
- "language": "en-US"
180
- },
181
- "llmName": MODEL_MAPPING.get(chat_request.model, chat_request.model),
182
- "externalApplicationId": external_id
183
- # "externalApplicationId": "c9a23c6d8"
184
- }
185
 
186
  # 流式请求处理
187
  async def generate_stream():
188
- headers = get_headers(auth_token)
189
- # headers["content-length"] = str(len(str(request_data)))
190
- headers["referer"] = "https://apps.abacus.ai/chatllm/?appId=" + external_id + "&convoId=" + conversation_id
191
- print(headers["referer"])
192
 
 
193
  for retry in range(MAX_RETRIES):
194
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  async with httpx.AsyncClient() as client:
196
  async with client.stream(
197
  "POST",
@@ -200,14 +211,17 @@ async def chat_completions(request: Request, chat_request: ChatRequest):
200
  content=json.dumps(request_data),
201
  timeout=TIMEOUT
202
  ) as response:
 
203
  async for line in response.aiter_lines():
204
  if not line.strip():
205
  continue
206
 
207
  try:
208
  data = json.loads(line)
209
-
210
- if data.get("type") == "text" and data.get("title") != "Thinking...":
 
 
211
  chunk = {
212
  "id": str(uuid.uuid4()),
213
  "object": "chat.completion.chunk",
@@ -216,12 +230,44 @@ async def chat_completions(request: Request, chat_request: ChatRequest):
216
  "choices": [{
217
  "delta": {
218
  "role": "assistant",
219
- "content": data.get("segment", "")
220
  },
221
  "index": 0
222
  }]
223
  }
224
  yield f"data: {json.dumps(chunk)}\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
  if data.get("end"):
227
  # 发送结束标记
 
65
  BASE_URL = "https://pa002.abacus.ai"
66
 
67
  TIMEOUT = 30.0 # 请求超时时间(秒)
68
+ MAX_RETRIES = 1 # 最大重试次数
69
  RETRY_DELAY = 1 # 重试延迟(秒)
70
 
71
  @app.get("/hf/v1/models")
 
161
  # 创建会话ID
162
  # conversation_id = str(uuid.uuid4())[:-9]
163
  # conversation_id = "d166251ca"
164
+ # conversation_id = "7f0f8d76d"
165
  # external_id = str(uuid.uuid4())[:-9]
166
  external_id = "f6ca7aa14"
167
 
168
  # 处理消息
169
  full_message = process_messages(chat_request.messages)
170
 
171
+
172
+ print("start!!!!!")
173
+
174
+ new_chat_data = {"deploymentId":"47845a764","name":"New Chat","externalApplicationId":"f6ca7aa14"}
 
 
 
 
 
 
 
 
 
 
175
 
176
  # 流式请求处理
177
  async def generate_stream():
178
+ headers_new_request = get_headers(auth_token)
179
+ headers_new_request["referer"] = "https://apps.abacus.ai/chatllm/?appId=" + external_id
180
+
 
181
 
182
+ # headers["content-length"] = str(len(str(request_data)))
183
  for retry in range(MAX_RETRIES):
184
  try:
185
+ response = httpx.post("https://apps.abacus.ai/api/createDeploymentConversation",
186
+ data=json.dumps(new_chat_data),
187
+ headers=headers_new_request,
188
+ )
189
+ conversation_id = response.json()["result"]["deploymentConversationId"]
190
+ # 准备请求数据
191
+ request_data = {
192
+ "requestId": str(uuid.uuid4()),
193
+ "deploymentConversationId": conversation_id,
194
+ "message": full_message,
195
+ "isDesktop": True,
196
+ "chatConfig": {
197
+ "timezone": "America/Los_Angeles",
198
+ "language": "en-US"
199
+ },
200
+ "llmName": MODEL_MAPPING.get(chat_request.model, chat_request.model),
201
+ "externalApplicationId": external_id
202
+ # "externalApplicationId": "c9a23c6d8"
203
+ }
204
+ headers = get_headers(auth_token)
205
+ headers["referer"] = "https://apps.abacus.ai/chatllm/?appId=" + external_id + "&convoId=" + conversation_id
206
  async with httpx.AsyncClient() as client:
207
  async with client.stream(
208
  "POST",
 
211
  content=json.dumps(request_data),
212
  timeout=TIMEOUT
213
  ) as response:
214
+ thinking_started = False
215
  async for line in response.aiter_lines():
216
  if not line.strip():
217
  continue
218
 
219
  try:
220
  data = json.loads(line)
221
+ # print(line)
222
+ if data.get("type") == "collapsible_component":
223
+ thinking_started = True
224
+ think_id = data.get("messageId", "")
225
  chunk = {
226
  "id": str(uuid.uuid4()),
227
  "object": "chat.completion.chunk",
 
230
  "choices": [{
231
  "delta": {
232
  "role": "assistant",
233
+ "content": "<think>"
234
  },
235
  "index": 0
236
  }]
237
  }
238
  yield f"data: {json.dumps(chunk)}\n\n"
239
+
240
+ if data.get("type") == "text" and data.get("title") != "Thinking...":
241
+ if thinking_started == True and data.get("messageId", "") != think_id:
242
+ thinking_started = False
243
+ chunk = {
244
+ "id": str(uuid.uuid4()),
245
+ "object": "chat.completion.chunk",
246
+ "created": int(uuid.uuid1().time_low),
247
+ "model": chat_request.model,
248
+ "choices": [{
249
+ "delta": {
250
+ "role": "assistant",
251
+ "content": "</think>" + data.get("segment", "")
252
+ },
253
+ "index": 0
254
+ }]
255
+ }
256
+ else:
257
+ chunk = {
258
+ "id": str(uuid.uuid4()),
259
+ "object": "chat.completion.chunk",
260
+ "created": int(uuid.uuid1().time_low),
261
+ "model": chat_request.model,
262
+ "choices": [{
263
+ "delta": {
264
+ "role": "assistant",
265
+ "content": data.get("segment", "")
266
+ },
267
+ "index": 0
268
+ }]
269
+ }
270
+ yield f"data: {json.dumps(chunk)}\n\n"
271
 
272
  if data.get("end"):
273
  # 发送结束标记