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

@@ -41,6 +41,7 @@ class LangChainAgent:
max_tool_consecutive_calls: int = 3, # 单个工具最大连续调用次数
deep_thinking: bool = False, # 是否启用深度思考模式
thinking_budget_tokens: Optional[int] = None, # 深度思考 token 预算
json_output: bool = False, # 是否强制 JSON 输出
capability: Optional[List[str]] = None # 模型能力列表,用于校验是否支持深度思考
):
"""初始化 LangChain Agent
@@ -64,7 +65,6 @@ class LangChainAgent:
self.streaming = streaming
self.is_omni = is_omni
self.max_tool_consecutive_calls = max_tool_consecutive_calls
self.deep_thinking = deep_thinking and ("thinking" in (capability or []))
# 工具调用计数器:记录每个工具的连续调用次数
self.tool_call_counter: Dict[str, int] = {}
@@ -80,6 +80,12 @@ class LangChainAgent:
self.system_prompt = system_prompt or "你是一个专业的AI助手"
# ChatTongyi 要求 messages 含 'json' 字样才能使用 response_format
# 在 system prompt 中注入 JSON 要求
from app.models.models_model import ModelProvider
if json_output and provider.lower() == ModelProvider.DASHSCOPE and not is_omni:
self.system_prompt += "\n请以JSON格式输出。"
logger.debug(
f"Agent 迭代次数配置: max_iterations={self.max_iterations}, "
f"tool_count={len(self.tools)}, "
@@ -87,23 +93,17 @@ class LangChainAgent:
f"auto_calculated={max_iterations is None}"
)
# 根据 capability 校验是否真正支持深度思考
actual_deep_thinking = self.deep_thinking
if deep_thinking and not actual_deep_thinking:
logger.warning(
f"模型 {model_name} 不支持深度思考capability 中无 'thinking'),已自动关闭 deep_thinking"
)
# 创建 RedBearLLM支持多提供商
# 创建 RedBearLLMcapability 校验由 RedBearModelConfig 统一处理
model_config = RedBearModelConfig(
model_name=model_name,
provider=provider,
api_key=api_key,
base_url=api_base,
is_omni=is_omni,
deep_thinking=actual_deep_thinking,
thinking_budget_tokens=thinking_budget_tokens if actual_deep_thinking else None,
support_thinking="thinking" in (capability or []),
capability=capability,
deep_thinking=deep_thinking,
thinking_budget_tokens=thinking_budget_tokens,
json_output=json_output,
extra_params={
"temperature": temperature,
"max_tokens": max_tokens,
@@ -112,6 +112,9 @@ class LangChainAgent:
)
self.llm = RedBearLLM(model_config, type=ModelType.CHAT)
# 从经过校验的 config 读取实际生效的能力开关
self.deep_thinking = model_config.deep_thinking
self.json_output = model_config.json_output
# 获取底层模型用于真正的流式调用
self._underlying_llm = self.llm._model if hasattr(self.llm, '_model') else self.llm

View File

@@ -1,7 +1,7 @@
from __future__ import annotations
import os
from typing import Any, Dict, Optional, TypeVar
from typing import Any, Dict, List, Optional, TypeVar
from langchain_aws import ChatBedrock
from langchain_community.chat_models import ChatTongyi
@@ -9,7 +9,7 @@ from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseLLM
from langchain_ollama import OllamaLLM
from langchain_openai import ChatOpenAI, OpenAI
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, model_validator
from app.core.error_codes import BizCode
from app.core.exceptions import BusinessException
@@ -25,10 +25,11 @@ class RedBearModelConfig(BaseModel):
provider: str
api_key: str
base_url: Optional[str] = None
capability: List[str] = Field(default_factory=list) # 模型能力列表,驱动所有能力开关
is_omni: bool = False # 是否为 Omni 模型
deep_thinking: bool = False # 是否启用深度思考模式
thinking_budget_tokens: Optional[int] = None # 深度思考 token 预算
support_thinking: bool = False # 模型是否支持 enable_thinking 参数capability 含 thinking
json_output: bool = False # 是否强制 JSON 输出
# 请求超时时间(秒)- 默认120秒以支持复杂的LLM调用可通过环境变量 LLM_TIMEOUT 配置
timeout: float = Field(default_factory=lambda: float(os.getenv("LLM_TIMEOUT", "120.0")))
# 最大重试次数 - 默认2次以避免过长等待可通过环境变量 LLM_MAX_RETRIES 配置
@@ -36,6 +37,29 @@ class RedBearModelConfig(BaseModel):
concurrency: int = 5 # 并发限流
extra_params: Dict[str, Any] = {}
@model_validator(mode="after")
def _resolve_capabilities(self) -> "RedBearModelConfig":
from app.core.logging_config import get_business_logger
logger = get_business_logger()
if self.deep_thinking and "thinking" not in self.capability:
logger.warning(
f"模型 {self.model_name} 不支持深度思考capability 中无 'thinking'),已自动关闭 deep_thinking"
)
self.deep_thinking = False
self.thinking_budget_tokens = None
if self.json_output and "json_output" not in self.capability:
logger.warning(
f"模型 {self.model_name} 不支持 JSON 输出capability 中无 'json_output'),已自动关闭 json_output"
)
self.json_output = False
if self.json_output and self.deep_thinking:
logger.warning(
f"模型 {self.model_name} json_output 与 deep_thinking 互斥,已自动关闭 deep_thinking"
)
self.deep_thinking = False
self.thinking_budget_tokens = None
return self
class RedBearModelFactory:
"""模型工厂类"""
@@ -74,17 +98,20 @@ class RedBearModelFactory:
is_streaming = bool(config.extra_params.get("streaming"))
if is_streaming:
params["stream_usage"] = True
# 只有支持 thinking 的模型传 enable_thinking
if config.support_thinking:
model_kwargs: Dict[str, Any] = config.extra_params.get("model_kwargs", {})
if is_streaming:
model_kwargs["enable_thinking"] = config.deep_thinking
if config.deep_thinking:
model_kwargs["incremental_output"] = True
if config.thinking_budget_tokens:
model_kwargs["thinking_budget"] = config.thinking_budget_tokens
else:
model_kwargs["enable_thinking"] = False
# 支持 thinking 的模型始终传 enable_thinking,关闭时显式传 False 避免模型默认开启思考
if "thinking" in config.capability:
extra_body = params.setdefault("extra_body", {})
if config.deep_thinking:
extra_body["enable_thinking"] = False
if is_streaming:
extra_body["enable_thinking"] = True
if config.thinking_budget_tokens:
extra_body["thinking_budget"] = config.thinking_budget_tokens
params["extra_body"] = extra_body
# JSON 输出模式
if config.json_output:
model_kwargs = params.setdefault("model_kwargs", {})
model_kwargs["response_format"] = {"type": "json_object"}
params["model_kwargs"] = model_kwargs
return params
@@ -108,27 +135,30 @@ class RedBearModelFactory:
**config.extra_params
}
# 流式模式下启用 stream_usage 以获取 token 统计
if config.extra_params.get("streaming"):
params["stream_usage"] = True
# 深度思考模式
is_streaming = bool(config.extra_params.get("streaming"))
if config.support_thinking:
if is_streaming and not config.is_omni:
if provider == ModelProvider.VOLCANO:
# 火山引擎深度思考仅流式调用支持,非流式时不传 thinking 参数
thinking_config: Dict[str, Any] = {
"type": "enabled" if config.deep_thinking else "disabled"
}
if config.deep_thinking and config.thinking_budget_tokens:
thinking_config["budget_tokens"] = config.thinking_budget_tokens
params["extra_body"] = {"thinking": thinking_config}
else:
# 始终显式传递 enable_thinking不支持该参数的模型如 DeepSeek-R1会直接忽略
model_kwargs: Dict[str, Any] = config.extra_params.get("model_kwargs", {})
model_kwargs["enable_thinking"] = config.deep_thinking
if config.deep_thinking and config.thinking_budget_tokens:
model_kwargs["thinking_budget"] = config.thinking_budget_tokens
params["model_kwargs"] = model_kwargs
if is_streaming:
params["stream_usage"] = True
# 支持 thinking 的模型始终传 enable_thinking关闭时显式传 False 避免模型默认开启思考
if "thinking" in config.capability:
# VOLCANO 深度思考仅流式支持
if provider == ModelProvider.VOLCANO:
thinking_config: Dict[str, Any] = {"type": "enabled" if config.deep_thinking else "disabled"}
if config.deep_thinking and config.thinking_budget_tokens:
thinking_config["budget_tokens"] = config.thinking_budget_tokens
params["extra_body"] = {"thinking": thinking_config}
else:
extra_body = params.setdefault("extra_body", {})
if config.deep_thinking:
extra_body["enable_thinking"] = False
if is_streaming:
extra_body["enable_thinking"] = True
if config.thinking_budget_tokens:
extra_body["thinking_budget"] = config.thinking_budget_tokens
params["extra_body"] = extra_body
# JSON 输出模式
if config.json_output:
params.setdefault("model_kwargs", {})
params["model_kwargs"]["response_format"] = {"type": "json_object"}
return params
elif provider == ModelProvider.DASHSCOPE:
params = {
@@ -137,18 +167,21 @@ class RedBearModelFactory:
"max_retries": config.max_retries,
**config.extra_params
}
# 只有支持 thinking 的模型传 enable_thinking
if config.support_thinking:
# 支持 thinking 的模型始终传 enable_thinking,关闭时显式传 False 避免模型默认开启思考
if "thinking" in config.capability:
is_streaming = bool(config.extra_params.get("streaming"))
model_kwargs: Dict[str, Any] = config.extra_params.get("model_kwargs", {})
if is_streaming:
model_kwargs["enable_thinking"] = config.deep_thinking
if config.deep_thinking:
model_kwargs["incremental_output"] = True
if config.thinking_budget_tokens:
model_kwargs["thinking_budget"] = config.thinking_budget_tokens
else:
model_kwargs = params.setdefault("model_kwargs", {})
if config.deep_thinking:
model_kwargs["enable_thinking"] = False
if is_streaming:
model_kwargs["enable_thinking"] = True
model_kwargs["incremental_output"] = True
if config.thinking_budget_tokens:
model_kwargs["thinking_budget"] = config.thinking_budget_tokens
params["model_kwargs"] = model_kwargs
if config.json_output:
model_kwargs = params.setdefault("model_kwargs", {})
model_kwargs["response_format"] = {"type": "json_object"}
params["model_kwargs"] = model_kwargs
return params
elif provider == ModelProvider.BEDROCK:
@@ -196,6 +229,10 @@ class RedBearModelFactory:
params["additional_model_request_fields"] = {
"thinking": {"type": "enabled", "budget_tokens": budget}
}
# JSON 输出模式
if config.json_output:
params.setdefault("model_kwargs", {})
params["model_kwargs"]["response_format"] = {"type": "json_object"}
return params
else:
raise BusinessException(f"不支持的提供商: {provider}", code=BizCode.PROVIDER_NOT_SUPPORTED)

View File

@@ -6,7 +6,8 @@ models:
description: AI21 Labs大语言模型completion生成模式256000上下文窗口
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -20,6 +21,7 @@ models:
is_official: true
capability:
- vision
- json_output
is_omni: false
tags:
- 大语言模型
@@ -38,6 +40,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -54,7 +57,8 @@ models:
description: Cohere大语言模型支持智能体思考、工具调用、流式工具调用128000上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -72,6 +76,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -87,7 +92,8 @@ models:
description: Meta Llama大语言模型支持智能体思考、工具调用128000上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -101,7 +107,8 @@ models:
description: Mistral AI大语言模型支持智能体思考、工具调用32000上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -115,7 +122,8 @@ models:
description: OpenAI大语言模型支持智能体思考、工具调用、流式工具调用32768上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -130,7 +138,8 @@ models:
description: Qwen大语言模型支持智能体思考、工具调用、流式工具调用32768上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型

View File

@@ -8,6 +8,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -22,6 +23,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -36,6 +38,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -48,7 +51,8 @@ models:
description: DeepSeek-V3.1大语言模型支持智能体思考131072超大上下文窗口对话模式支持丰富生成参数调节
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -61,7 +65,8 @@ models:
description: DeepSeek-V3.2-exp实验版大语言模型支持智能体思考131072超大上下文窗口对话模式支持丰富生成参数调节
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -74,7 +79,8 @@ models:
description: DeepSeek-V3.2大语言模型支持智能体思考131072超大上下文窗口对话模式支持丰富生成参数调节
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -87,7 +93,8 @@ models:
description: DeepSeek-V3大语言模型支持智能体思考64000上下文窗口对话模式支持文本与JSON格式输出
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -100,7 +107,8 @@ models:
description: farui-plus大语言模型支持多工具调用、智能体思考、流式工具调用12288上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -115,7 +123,8 @@ models:
description: GLM-4.7大语言模型支持多工具调用、智能体思考、流式工具调用202752超大上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -133,6 +142,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -150,6 +160,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -180,6 +191,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -210,7 +222,7 @@ models:
is_deprecated: false
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -376,6 +388,7 @@ models:
capability:
- vision
- video
- json_output
is_omni: false
tags:
- 大语言模型
@@ -448,6 +461,7 @@ models:
capability:
- vision
- video
- qwen-vl-plus-latest
is_omni: false
tags:
- 大语言模型
@@ -466,6 +480,7 @@ models:
capability:
- vision
- video
- qwen-vl-plus-latest
is_omni: false
tags:
- 大语言模型
@@ -481,7 +496,8 @@ models:
description: qwen2.5-0.5b-instruct大语言模型支持多工具调用、智能体思考、流式工具调用32768上下文窗口对话模式未废弃
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -498,6 +514,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -513,7 +530,7 @@ models:
is_deprecated: false
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -530,6 +547,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -546,6 +564,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -561,7 +580,7 @@ models:
is_deprecated: false
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -578,6 +597,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -594,6 +614,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -610,6 +631,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -626,6 +648,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -641,7 +664,7 @@ models:
is_deprecated: false
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -656,7 +679,7 @@ models:
is_deprecated: false
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -672,6 +695,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -687,6 +711,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -702,6 +727,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -719,6 +745,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -736,6 +763,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -752,6 +780,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -768,7 +797,7 @@ models:
is_deprecated: false
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -785,6 +814,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -803,6 +833,8 @@ models:
- vision
- video
- audio
- thinking
- json_output
is_omni: true
tags:
- 大语言模型
@@ -822,7 +854,7 @@ models:
capability:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -844,6 +876,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -864,7 +897,7 @@ models:
capability:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -886,6 +919,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -907,6 +941,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -928,6 +963,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -947,6 +983,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -964,6 +1001,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -979,6 +1017,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -994,6 +1033,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型

View File

@@ -10,6 +10,7 @@ models:
- vision
- audio
- video
- json_output
is_omni: true
tags:
- 大语言模型
@@ -27,7 +28,8 @@ models:
description: gpt-3.5-turbo-0125大语言模型支持多工具调用、智能体思考、流式工具调用16385上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -42,7 +44,8 @@ models:
description: gpt-3.5-turbo-1106大语言模型支持多工具调用、智能体思考、流式工具调用16385上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -57,7 +60,8 @@ models:
description: gpt-3.5-turbo-16k大语言模型支持多工具调用、智能体思考、流式工具调用16385上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -84,7 +88,8 @@ models:
description: gpt-3.5-turbo大语言模型支持多工具调用、智能体思考、流式工具调用16385上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -99,7 +104,8 @@ models:
description: gpt-4-0125-preview大语言模型支持多工具调用、智能体思考、流式工具调用128000上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -114,7 +120,8 @@ models:
description: gpt-4-1106-preview大语言模型支持多工具调用、智能体思考、流式工具调用128000上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -131,6 +138,7 @@ models:
is_official: true
capability:
- vision
- json_output
is_omni: false
tags:
- 大语言模型
@@ -146,7 +154,8 @@ models:
description: gpt-4-turbo-preview大语言模型支持多工具调用、智能体思考、流式工具调用128000上下文窗口对话模式
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -163,6 +172,7 @@ models:
is_official: true
capability:
- vision
- json_output
is_omni: false
tags:
- 大语言模型
@@ -194,6 +204,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -213,6 +224,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -231,6 +243,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -248,6 +261,7 @@ models:
is_official: true
capability:
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -266,6 +280,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -284,6 +299,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -302,6 +318,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -321,6 +338,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -340,6 +358,7 @@ models:
capability:
- vision
- thinking
- json_output
is_omni: false
tags:
- 大语言模型

View File

@@ -11,6 +11,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -26,6 +27,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -41,6 +43,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -56,6 +59,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -72,6 +76,7 @@ models:
capability:
- vision
- video
- json_output
is_omni: false
tags:
- 大语言模型
@@ -87,6 +92,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -102,6 +108,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -117,6 +124,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -132,6 +140,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -148,6 +157,7 @@ models:
- vision
- video
- thinking
- json_output
is_omni: false
tags:
- 大语言模型
@@ -175,7 +185,8 @@ models:
description: 全新一代主力模型,性能全面升级,在知识、代码、推理等方面表现卓越。最大支持 128k 上下文窗口,输出长度支持最大 12k tokens。
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型
@@ -187,7 +198,8 @@ models:
description: 全新一代轻量版模型,极致响应速度,效果与时延均达到全球一流水平。支持 32k 上下文窗口,输出长度支持最大 12k tokens。
is_deprecated: false
is_official: true
capability: []
capability:
- json_output
is_omni: false
tags:
- 大语言模型

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: