19 lines
628 B
Python
19 lines
628 B
Python
|
|
"""
|
|||
|
|
与 `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,
|
|||
|
|
)
|