Skip to content

Commit

Permalink
Capture reasoning content inside openai stream processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-FrançoisMillet authored and Jean-FrançoisMillet committed Feb 11, 2025
1 parent cc22a05 commit e471961
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 447 deletions.
2 changes: 0 additions & 2 deletions src/libs/agent-runtime/openrouter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { LOBE_DEFAULT_MODEL_LIST } from '@/config/modelProviders';

import { ModelProvider } from '../types';
import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory';
import { OpenRouterReasoningStream } from '../utils/streams';
import { OpenRouterModelCard } from './type';

export const LobeOpenRouterAI = LobeOpenAICompatibleFactory({
Expand All @@ -15,7 +14,6 @@ export const LobeOpenRouterAI = LobeOpenAICompatibleFactory({
stream: payload.stream ?? true,
} as any;
},
handleStream: OpenRouterReasoningStream,
},
constructorOptions: {
defaultHeaders: {
Expand Down
1 change: 0 additions & 1 deletion src/libs/agent-runtime/utils/streams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export * from './bedrock';
export * from './google-ai';
export * from './ollama';
export * from './openai';
export * from './openrouter';
export * from './protocol';
export * from './qwen';
export * from './spark';
19 changes: 16 additions & 3 deletions src/libs/agent-runtime/utils/streams/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,22 @@ export const transformOpenAIStream = (
}

if (item.delta) {
let reasoning_content =
'reasoning_content' in item.delta ? item.delta.reasoning_content : null;
let content = 'content' in item.delta ? item.delta.content : null;
type Delta = {
content?: string;
reasoning?: string;
reasoning_content?: string;
};

const delta = item.delta as Delta;
let reasoning_content: string | null = null;

if ('reasoning_content' in delta && typeof delta.reasoning_content === 'string') {
reasoning_content = delta.reasoning_content;
} else if ('reasoning' in delta && typeof delta.reasoning === 'string') {
reasoning_content = delta.reasoning;
}

let content = 'content' in delta ? delta.content : null;

// DeepSeek reasoner will put thinking in the reasoning_content field
// litellm and not set content = null when processing reasoning content
Expand Down
Loading

0 comments on commit e471961

Please sign in to comment.