Files
life-echo/api/app/core/langchain_llm.py
2026-03-20 15:15:35 +08:00

19 lines
628 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
与 `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,
)