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 8b2530e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 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 @@ -45,7 +46,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 +57,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] text: ${text} context: ${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 @@ -77,6 +84,10 @@ export class LobeMinimaxAI implements LobeRuntimeAI {
},
);

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 8b2530e

Please sign in to comment.