Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: getDefaultAgent is not a function #5816

Closed
5 tasks done
guidev opened this issue Jun 19, 2024 · 5 comments
Closed
5 tasks done

TypeError: getDefaultAgent is not a function #5816

guidev opened this issue Jun 19, 2024 · 5 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@guidev
Copy link
Contributor

guidev commented Jun 19, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

  1. git clone https://github.com/guidev/nonworking/tree/node20

  2. npm run start

Error Message and Stack Trace (if applicable)

TypeError: getDefaultAgent is not a function
    at OpenAI.buildRequest (file:///Users/name/Progetti/nonworking/node_modules/openai/core.mjs:208:66)
    at OpenAI.makeRequest (file:///Users/name/Progetti/nonworking/node_modules/openai/core.mjs:279:44)
    at async file:///Users/name/Progetti/nonworking/node_modules/@langchain/openai/dist/chat_models.js:796:29
    at async RetryOperation._fn (/Users/name/Progetti/nonworking/node_modules/p-retry/index.js:50:12)

Node.js v22.3.0

Description

When using openai-node with langchain and datadog, ESM, I get the following error:

System Info

	"@langchain/openai": "^0.1.3",
	"dd-trace": "^5.17.0",
	"dotenv": "^16.4.5",
	"express": "^4.19.2",
	"langchain": "0.2.5"
@guidev
Copy link
Contributor Author

guidev commented Jun 19, 2024

Also opened an issue at openai/openai-node#903

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jun 19, 2024
Copy link

dosubot bot commented Jun 19, 2024

To resolve the "TypeError: getDefaultAgent is not a function" error when using openai-node with langchain and datadog, ensure you are using the correct function names and imports from the langchain library. There is no getDefaultAgent function exported from langchain/src/agents/toolkits/index.ts.

Instead, use one of the available agent creation functions such as createOpenAIToolsAgent. Here is an example:

import { createOpenAIToolsAgent } from 'langchain/src/agents/openai_tools/index.js';

const agent = await createOpenAIToolsAgent({
  llm: new ChatOpenAI({
    temperature: 0,
    modelName: "gpt-3.5-turbo-1106",
  }),
  tools: [
    // your tools here
  ],
  prompt: await pull<ChatPromptTemplate>("hwchase17/openai-tools-agent"),
  streamRunnable: true,
});

Make sure to replace getDefaultAgent with the correct function name from the available exports [1][2][3].

To continue talking to Dosu, mention @dosu.

@guidev
Copy link
Contributor Author

guidev commented Jun 20, 2024

@dosu

Here's the full code:

import './config/datadog.js';
import express from 'express';
import {ChatOpenAI} from "@langchain/openai";
import {PromptTemplate} from "@langchain/core/prompts";
import {JsonOutputParser} from "@langchain/core/output_parsers";
import dotenv from "dotenv";

dotenv.config();


const app = express();

const prompt = PromptTemplate.fromTemplate(`Return a JSON object with a 'text' field containing a joke`);

const llm = new ChatOpenAI({modelName: "gpt-4o", verbose: true, apiKey: "doesntmatter"})
    .bind({
        response_format: {
            type: "json_object",
        },
    });

const parser = new JsonOutputParser();

const chain = prompt.pipe(llm).pipe(parser);

console.log(await chain.invoke({}));

app.listen(process.env.PORT || 3000);

Copy link

dosubot bot commented Jun 20, 2024

To resolve the "TypeError: getDefaultAgent is not a function" error, you should use the createOpenAIFunctionsAgent method instead of the deprecated OpenAIAgent class. Here is an updated version of your code using the createOpenAIFunctionsAgent method:

import './config/datadog.js';
import express from 'express';
import { ChatOpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { JsonOutputParser } from "@langchain/core/output_parsers";
import dotenv from "dotenv";
import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
import { pull } from "langchain/hub";
import { AIMessage, HumanMessage } from "@langchain/core/messages";

dotenv.config();

const app = express();

const prompt = await pull<PromptTemplate>("hwchase17/openai-functions-agent");

const llm = new ChatOpenAI({
  modelName: "gpt-4o",
  verbose: true,
  apiKey: process.env.OPENAI_API_KEY,
});

const agent = await createOpenAIFunctionsAgent({
  llm,
  tools: [], // Define your tools here
  prompt,
});

const agentExecutor = new AgentExecutor({
  agent,
  tools: [], // Define your tools here
});

const result = await agentExecutor.invoke({
  input: "Return a JSON object with a 'text' field containing a joke",
});

console.log(result);

app.listen(process.env.PORT || 3000);

Make sure to replace "hwchase17/openai-functions-agent" with the correct prompt template if needed and define any tools you want the agent to use. This should resolve the error and properly configure the agent [1].

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 19, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 26, 2024
@nakatsuka-k
Copy link

@guidev
I was able to resolve the issue by modifying it to make the request using Fetch as shown below.

      const openAiApiUrl = "https://api.openai.com/v1/chat/completions";
      
      const response = await fetch(openAiApiUrl, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "Authorization": `Bearer ${appConfig.openAiKey}`,
        },
        body: JSON.stringify({
          model: "gpt-3.5-turbo",
          messages: [...messages],
          max_tokens: 256,
        }),
      });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants