Repository navigation
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds a generation-parameter helper module and uses it in chat and OpenAI completion endpoints to extract/pop and merge request-provided generation settings into dialog.llm_setting (preserving explicit zero values and merging into existing settings). Adds unit tests covering forwarding, zero preservation, and merge semantics. ChangesGeneration Parameter Preservation Across Completion APIs
Sequence Diagram(s)sequenceDiagram
participant Client
participant chat_api.session_completion
participant openai_api.openai_chat_completions
participant _generation_params.pop_generation_config
participant _generation_params.extract_generation_config
participant _generation_params.merge_generation_config
Client->>chat_api.session_completion: POST /api/v1/chat/completions (req)
chat_api.session_completion->>_generation_params.pop_generation_config: pop_generation_config(req)
chat_api.session_completion->>_generation_params.merge_generation_config: merge_generation_config(dia, config)
Client->>openai_api.openai_chat_completions: POST /api/v1/openai/{chat_id}/chat/completions (req)
openai_api.openai_chat_completions->>_generation_params.extract_generation_config: extract_generation_config(req)
openai_api.openai_chat_completions->>_generation_params.merge_generation_config: merge_generation_config(dia, config)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/apps/restful_apis/chat_api.py (1)
1209-1215:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winFix overwrite of
dia.llm_settingwhen overridingchat_model_id
chat_model_configis derived from_extract_generation_config(req)and is{}when the request doesn’t include any generation params. In thechat_model_idpath, the code doesdia.llm_setting = chat_model_config, wiping the existingdia.llm_setting; theelsepath preserves it because_merge_generation_config()early-returns whengeneration_configis falsy.♻️ Proposed fix
if chat_model_id: if not await thread_pool_exec(TenantLLMService.get_api_key, tenant_id=dia.tenant_id, model_name=chat_model_id): return get_data_error_result(message=f"Cannot use specified model {chat_model_id}.") dia.llm_id = chat_model_id - dia.llm_setting = chat_model_config - else: - _merge_generation_config(dia, chat_model_config) + _merge_generation_config(dia, chat_model_config)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/restful_apis/chat_api.py` around lines 1209 - 1215, When chat_model_id is provided the code unconditionally sets dia.llm_setting = chat_model_config which can wipe existing settings because chat_model_config (from _extract_generation_config(req)) may be {} — instead, only overwrite or merge when chat_model_config is truthy: check chat_model_config and if non-empty assign or merge into dia.llm_setting (use same merging behavior as _merge_generation_config), otherwise leave dia.llm_setting intact; update the chat_model_id branch that calls TenantLLMService.get_api_key and sets dia.llm_id to perform this conditional assignment/merge instead of a blind assignment.
🧹 Nitpick comments (1)
api/apps/restful_apis/openai_api.py (1)
33-45: ⚡ Quick winDuplicated generation-config helpers.
_GENERATION_CONFIG_KEYS,_extract_generation_config, and_merge_generation_configare byte-for-byte identical to the ones added inchat_api.py. Consider extracting them into a shared utility module to avoid divergence as the supported key list evolves.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/restful_apis/openai_api.py` around lines 33 - 45, The three helpers _GENERATION_CONFIG_KEYS, _extract_generation_config, and _merge_generation_config are duplicated; extract them into a single shared utility (e.g., a new module exposing _GENERATION_CONFIG_KEYS, extract_generation_config, merge_generation_config), replace the local copies in openai_api.py and chat_api.py to import the shared symbols, update references (rename callers if you change function names) and ensure deepcopy and getattr usage remains in merge_generation_config so dialog.llm_setting behavior is preserved.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@api/apps/restful_apis/openai_api.py`:
- Around line 45-47: Add a second blank line between the two top-level function
definitions _merge_generation_config and _validate_llm_id so there are two blank
lines separating them (satisfy E302/ruff); locate the function def for
_merge_generation_config and ensure exactly two blank lines before the line
starting with def _validate_llm_id(llm_id, tenant_id, llm_setting=None).
---
Outside diff comments:
In `@api/apps/restful_apis/chat_api.py`:
- Around line 1209-1215: When chat_model_id is provided the code unconditionally
sets dia.llm_setting = chat_model_config which can wipe existing settings
because chat_model_config (from _extract_generation_config(req)) may be {} —
instead, only overwrite or merge when chat_model_config is truthy: check
chat_model_config and if non-empty assign or merge into dia.llm_setting (use
same merging behavior as _merge_generation_config), otherwise leave
dia.llm_setting intact; update the chat_model_id branch that calls
TenantLLMService.get_api_key and sets dia.llm_id to perform this conditional
assignment/merge instead of a blind assignment.
---
Nitpick comments:
In `@api/apps/restful_apis/openai_api.py`:
- Around line 33-45: The three helpers _GENERATION_CONFIG_KEYS,
_extract_generation_config, and _merge_generation_config are duplicated; extract
them into a single shared utility (e.g., a new module exposing
_GENERATION_CONFIG_KEYS, extract_generation_config, merge_generation_config),
replace the local copies in openai_api.py and chat_api.py to import the shared
symbols, update references (rename callers if you change function names) and
ensure deepcopy and getattr usage remains in merge_generation_config so
dialog.llm_setting behavior is preserved.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: bf570d80-d74a-4789-8c2d-ebb12fa1d3b3
📒 Files selected for processing (3)
api/apps/restful_apis/chat_api.pyapi/apps/restful_apis/openai_api.pytest/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
d9b8962 to
31503c4
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/apps/restful_apis/chat_api.py (1)
1197-1203:⚠️ Potential issue | 🟠 Major | ⚡ Quick winFix
dia.llm_settingoverwrite whenchat_model_idis set.When
chat_model_idis provided, the code assignsdia.llm_setting = chat_model_configinstead of merging. Sincechat_model_configcomes fromextract_generation_config(req)(only generation keys liketemperature,top_p,max_tokens), any request that only switchesllm_id(no generation params) yields{}and overwrites the dialog’s existing generation settings. Theelsebranch correctly preserves existing settings viamerge_generation_config(dia, chat_model_config)(which returns early on empty config). Prefer merging here as well so dialog generation behavior doesn’t change unexpectedly.🐛 Proposed fix
dia.llm_id = chat_model_id - dia.llm_setting = chat_model_config + merge_generation_config(dia, chat_model_config) else: merge_generation_config(dia, chat_model_config)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/restful_apis/chat_api.py` around lines 1197 - 1203, When chat_model_id is set the handler currently does dia.llm_setting = chat_model_config which can clobber existing generation settings with an empty config; change it to merge the incoming generation config into the dialog using merge_generation_config(dia, chat_model_config) instead of direct assignment while still setting dia.llm_id = chat_model_id and keeping the pre-check via TenantLLMService.get_api_key; this ensures existing dia.llm_setting is preserved when chat_model_config is empty and respects merge_generation_config's early-return behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@api/apps/restful_apis/chat_api.py`:
- Line 1144: The extracted generation settings returned by
extract_generation_config(req) are left in req and get forwarded into
dialog_service.async_chat via **req; remove the consumed keys (temperature,
top_p, frequency_penalty, presence_penalty, max_tokens) from req after calling
extract_generation_config and before calling async_chat so they are not leaked
into downstream kwargs—i.e., call extract_generation_config(req) to get
chat_model_config, then pop or del those specific keys from req, and then pass
chat_model_config plus the sanitized req into dialog_service.async_chat.
---
Outside diff comments:
In `@api/apps/restful_apis/chat_api.py`:
- Around line 1197-1203: When chat_model_id is set the handler currently does
dia.llm_setting = chat_model_config which can clobber existing generation
settings with an empty config; change it to merge the incoming generation config
into the dialog using merge_generation_config(dia, chat_model_config) instead of
direct assignment while still setting dia.llm_id = chat_model_id and keeping the
pre-check via TenantLLMService.get_api_key; this ensures existing
dia.llm_setting is preserved when chat_model_config is empty and respects
merge_generation_config's early-return behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6f4f284a-470e-4f58-b211-d3b3478a5655
📒 Files selected for processing (4)
api/apps/restful_apis/_generation_params.pyapi/apps/restful_apis/chat_api.pyapi/apps/restful_apis/openai_api.pytest/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
🚧 Files skipped from review as they are similar to previous changes (1)
- test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
3b24817 to
7fd583e
Compare
27ede91 to
af96847
Compare
|
Hi @wangq8, @JinHai-CN, @KevinHuSh, @yingfeng, @xugangqiang, would you please review my PR and give me your feedback? |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15389 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
LGTM |
What problem does this PR solve?
Closes #15388.
Chat completion routes did not reliably honor per-request generation settings:
/api/v1/chat/completionscopied generation settings with a truthiness check, so valid zero values such astemperature: 0,top_p: 0,frequency_penalty: 0,presence_penalty: 0, andmax_tokens: 0were dropped./api/v1/openai/{chat_id}/chat/completionsdid not forward standard generation settings into the request-specific dialog LLM settings before callingasync_chat.This PR preserves explicitly supplied generation parameters, including zero values, and merges request-level overrides into existing dialog settings where appropriate.
The supported generation parameter keys and merge behavior live in a shared REST API helper to keep both completion routes aligned.
Validation:
git diff --checkpython3 -m py_compile api/apps/restful_apis/_generation_params.py api/apps/restful_apis/chat_api.py api/apps/restful_apis/openai_api.py test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.pyuv run ruff check api/apps/restful_apis/_generation_params.py api/apps/restful_apis/chat_api.py api/apps/restful_apis/openai_api.py test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.pyZHIPU_AI_API_KEY=dummy uv run pytest test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py -q -k generation_paramsType of change