Update main.py
Browse files
main.py
CHANGED
@@ -191,24 +191,60 @@ async def chat_completions(
|
|
191 |
# 在处理消息时先判断模型类型
|
192 |
if data.get('model') == 'AkashGen' and "<image_generation>" in msg_data:
|
193 |
# 图片生成模型的特殊处理
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
content_buffer += msg_data
|
214 |
|
@@ -230,7 +266,7 @@ async def chat_completions(
|
|
230 |
"id": f"chatcmpl-{chat_id}",
|
231 |
"object": "chat.completion.chunk",
|
232 |
"created": int(time.time()),
|
233 |
-
"model": data.get('model'),
|
234 |
"choices": [{
|
235 |
"delta": {},
|
236 |
"index": 0,
|
|
|
191 |
# 在处理消息时先判断模型类型
|
192 |
if data.get('model') == 'AkashGen' and "<image_generation>" in msg_data:
|
193 |
# 图片生成模型的特殊处理
|
194 |
+
match = re.search(r"jobId='([^']+)' prompt='([^']+)' negative='([^']*)'", msg_data)
|
195 |
+
if match:
|
196 |
+
job_id, prompt, negative = match.groups()
|
197 |
+
print(f"Starting image generation process for job_id: {job_id}")
|
198 |
+
|
199 |
+
# 立即发送思考开始的消息
|
200 |
+
start_time = time.time()
|
201 |
+
think_msg = "<think>\n"
|
202 |
+
think_msg += "🎨 Generating image...\n\n"
|
203 |
+
think_msg += f"Prompt: {prompt}\n"
|
204 |
+
|
205 |
+
# 发送思考开始消息 (使用标准 OpenAI 格式)
|
206 |
+
chunk = {
|
207 |
+
"id": f"chatcmpl-{chat_id}",
|
208 |
+
"object": "chat.completion.chunk",
|
209 |
+
"created": int(time.time()),
|
210 |
+
"model": data.get('model'),
|
211 |
+
"choices": [{
|
212 |
+
"delta": {"content": think_msg},
|
213 |
+
"index": 0,
|
214 |
+
"finish_reason": None
|
215 |
+
}]
|
216 |
+
}
|
217 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
218 |
+
|
219 |
+
# 检查图片状态和上传
|
220 |
+
result = await upload_to_xinyew(
|
221 |
+
await check_image_status(session, job_id, headers),
|
222 |
+
job_id
|
223 |
+
)
|
224 |
+
|
225 |
+
# 发送结束消息
|
226 |
+
elapsed_time = time.time() - start_time
|
227 |
+
end_msg = f"\n🤔 Thinking for {elapsed_time:.1f}s...\n"
|
228 |
+
end_msg += "</think>\n\n"
|
229 |
+
if result:
|
230 |
+
end_msg += f""
|
231 |
+
else:
|
232 |
+
end_msg += "*Image generation or upload failed.*\n"
|
233 |
+
|
234 |
+
# 发送结束消息 (使用标准 OpenAI 格式)
|
235 |
+
chunk = {
|
236 |
+
"id": f"chatcmpl-{chat_id}",
|
237 |
+
"object": "chat.completion.chunk",
|
238 |
+
"created": int(time.time()),
|
239 |
+
"model": data.get('model'),
|
240 |
+
"choices": [{
|
241 |
+
"delta": {"content": end_msg},
|
242 |
+
"index": 0,
|
243 |
+
"finish_reason": None
|
244 |
+
}]
|
245 |
+
}
|
246 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
247 |
+
continue
|
248 |
|
249 |
content_buffer += msg_data
|
250 |
|
|
|
266 |
"id": f"chatcmpl-{chat_id}",
|
267 |
"object": "chat.completion.chunk",
|
268 |
"created": int(time.time()),
|
269 |
+
"model": data.get('model'),
|
270 |
"choices": [{
|
271 |
"delta": {},
|
272 |
"index": 0,
|