langchain 在Runnable上调用bind_tools后,可配置字段不可用,

cdmah0mi  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(33)

检查其他资源

  • 我为这个问题添加了一个非常描述性的标题。
  • 我在集成搜索中搜索了 LangGraph /LangChain 文档。
  • 我使用 GitHub 搜索找到了一个类似的问题,但没有找到。
  • 我确信这是 LangGraph/LangChain 中的一个 bug,而不是我的代码。
  • 我确信这是一个更好的问题 rather than a GitHub discussion ,因为这是一个 LangGraph bug 而不是设计问题。

示例代码

from langchain_openai import ChatOpenAI
from langchain_core.runnables import ConfigurableField
from langchain_core.pydantic_v1 import BaseModel
import os

os.environ["OPENAI_API_KEY"] = "..."

class Add(BaseModel):
    """Add two numbers"""

    a: int
    b: int

configurable_temperature = ConfigurableField(
    id="temperature",
    name="Temperature",
    description="The temperature of the model",
)

tools = [Add]

model = ChatOpenAI(temperature=0).configurable_fields(
    temperature=configurable_temperature
)

print("Model without Tools")
print("Config Specs - ", model.config_specs)
print("Config Schema Json - ", model.config_schema(include=["temperature"]).schema_json())

print("\n\nModel with Tools")
model_with_tools = model.bind_tools(tools)
print("Config Specs - ", model_with_tools.config_specs)
print("Config Schema Json - ", model_with_tools.config_schema(include=["temperature"]).schema_json())

错误信息和堆栈跟踪(如果适用)

Model without Tools
Config Specs -  [ConfigurableFieldSpec(id='temperature', annotation=<class 'float'>, name='Temperature', description='The temperature of the model', default=0.0, is_shared=False, dependencies=None)]
Config Schema Json -  {"title": "RunnableConfigurableFieldsConfig", "type": "object", "properties": {"configurable": {"$ref": "#/definitions/Configurable"}}, "definitions": {"Configurable": {"title": "Configurable", "type": "object", "properties": {"temperature": {"title": "Temperature", "description": "The temperature of the model", "default": 0.0, "type": "number"}}}}}

Model with Tools
Config Specs -  []
Config Schema Json -  {"title": "ChatOpenAIConfig", "type": "object", "properties": {}}

描述

在使用工具的模型时,可配置字段没有暴露或在内部使用。
我做错了什么吗?请建议在使用 tool_calling 时设置可配置字段的正确方法。

系统信息

系统信息

操作系统:Linux
操作系统版本:langchain-ai/langgraph#1 SMP Fri Mar 29 23:14:13 UTC 2024
Python 版本:3.11.9 (main, Apr 19 2024, 16:48:06) [GCC 11.2.0]

软件包信息

langchain_core: 0.2.18
langchain: 0.2.7
langchain_community: 0.2.7
langsmith: 0.1.85
langchain_chroma: 0.1.2
langchain_cli: 0.0.25
langchain_openai: 0.1.16
langchain_text_splitters: 0.2.2
langgraph: 0.1.8
langserve: 0.2.2

1aaf6o9v

1aaf6o9v1#

使用 bind_tools 返回一个 RunnableBinding 的示例,其中绑定属性在这种情况下将是 ChatOpenAI。要恢复可配置字段,您可以在调用 bind_tools 后更新 RunnableBindingbound 属性:

from langchain_openai import ChatOpenAI
from langchain_core.runnables import ConfigurableField
from langchain_core.pydantic_v1 import BaseModel
import os

class Add(BaseModel):
    """Add two numbers"""
    a: int
    b: int

configurable_temperature = ConfigurableField(
    id="temperature",
    name="Temperature",
    description="The temperature of the model",
)

tools = [Add]

model = ChatOpenAI(temperature=0).configurable_fields(
    temperature=configurable_temperature
)

print("Model without Tools")
print("Config Specs - ", model.config_specs)
print("Config Schema Json - ", model.config_schema(include=["temperature"]).schema_json())

print("\n\nModel with Tools")
model_with_tools = model.bind_tools(tools)
model_with_tools.bound = ChatOpenAI(temperature=0).configurable_fields(
    temperature=configurable_temperature
) # This is what should be updated
print("Config Specs - ", model_with_tools.config_specs)
print("Config Schema Json - ", model_with_tools.config_schema(include=["temperature"]).schema_json())

我仔细检查并确认可配置字段按预期工作。此外,由于 RunnableBinding 将绑定属性存储在 kwargs 中,因此在更新 bound 属性时,它们仍然存在。

lf5gs5x2

lf5gs5x22#

我不确定PR是否能解决这个问题,因为在我看来,这需要bind_tools对所有支持它的ChatModels进行更改。@ccurme?

chy5wohz

chy5wohz3#

我也遇到了这个问题,在使用LangChain与NeMo Guardrails时,无法在绑定工具的模型上设置温度。上述解决方法对我似乎不起作用。

相关问题