| 333 | f.write(f"Tool choice: {tool_choice}\n") |
| 334 | |
| 335 | def generate(): |
| 336 | if not messages: |
| 337 | yield "event: response.completed\n" |
| 338 | yield ( |
| 339 | "data: " |
| 340 | + json.dumps({ |
| 341 | "type": "response.completed", |
| 342 | "response": { |
| 343 | "id": response_id, "object": "response", |
| 344 | "status": "completed", "model": effective_model, |
| 345 | "output": [], "usage": {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}, |
| 346 | }, |
| 347 | }, ensure_ascii=False) |
| 348 | + "\n\n" |
| 349 | ) |
| 350 | return |
| 351 | |
| 352 | # response.created |
| 353 | yield "event: response.created\n" |
| 354 | yield ( |
| 355 | "data: " |
| 356 | + json.dumps({ |
| 357 | "type": "response.created", |
| 358 | "response": { |
| 359 | "id": response_id, "object": "response", |
| 360 | "status": "in_progress", "model": effective_model, |
| 361 | "output": [], "usage": None, |
| 362 | }, |
| 363 | }, ensure_ascii=False) |
| 364 | + "\n\n" |
| 365 | ) |
| 366 | |
| 367 | # response.in_progress |
| 368 | yield "event: response.in_progress\n" |
| 369 | yield ( |
| 370 | "data: " |
| 371 | + json.dumps({ |
| 372 | "type": "response.in_progress", |
| 373 | "response": { |
| 374 | "id": response_id, "object": "response", |
| 375 | "status": "in_progress", "model": effective_model, |
| 376 | "output": [], "usage": None, |
| 377 | }, |
| 378 | }, ensure_ascii=False) |
| 379 | + "\n\n" |
| 380 | ) |
| 381 | |
| 382 | # 构建 DeepSeek 请求 |
| 383 | headers = { |
| 384 | "Authorization": f"Bearer {DEEPSEEK_API_KEY}", |
| 385 | "Content-Type": "application/json", |
| 386 | } |
| 387 | payload = { |
| 388 | "model": effective_model, |
| 389 | "messages": messages, |
| 390 | "stream": True, |
| 391 | "stream_options": {"include_usage": True}, |
| 392 | "thinking": {"type": "disabled"}, |