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

🐛 fix: MiniMax output long content interrupted by non-existent error #4088

Merged
merged 13 commits into from
Sep 25, 2024
8 changes: 4 additions & 4 deletions src/libs/agent-runtime/minimax/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ describe('LobeMinimaxAI', () => {
});
});

it('should include max tokens when model is abab6.5-chat', () => {
it('should include max tokens when model is abab6.5t-chat', () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'abab6.5-chat',
model: 'abab6.5t-chat',
temperature: 0,
top_p: 0,
};
Expand All @@ -265,9 +265,9 @@ describe('LobeMinimaxAI', () => {

expect(result).toEqual({
messages: [{ content: 'Hello', role: 'user' }],
model: 'abab6.5-chat',
model: 'abab6.5t-chat',
stream: true,
max_tokens: 2048,
max_tokens: 4096,
});
});
});
Expand Down
22 changes: 16 additions & 6 deletions src/libs/agent-runtime/minimax/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ export class LobeMinimaxAI implements LobeRuntimeAI {
// https://www.minimaxi.com/document/guides/chat-model/V2
private getMaxTokens(model: string): number | undefined {
switch (model) {
case 'abab6.5-chat':
case 'abab6.5t-chat':
case 'abab6.5g-chat':
case 'abab5.5s-chat':
case 'abab5.5-chat':{
return 4096;
}
case 'abab6.5s-chat': {
return 2048;
return 8192;
}
}
}
Expand All @@ -139,12 +144,17 @@ export class LobeMinimaxAI implements LobeRuntimeAI {

return {
...params,
max_tokens: this.getMaxTokens(payload.model),
frequency_penalty: undefined,
max_tokens:
payload.max_tokens !== undefined
? payload.max_tokens
: this.getMaxTokens(payload.model),
presence_penalty: undefined,
stream: true,
temperature:
temperature === undefined || temperature <= 0
? undefined
: temperature / 2,
temperature === undefined || temperature <= 0
? undefined
: temperature / 2,

tools: params.tools?.map((tool) => ({
function: {
Expand Down
12 changes: 12 additions & 0 deletions src/libs/agent-runtime/utils/streams/minimax.ts
arvinxx marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ const unit8ArrayToJSONChunk = (unit8Array: Uint8Array): OpenAI.ChatCompletionChu
// chunkValue example:
// data: {"id":"028a65377137d57aaceeffddf48ae99f","choices":[{"finish_reason":"tool_calls","index":0,"delta":{"role":"assistant","tool_calls":[{"id":"call_function_7371372822","type":"function","function":{"name":"realtime-weather____fetchCurrentWeather","arguments":"{\"city\": [\"杭州\", \"北京\"]}"}}]}}],"created":155511,"model":"abab6.5s-chat","object":"chat.completion.chunk"}

const dataIdPattern = /data: {"id":/g;
const matchCount = (chunkValue.match(dataIdPattern) || []).length;
if (matchCount === 2) {
const secondDataIdIndex = chunkValue.indexOf(
'data: {"id":',
chunkValue.indexOf('data: {"id":') + 1,
);
if (secondDataIdIndex !== -1) {
chunkValue = chunkValue.slice(0, secondDataIdIndex).trim();
}
}

// so we need to remove `data:` prefix and then parse it as JSON
if (chunkValue.startsWith('data:')) {
chunkValue = chunkValue.slice(5).trim();
Expand Down