Skip to content

Commit

Permalink
fix: #4997 zhipu chat model response typeError (#5212)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonnow authored May 1, 2024
1 parent 4592eaf commit 1329a61
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions libs/langchain-community/src/chat_models/zhipuai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ interface ResponseChoice {
message: ChoiceMessage;
}

interface ZhipuAIError {
error: BaseResponse;
}

/**
* Interface representing a response from a chat completion.
*/
interface ChatCompletionResponse extends BaseResponse {
interface ChatCompletionResponse extends ZhipuAIError {
choices: ResponseChoice[];
created: number;
id: string;
Expand Down Expand Up @@ -295,12 +299,12 @@ export class ChatZhipuAI extends BaseChatModel implements ChatZhipuAIParams {
options?.signal,
(event) => {
const data: ChatCompletionResponse = JSON.parse(event.data);
if (data?.code) {
if (data?.error?.code) {
if (rejected) {
return;
}
rejected = true;
reject(new Error(data?.message));
reject(new Error(data?.error?.message));
return;
}

Expand Down Expand Up @@ -340,8 +344,8 @@ export class ChatZhipuAI extends BaseChatModel implements ChatZhipuAIParams {
false,
options?.signal
).then<ChatCompletionResponse>((data) => {
if (data?.code) {
throw new Error(data?.message);
if (data?.error?.code) {
throw new Error(data?.error?.message);
}
const { finish_reason, message } = data.choices[0];
const text = message.content;
Expand Down

0 comments on commit 1329a61

Please sign in to comment.