mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-11 14:15:40 +08:00
Fix OpenAI agent stream chunk shape (#16402)
### What problem does this PR solve? Closes #8175. The Agent OpenAI-compatible streaming path uses `get_data_openai(..., stream=True)`, but that helper currently returns a minimal chunk shape. The main OpenAI-compatible chat endpoint already includes chunk metadata such as `created`, `system_fingerprint`, `usage`, `logprobs`, and assistant role/tool placeholders. This PR aligns the Agent stream helper with that existing OpenAI-compatible chunk shape while keeping the current `delta.content` behavior and existing reference injection path intact. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) - [ ] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe): ### Verification - `./.venv/bin/python -m pytest test/unit_test/api/utils/test_api_utils.py -q` - `python3 -m py_compile api/utils/api_utils.py test/unit_test/api/utils/test_api_utils.py` - `uvx ruff check api/utils/api_utils.py test/unit_test/api/utils/test_api_utils.py` --------- Co-authored-by: Harsh Kashyap <harshkashyap@Harshs-MacBook-Pro.local>
This commit is contained in:
@@ -428,15 +428,36 @@ def get_data_openai(id=None, created=None, model=None, prompt_tokens=0, completi
|
||||
total_tokens = prompt_tokens + completion_tokens
|
||||
|
||||
if stream:
|
||||
usage = None
|
||||
if finish_reason is not None:
|
||||
usage = {
|
||||
"prompt_tokens": prompt_tokens,
|
||||
"completion_tokens": completion_tokens,
|
||||
"total_tokens": total_tokens,
|
||||
"completion_tokens_details": {
|
||||
"reasoning_tokens": 0,
|
||||
"accepted_prediction_tokens": 0,
|
||||
"rejected_prediction_tokens": 0,
|
||||
},
|
||||
}
|
||||
return {
|
||||
"id": f"{id}",
|
||||
"object": "chat.completion.chunk",
|
||||
"created": created if created is not None else int(time.time()),
|
||||
"model": model,
|
||||
"system_fingerprint": "",
|
||||
"usage": usage,
|
||||
"choices": [
|
||||
{
|
||||
"delta": {"content": content},
|
||||
"delta": {
|
||||
"content": content,
|
||||
"role": "assistant",
|
||||
"function_call": None,
|
||||
"tool_calls": None,
|
||||
},
|
||||
"finish_reason": finish_reason,
|
||||
"index": 0,
|
||||
"logprobs": None,
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user