Skip to content

Commit

Permalink
add debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshen committed Apr 21, 2024
1 parent 2c774e7 commit 7a954b3
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/libs/agent-runtime/minimax/index.ts
Expand Up @@ -2,6 +2,7 @@ import { StreamingTextResponse } from 'ai';
import { isEmpty } from 'lodash-es';
import OpenAI from 'openai';

import { debugStream } from '@/libs/agent-runtime/utils/debugStream';
import { fetchSSE } from '@/utils/fetch';

import { LobeRuntimeAI } from '../BaseAI';
Expand Down Expand Up @@ -37,6 +38,7 @@ export class LobeMinimaxAI implements LobeRuntimeAI {
options?: ChatCompetitionOptions,
): Promise<StreamingTextResponse> {
try {
const encoder = new TextEncoder();
let dataResponse: MinimaxResponse;
let streamController: ReadableStreamDefaultController;
const readableStream = new ReadableStream({
Expand All @@ -45,7 +47,7 @@ export class LobeMinimaxAI implements LobeRuntimeAI {
},
});

await fetchSSE(
const response = await fetchSSE(
() =>
fetch('https://api.minimax.chat/v1/text/chatcompletion_v2', {
body: JSON.stringify(this.buildCompletionsParams(payload)),
Expand All @@ -56,11 +58,17 @@ export class LobeMinimaxAI implements LobeRuntimeAI {
method: 'POST',
}),
{
onFinish: async () => {
onFinish: async (text, context) => {
if (process.env.DEBUG_MINIMAX_CHAT_COMPLETION === '1') {
console.log(`[minimax finish]\ntext: ${text} \ncontext: ${JSON.stringify(context)}`);
}
streamController.close();
this.throwIfErrorResponse(dataResponse);
},
onMessageHandle: (text) => {
if (process.env.DEBUG_MINIMAX_CHAT_COMPLETION === '1') {
console.log(`[minimax] ${text}`);
}
let body = text;
if (body.startsWith('data:')) {
body = body.slice(5).trim();
Expand All @@ -71,12 +79,18 @@ export class LobeMinimaxAI implements LobeRuntimeAI {
const data = JSON.parse(body) as MinimaxResponse;
dataResponse = data;
if (data.choices?.at(0)?.delta?.content) {
streamController.enqueue(data.choices.at(0)?.delta.content);
streamController.enqueue(
encoder.encode(data.choices.at(0)?.delta.content || undefined),
);
}
},
},
);

if (process.env.DEBUG_MINIMAX_CHAT_COMPLETION === '1' && response?.body) {
debugStream(response.body).catch(console.error);
}

return new StreamingTextResponse(readableStream, { headers: options?.headers });
} catch (error) {
const err = error as Error | ChatCompletionErrorPayload;
Expand Down

0 comments on commit 7a954b3

Please sign in to comment.