langchain ConfigurableFields对代理不起作用,

uttx8gqw  于 3个月前  发布在  其他
关注(0)|答案(4)|浏览(50)

检查其他资源

  • 我为这个问题添加了一个非常描述性的标题。
  • 我使用集成搜索在LangChain文档中进行了搜索。
  • 我使用GitHub搜索查找了一个类似的问题,但没有找到。
  • 我确信这是LangChain中的一个bug,而不是我的代码。
  • 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决此bug。

示例代码

import langchain.agents import AgentExecutor
import langchain.agents as lc_agents

def fetch_config_from_header(config: Dict[str, Any], req: Request) -> Dict[str, Any]:
    config = config.copy()
    configurable = config.get("configurable", {})
 
    if "x-model-name" in req.headers:
        configurable["model_name"] = req.headers["x-model-name"]
    else:
        raise HTTPException(401, "No model name provided")
   
    if "x-api-key" in req.headers:
        configurable["default_headers"] = {
            "Content-Type":"application/json",
            "api-key": req.headers["x-api-key"]
        }
    else:
        raise HTTPException(401, "No API key provided")
   
    if "x-model-kwargs" in req.headers:
        configurable["model_kwargs"] = json.loads(req.headers["x-model-kwargs"])
    else:
        raise HTTPException(401, "No model arguments provided")
   
    configurable["openai_api_base"] = f"https://someendpoint.com/{req.headers['x-model-name']}"
    config["configurable"] = configurable
    return config
 
chat_model = ChatOpenAI(
    model_name = "some_model",
    model_kwargs = {},
    default_headers = {},
    openai_api_key = "placeholder",
    openai_api_base = "placeholder").configurable_fields(
        model_name = ConfigurableField(id="model_name"),
        model_kwargs = ConfigurableField(id="model_kwargs"),
        default_headers = ConfigurableField(id="default_headers"),
        openai_api_base = ConfigurableField(id="openai_api_base"),
    )

agent = lc_agents.tool_calling_agent.base.create_tool_calling_agent(chat_model, tools, prompt_template)
runnable = AgentExecutor(agent=agent, tools=tools)

add_routes(
    app,
    runnable.with_types(input_type=InputChat),
    path="/some_agent",
    per_req_config_modifier=fetch_config_from_header,
)

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

  • 无响应*

描述

理想情况下,当我们将一个字段设置为可配置时,它应该根据per_req_config_modifier给出的新可配置值进行更新。然而,温度、openai_api_base、default_headers等可配置变量都没有传递给最终的客户端。某些相关函数的一些相关值如下:

# returned value of config in fetch_config_from_header()
{'configurable': {'model_name': 'some_model', 'default_headers': {'Content-Type': 'application/json', 'api-key': 'some_api_key'}, 'model_kwargs': {'user': 'some_user'}, 'openai_api_base': 'https://someendpoint.com/some_model', 'temperature': 0.6}

# values of cast_to, opts in openai's _base_client.py AsyncAPIClient.post()
cast_to: <class 'openai.types.chat.chat_completion.ChatCompletion'>
opts: method='post' url='/chat/completions' params={} headers=NOT_GIVEN max_retries=NOT_GIVEN timeout=NOT_GIVEN files=None idempotency_key=None post_parser=NOT_GIVEN json_data={'messages': [{'content': 'some_content', 'role': 'system'}], 'model': 'default_model', 'n': 1, 'stream': False, 'temperature': 0.7} extra_json=None

系统信息

langchain==0.2.6
langchain-community==0.2.6
langchain-core==0.2.10
langchain-experimental==0.0.62
langchain-openai==0.1.13
langchain-text-splitters==0.2.2
langgraph==0.1.5
langserve==0.2.2
langsmith==0.1.82
openai==1.35.7

platform = linux
python version = 3.12.4
xienkqul

xienkqul1#

Hi @quadcube,这是一个已知的问题,关于langchain中的代理执行器。我们正在将用户迁移到使用langgraph中的执行器,该执行器已经构建来解决langchain中执行器的一些底层问题。
感谢您提供的信息!我明天早上将使用langgraph的执行器进行测试,并报告结果。

ghhaqwfi

ghhaqwfi3#

@spike-spiegel-21 这里是可配置代理的示例,以便重现问题。
提前感谢!

deikduxw

deikduxw4#

Hi @quadcube,这是一个已知的问题,关于langchain中的代理执行器。我们正在将用户迁移到使用构建的langgraph中的执行器,以解决langchain中执行器的一些底层问题。

相关问题