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

ChatOpenAI JSON mode errors in agent chain: llm.predictMessages is not a function #3344

Closed
gramliu opened this issue Nov 20, 2023 · 2 comments · Fixed by #3346
Closed

ChatOpenAI JSON mode errors in agent chain: llm.predictMessages is not a function #3344

gramliu opened this issue Nov 20, 2023 · 2 comments · Fixed by #3346
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@gramliu
Copy link
Contributor

gramliu commented Nov 20, 2023

I'm binding a ChatOpenAI model with JSON mode as specified in the docs:

const model = new ChatOpenAI({
    modelName: "gpt-4-1106-preview",
  }).bind({
    response_format: {
      type: "json_object",
    },
  });

I'm then using this model in a chain as part of an agent:

const chain = new LLMChain({
  prompt: ChatPromptTemplate.fromTemplate(
    "Return the current weather. Use JSON format."
  ),
  llm: model,
});
const agent = new OpenAIAgent({
  llmChain: chain,
  allowedTools: tools.map((tool) => tool.name),
  tools,
});
const executor = new AgentExecutor({
  agent,
  tools,
  verbose: true,
  maxIterations: 10,
});

After calling the executor, however, I'm seeing this error: TypeError: llm.predictMessages is not a function.

I found this issue (#3226) and its fix (#3200) which patches a similar error for llm.callKeys, but it doesn't look like the predictMessages call in the agent class was handled yet.

Copy link

dosubot bot commented Nov 20, 2023

🤖

Hello @gramliu,

I'm an AI bot designed to assist you with bug fixes, answering questions, and guiding you through your contributions. While we're waiting for a human maintainer, I'm here to help. Please know that I'll be taking a closer look at the issue you've reported and will provide you with a thorough answer soon.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Nov 20, 2023
Copy link

dosubot bot commented Nov 20, 2023

🤖

Hello @gramliu,

I'm an AI bot here to help you with your questions, issues, and contributions to the langchainjs repository while we wait for a human maintainer. Let's work together to resolve your issue!

Based on the code snippets and error message you've provided, it seems like the issue is arising because the ChatOpenAI class doesn't have a predictMessages method. The predictMessages method is part of the BaseChatModel class, but it doesn't appear to be implemented in the ChatOpenAI class.

In the OpenAIAgent class, the llm.predictMessages method is called, which is expected to be a method of the language model (llm) instance. However, in your case, the llm instance is an instance of the ChatOpenAI class, which doesn't have a predictMessages method, hence the error.

To resolve this issue, you could consider implementing a predictMessages method in the ChatOpenAI class that wraps around the _call method. Here's a rough example of how you might do this:

async predictMessages(
  messages: BaseMessage[],
  options?: string[] | CallOptions,
  callbacks?: Callbacks
): Promise<BaseMessage> {
  // Convert the messages to a prompt string
  const prompt = messages.map(message => message.content).join('\n');
  
  // Call the OpenAI API
  const response = await this._call(prompt, options, callbacks);
  
  // Convert the response to a BaseMessage instance
  const message = new BaseMessage({ content: response });
  
  return message;
}

Please note that this is a simplified example and you might need to adjust it according to your specific needs and the exact structure of your BaseMessage class.

If you're not comfortable making these changes yourself, I would recommend reaching out to the maintainers of the langchainjs library and asking them to implement this feature. You could do this by opening a new issue in the langchainjs repository and describing the problem and your proposed solution.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

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

Successfully merging a pull request may close this issue.

1 participant