Files
life-echo/api/app/core/langchain_llm.py

19 lines
628 B
Python
Raw Normal View History

2026-03-20 15:15:35 +08:00
"""
`get_llm_provider().langchain_llm` 配合使用的 LangChain Runnable 约定
langchain-openai 要求用顶层 `response_format` 绑定 JSON 模式禁止对 `.bind()` 传入
`model_kwargs={"response_format": ...}`会错误传入底层 `completions.create`
"""
from __future__ import annotations
from typing import Any
def bind_json_object_mode(llm: Any, *, max_tokens: int) -> Any:
"""返回绑定 `response_format=json_object` 与 `max_tokens` 的 Runnable通常为 ChatOpenAI"""
return llm.bind(
response_format={"type": "json_object"},
max_tokens=max_tokens,
)