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 when invoke ZhipuAI chatmodel with invalid api key #4997

Closed
5 tasks done
jason-wangting opened this issue Apr 7, 2024 · 1 comment · Fixed by #5212, cnkang/LibreChat#51 or cnkang/LibreChat#52 · May be fixed by aechegoyan17/LibreChat#1 or Avish24x/ai-companion#5
Closed
5 tasks done
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@jason-wangting
Copy link

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

import { ChatZhipuAI } from "@langchain/community/chat_models/zhipuai";

const model = new ChatZhipuAI({
     modelName: "glm-4", // Available models:
     temperature: 1,
     zhipuAIApiKey: "invalid api key", 
});
 const res = await model.invoke([new HumanMessage("Hello")]);

Error Message and Stack Trace (if applicable)

    TypeError: Cannot read properties of undefined (reading '0')
                at /node_modules/@langchain/community/dist/chat_models/zhipuai.cjs:209:64
                at processTicksAndRejections (node:internal/process/task_queues:95:5)
                at ChatZhipuAI._generate (/node_modules/@langchain/community/dist/chat_models/zhipuai.cjs:202:15)
                at async Promise.allSettled (index 0)
                at ChatZhipuAI._generateUncached (/node_modules/@langchain/core/dist/language_models/chat_models.cjs:118:25)
                at ChatZhipuAI.invoke (/node_modules/@langchain/core/dist/language_models/chat_models.cjs:54:24)

Description

I am trying to use ChatZhipuAI in my project.
Code is here:

import { ChatZhipuAI } from "@langchain/community/chat_models/zhipuai";

const model = new ChatZhipuAI({
     modelName: "glm-4", // Available models:
     temperature: 1,
     zhipuAIApiKey: "invalid api key", 
});
 const res = await model.invoke([new HumanMessage("Hello")]);

You will get a TypeError if you run the code.

You can find the error point here: zhipuai.ts#327

The reason is that ZhipuChatModel will return {error: {code: 1002, message: 'error message'}} not { code: 1002, message: 'error message' } when you invoke with invalid api key.

So the error will not be caught by zhipuai.ts#324, lead to the TypeError.

System Info

"langchain": "^0.1.31",
"@langchain/community": "0.0.44",

Copy link

dosubot bot commented Apr 7, 2024

Hey @jason-wangting, great to see you diving into the depths of LangChain again! 🚀

Based on the error message and stack trace you provided, it seems that the error is occurring because the error handling logic in the ChatZhipuAI model from the @langchain/community package is expecting a different error format than what ZhipuChatModel returns when invoked with an invalid API key.

The ZhipuChatModel returns an error object in the format {error: {code: 1002, message: 'error message'}} when invoked with an invalid API key. However, the error handling logic in the ChatZhipuAI model seems to expect a different format, leading to the TypeError.

To resolve this issue, you should modify the error handling logic in the completionWithRetry method to correctly interpret and handle the error format returned by the ZhipuChatModel. Here's a suggested modification to the completionWithRetry method:

async completionWithRetry(
  request: ChatCompletionRequest,
  stream: boolean,
  signal?: AbortSignal,
  onmessage?: (event: MessageEvent) => void
) {
  const makeCompletionRequest = async () => {
    const response = await fetch(this.apiUrl, {
      method: "POST",
      headers: {
        ...(stream ? { Accept: "text/event-stream" } : {}),
        Authorization: `Bearer ${this.zhipuAIApiKey}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(request),
      signal,
    });

    // Check if the response status code indicates an error
    if (!response.ok) {
      // Extract error message from response body if possible
      const errorBody = await response.json().catch(() => null);
      const errorMessage = errorBody?.message || "An error occurred while making the request.";
      throw new Error(`Request failed with status ${response.status}: ${errorMessage}`);
    }

    // ... rest of the method
  };

  return this.caller.call(makeCompletionRequest);
}

This modification ensures that if the response status code indicates an error (e.g., 401 Unauthorized for an invalid API key), an error is thrown with a message that includes the status code and any error message provided in the response body. This approach allows for more accurate and informative error handling, especially in cases where the error format returned by ZhipuChatModel differs from what the existing error handling logic expects.

You can find more information about this in the LangChain JS repository.

Please let me know if this helps or if you need further assistance.

Sources

About Dosu

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