You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LangChain中如何将用户输入Question传递至工具函数?

解决LangChain工具函数中question参数未定义的问题

1. 修改工具函数,接收输入参数

getProductsDetails目前未接收任何参数,需要添加参数来获取agent传递的用户查询内容:

export const getProductsDetails = async (input) => {
    await createPineconeIndex(client, indexName, vectorDimension);
    await updatePinecone(client, indexName, docs);
    // 将input作为查询参数传入Pinecone查询函数
    const queryResponse = await queryPineconeVectorStoreAndQueryLLM(client, indexName, input);
  
    return queryResponse ;    
};

2. 优化工具描述,引导agent正确传参

调整getProductInfo的描述文本,明确告知agent工具的输入要求,避免参数传递偏差:

export const getProductInfo = new DynamicTool({
  name: 'Get the product info',
  description: `需要获取myapp内产品信息时调用此工具。输入内容必须是用户的产品查询问题,工具会返回从Pinecone匹配到的对应信息。`,
  func: getProductsDetails,
});

3. 确保后端接口完整传递用户输入

你的后端接口已正确从req.body获取question并通过prompt模板传递给agent,可进一步确保prompt模板保留原始问题信息,示例模板如下:

const promptTemplate = PromptTemplate.fromTemplate(`请处理用户问题:{userPrompt},若需要产品相关信息,请调用Get the product info工具。`);

完成以上调整后,agent调用工具时会自动将用户的question作为输入传递给getProductsDetails,queryPineconeVectorStoreAndQueryLLM就能获取到有效参数。

内容的提问来源于stack exchange,提问作者Tracy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 10:55:22