mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-03 17:21:59 +08:00
Fix: Chat completion generation parameter overrides (#15389)
### What problem does this PR solve? Closes #15388. Chat completion routes did not reliably honor per-request generation settings: - `/api/v1/chat/completions` copied generation settings with a truthiness check, so valid zero values such as `temperature: 0`, `top_p: 0`, `frequency_penalty: 0`, `presence_penalty: 0`, and `max_tokens: 0` were dropped. - `/api/v1/openai/{chat_id}/chat/completions` did not forward standard generation settings into the request-specific dialog LLM settings before calling `async_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 --check` - `python3 -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.py` - `uv 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.py` - `ZHIPU_AI_API_KEY=dummy uv run pytest test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py -q -k generation_params` ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
38
api/apps/restful_apis/_generation_params.py
Normal file
38
api/apps/restful_apis/_generation_params.py
Normal file
@@ -0,0 +1,38 @@
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
GENERATION_CONFIG_KEYS = ("temperature", "top_p", "frequency_penalty", "presence_penalty", "max_tokens")
|
||||
|
||||
|
||||
def extract_generation_config(req):
|
||||
return {key: req[key] for key in GENERATION_CONFIG_KEYS if key in req and req[key] is not None}
|
||||
|
||||
|
||||
def pop_generation_config(req):
|
||||
generation_config = extract_generation_config(req)
|
||||
for key in GENERATION_CONFIG_KEYS:
|
||||
req.pop(key, None)
|
||||
return generation_config
|
||||
|
||||
|
||||
def merge_generation_config(dialog, generation_config):
|
||||
if not generation_config:
|
||||
return
|
||||
llm_setting = deepcopy(getattr(dialog, "llm_setting", None) or {})
|
||||
llm_setting.update(generation_config)
|
||||
dialog.llm_setting = llm_setting
|
||||
@@ -26,6 +26,7 @@ from types import SimpleNamespace
|
||||
from quart import Response, request
|
||||
|
||||
from api.apps import current_user, login_required
|
||||
from api.apps.restful_apis._generation_params import merge_generation_config, pop_generation_config
|
||||
from api.db.joint_services.tenant_model_service import (
|
||||
get_tenant_default_model_by_type, get_model_config_from_provider_instance, get_api_key, split_model_name
|
||||
)
|
||||
@@ -1173,11 +1174,7 @@ async def session_completion(chat_id_in_arg=""):
|
||||
session_id = req.pop("session_id", "") or req.pop("conversation_id", "") or ""
|
||||
chat_model_id = req.pop("llm_id", "")
|
||||
|
||||
chat_model_config = {}
|
||||
for model_config in ["temperature", "top_p", "frequency_penalty", "presence_penalty", "max_tokens"]:
|
||||
config = req.get(model_config)
|
||||
if config:
|
||||
chat_model_config[model_config] = config
|
||||
chat_model_config = pop_generation_config(req)
|
||||
|
||||
try:
|
||||
conv = None
|
||||
@@ -1220,7 +1217,6 @@ async def session_completion(chat_id_in_arg=""):
|
||||
msg.append(m)
|
||||
else:
|
||||
dia = _build_default_completion_dialog()
|
||||
dia.llm_setting = chat_model_config
|
||||
|
||||
req.pop("messages", None)
|
||||
req.pop("question", None)
|
||||
@@ -1242,6 +1238,7 @@ async def session_completion(chat_id_in_arg=""):
|
||||
if not tenant_info or not tenant_info.llm_id:
|
||||
raise LookupError("No default chat model for tenant.")
|
||||
dia.llm_id = tenant_info.llm_id
|
||||
merge_generation_config(dia, chat_model_config)
|
||||
|
||||
stream_mode = req.pop("stream", True)
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import time
|
||||
from quart import Response, jsonify
|
||||
|
||||
from api.apps import current_user, login_required
|
||||
from api.apps.restful_apis._generation_params import extract_generation_config, merge_generation_config
|
||||
from api.db.services.dialog_service import DialogService, async_chat
|
||||
from api.db.services.doc_metadata_service import DocMetadataService
|
||||
from api.db.joint_services.tenant_model_service import get_model_config_from_provider_instance, get_api_key
|
||||
@@ -29,6 +30,7 @@ from common.metadata_utils import convert_conditions, meta_filter
|
||||
from common.token_utils import num_tokens_from_string
|
||||
from rag.prompts.generator import chunks_format
|
||||
|
||||
|
||||
def _validate_llm_id(llm_id, tenant_id, llm_setting=None):
|
||||
if not llm_id:
|
||||
return None
|
||||
@@ -278,6 +280,7 @@ async def openai_chat_completions(chat_id):
|
||||
dia.llm_id = requested_model
|
||||
if not get_api_key(tenant_id=dia.tenant_id, model_name=requested_model):
|
||||
return get_error_data_result(message=f"Cannot use specified model {requested_model}.")
|
||||
merge_generation_config(dia, extract_generation_config(req))
|
||||
|
||||
metadata_condition = extra_body.get("metadata_condition") or {}
|
||||
if metadata_condition and not isinstance(metadata_condition, dict):
|
||||
|
||||
Reference in New Issue
Block a user