feat(llm): add json_output support for structured LLM responses

This commit is contained in:
Timebomb2018
2026-04-16 16:27:55 +08:00
parent 5ce0bdb0f5
commit 8c6b65db12
23 changed files with 304 additions and 110 deletions

View File

@@ -116,6 +116,11 @@ class LLMNodeConfig(BaseNodeConfig):
description="Top-p 采样参数"
)
json_output: bool = Field(
default=False,
description="是否以 JSON 格式输出"
)
frequency_penalty: float | None = Field(
default=None,
ge=-2.0,

View File

@@ -22,6 +22,7 @@ from app.db import get_db_context
from app.models import ModelType
from app.schemas.model_schema import ModelInfo
from app.services.model_service import ModelConfigService
from app.models.models_model import ModelProvider
logger = logging.getLogger(__name__)
@@ -126,7 +127,11 @@ class LLMNode(BaseNode):
# 4. 创建 LLM 实例(使用已提取的数据)
# 注意:对于流式输出,需要在模型初始化时设置 streaming=True
extra_params = {"streaming": stream} if stream else {}
extra_params: dict[str, Any] = {"streaming": stream} if stream else {}
if self.typed_config.temperature is not None:
extra_params["temperature"] = self.typed_config.temperature
if self.typed_config.max_tokens is not None:
extra_params["max_tokens"] = self.typed_config.max_tokens
llm = RedBearLLM(
RedBearModelConfig(
@@ -135,7 +140,9 @@ class LLMNode(BaseNode):
api_key=model_info.api_key,
base_url=model_info.api_base,
extra_params=extra_params,
is_omni=model_info.is_omni
is_omni=model_info.is_omni,
capability=model_info.capability,
json_output=self.typed_config.json_output,
),
type=model_info.model_type
)
@@ -218,6 +225,16 @@ class LLMNode(BaseNode):
rendered = self._render_template(prompt_template, variable_pool)
self.messages = [{"role": "user", "content": rendered}]
# ChatTongyi 要求 messages 含 'json' 字样才能使用 response_format在 system prompt 中注入
if (self.typed_config.json_output
and model_info.provider.lower() == ModelProvider.DASHSCOPE
and not model_info.is_omni):
system_msg = next((m for m in self.messages if m["role"] == "system"), None)
if system_msg:
system_msg["content"] += "\n请以JSON格式输出。"
else:
self.messages.insert(0, {"role": "system", "content": "请以JSON格式输出。"})
return llm
async def execute(self, state: WorkflowState, variable_pool: VariablePool) -> AIMessage: