Skip to content

支持流式 Chat Completions 输出 usage chunk#20

Closed
Zker67 wants to merge 1 commit into
zqbxdev:devfrom
Zker67:fix-chat-stream-usage
Closed

支持流式 Chat Completions 输出 usage chunk#20
Zker67 wants to merge 1 commit into
zqbxdev:devfrom
Zker67:fix-chat-stream-usage

Conversation

@Zker67
Copy link
Copy Markdown
Contributor

@Zker67 Zker67 commented May 25, 2026

改动

  • 支持 stream_options.include_usage=true 的 Chat Completions 流式 usage 输出。
  • 普通文本、Grok Console、Grok app-chat 流式路径结束前都会额外发送 choices: [] 且包含 usage 的 chunk。
  • 普通内容 chunk 在开启该选项时补充 usage: null
  • 复用现有 token 计数函数,统计 prompt_tokenscompletion_tokenstotal_tokens
  • 增加三条回归测试,覆盖普通文本、Grok Console 和 Grok app-chat 流式路径。

验证

  • python -m py_compile services/protocol/openai_v1_chat_complete.py test/test_grok_provider.py
  • python -m unittest test.test_grok_provider.GrokProviderTests.test_streaming_grok_chat_completion_returns_openai_chunks test.test_grok_provider.GrokProviderTests.test_streaming_grok_console_completion_emits_reasoning_content test.test_grok_provider.GrokProviderTests.test_streaming_grok_console_completion_includes_usage_when_requested test.test_grok_provider.GrokProviderTests.test_streaming_grok_app_chat_completion_emits_reasoning_content test.test_grok_provider.GrokProviderTests.test_streaming_grok_app_chat_completion_includes_usage_when_requested test.test_grok_provider.GrokProviderTests.test_streaming_text_completion_includes_usage_when_requested

关联 #19

@github-actions
Copy link
Copy Markdown

AI PR Review Refused

This PR targets main, but AI PR Review only runs for PRs targeting dev.
main is the stable release branch, and direct PRs to main are not accepted.
Please change the PR base branch to dev, then run AI PR Review again.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI PR Review

Decision: comment
Confidence: 0.91

Summary

整体实现方向正确:已在三条流式路径中按 stream_options.include_usage=true 增加 usage: null 和末尾 choices: [] + usage chunk,并补充了对应回归测试。未发现会直接破坏主流程的明显问题,但有一处 usage 统计边界条件建议修正。

Findings

  1. [minor] services/protocol/openai_v1_chat_complete.py:79 - completion_usage()completion_tokens / reasoning_tokens 的计算被 if messages else 0 保护,导致当 messages 为空但仍有输出内容时会错误返回 0。是否统计 completion token 不应依赖 messages 是否为空,建议改为基于 content / reasoning_content 本身判断。

Recent Comment Summary

功能与测试覆盖基本到位;建议修正 completion_usage() 中 completion/reasoning token 统计对 messages 的不必要依赖,以避免空消息场景下 usage 统计失真。

@Zker67 Zker67 force-pushed the fix-chat-stream-usage branch from 1506abc to 36313c2 Compare May 25, 2026 01:48
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI PR Review

Decision: comment
Confidence: 0.88

Summary

整体改动方向正确,已在三条流式路径(普通文本、Grok Console、Grok app-chat)统一实现 stream_options.include_usage=true 时的 usage 追加 chunk,并补充了对应回归测试。未发现明确阻塞性问题。

Findings

No specific findings were returned.

Recent Comment Summary

实现与测试覆盖基本完整,行为与预期一致;建议后续可再补充一个 messages=[] 场景下 usage 统计的边界测试以确认 token 计数策略。

@Zker67
Copy link
Copy Markdown
Contributor Author

Zker67 commented May 25, 2026

这个 PR 初次提交时误选了 main,触发了上游的 wrong-target-branch 标签;当前账号无法移除该标签。后续改用新的 dev 目标 PR。

@Zker67 Zker67 closed this May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant