Error “[object Object]“ when using LangChain‘s AzureChatOpenAI in Node.js

**题意:**在Node.js中使用LangChain库的AzureChatOpenAI功能时遇到错误"Error '[object Object]'"

问题背景:

I'm attempting to use LangChain's AzureChatOpenAI with the gpt-35-turbo-16k model in a Node.js application to create an OpenAI Function Agent. I've properly set up the required environment variables for Azure OpenAI setup. However, I'm encountering an error, which is not very descriptive, when trying to run the function.

在Node.js应用程序中尝试使用LangChain的AzureChatOpenAI与gpt-35-turbo-16k模型来创建一个OpenAI Function Agent时,已经正确地设置了Azure OpenAI所需的环境变量,但在尝试运行该函数时遇到了一个不太具体的错误

Here's the relevant code snippet: 下面是相关的代码片段:

cs 复制代码
import { pull } from "langchain/hub";
import type { ChatPromptTemplate } from "@langchain/core/prompts";
import { AzureChatOpenAI } from "@langchain/azure-openai";

const model = new AzureChatOpenAI({
  azureOpenAIApiDeploymentName: "gpt-35-turbo-16k",
  azureOpenAIApiVersion: "2024-03-01-preview",
  modelName: "gpt-35-turbo-16k",
  temperature: 0,
  maxRetries: 1,
});

export async function ChatWithFunctionAgent(question: string) {
  try {
    // Get the prompt to use - you can modify this!
    // If you want to see the prompt in full, you can at:
    // https://smith.langchain.com/hub/hwchase17/openai-functions-agent
    const prompt = await pull<ChatPromptTemplate>(
      "hwchase17/openai-functions-agent"
    );
    const agent = await createOpenAIFunctionsAgent({
      llm: model,
      tools: [],
      prompt,
    });
    const agentExecutor = new AgentExecutor({
      agent,
      tools: [],
    });
    const result = await agentExecutor.invoke({
      input: "what is LangChain?",
    });
    console.log(result);
    return result;
  } catch (error) {
    console.log(error);
  }
}

Error: 错误信息:

cs 复制代码
Http function processed request for url "http://localhost:7071/api/chat"
[1] [2024-04-04T05:27:52.461Z] Error: [object Object]
[1] [2024-04-04T05:27:52.461Z]     at /home/ujwal/code/roc-next/azure-func/node_modules/@langchain/core/dist/utils/async_caller.cjs:98:23
[1] [2024-04-04T05:27:52.461Z]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[2024-04-04T05:27:52.461Z]     at async RetryOperation._fn (/home/ujwal/code/roc-next/azure-func/node_modules/p-retry/index.js:50:12) {
[1] [2024-04-04T05:27:52.461Z]   attemptNumber: 2,
[1] [2024-04-04T05:27:52.461Z]   retriesLeft: 0
[1] [2024-04-04T05:27:52.461Z] }

What I've tried: 我已经尝试做的事情:

  • I've already double-checked to ensure all the required environment variables are set, including those needed for authentication with Azure.

我已经反复检查过了,确保所有必需的环境变量都已设置,包括与Azure进行身份验证所需的环境变量。

  • I've manually invoked the model, that is working fine, so no issues with Azure credentials or connection.

我已经手动调用了模型,它工作正常,所以Azure的凭据或连接没有问题。

Expected behavior: 期望结果:

  • AzureChatOpenAI to work with langchain OpenAIFunction Agent

AzureChatOpenAIlangchainOpenAIFunctionAgent协同工作

Any help would be greatly appreciated! 任何帮助都将不胜感激!

问题解决:

I figured out why this issue was occuring, if the tools array is empty, the AzureOpenAI API throws an error. To fix this, you need to have atleast one tool in the tools array, you can refer to Defining custom tools | 🦜️🔗 Langchain to create custom tools. You also need to make sure that the name of the tool does not contain any spaces.

我弄清楚了这个问题发生的原因,如果tools数组为空,AzureOpenAI API会抛出一个错误。为了解决这个问题,你需要在tools数组中至少包含一个工具。你可以参考"Defining custom tools | 🦜️🔗 Langchain"来创建自定义工具。此外,你还需要确保工具的名称中不包含任何空格。

cs 复制代码
import { DynamicTool } from "@langchain/core/tools";

const tools = [
  new DynamicTool({
    name: "FOO",
    description:
      "call this to get the value of foo. input should be an empty string.",
    func: async () => "baz",
  })
]
Hope this helps.
相关推荐
TechMastery2 小时前
【LLM大模型技术专题】「入门到精通系列教程」LangChain4j与Spring Boot集成开发实战指南
langchain·llm
-曾牛2 小时前
【LangChain4j快速入门】5分钟用Java玩转GPT-4o-mini,Spring Boot整合实战!| 附源码
java·开发语言·人工智能·spring boot·ai·chatgpt
TDengine (老段)2 小时前
TDengine 语言连接器(Node.js)
大数据·c语言·数据库·物联网·node.js·时序数据库·tdengine
月之圣痕2 小时前
c#清理释放内存
c#
wt_cs2 小时前
身份认证C#集成方案-数字时代身份证实名认证利器
开发语言·c#
QQ_7781329743 小时前
【LangChain实战】构建下一代智能问答系统:从RAG架构到生产级优化
langchain
傻小胖3 小时前
Node.js 中文件系统模块(`fs`)的详细总结,包括定义、作用、各种写入方式及使用场景
node.js
质变科技AI就绪数据云3 小时前
质变科技发布自主数据分析MCP Server
ai·数据挖掘·数据分析·mcp·人工智能代理
幼儿园园霸柒柒4 小时前
第七章:7.2求方程a*x*x+b*x+c=0的根,用3个函数,分别求当:b*b-4*a*c大于0、等于0和小于0时的根并输出结果。从主函数输入a、b、c的值
c语言·开发语言·算法·c#
堆栈future4 小时前
基于MCP构建天气预报智能体
llm·openai·mcp