diff --git a/blocks/blocks/agent.ts b/blocks/blocks/agent.ts index 570e1c458d..c175fac450 100644 --- a/blocks/blocks/agent.ts +++ b/blocks/blocks/agent.ts @@ -4,11 +4,13 @@ import { BlockConfig } from '../types' // Map of models to their tools const MODEL_TOOLS = { 'gpt-4o': 'openai.chat', + 'o1': 'openai.chat', + 'o1-mini': 'openai.chat', + 'deepseek-v3': 'deepseek.chat', + 'deepseek-r1': 'deepseek.reasoner', 'claude-3-5-sonnet-20241022': 'anthropic.chat', 'gemini-pro': 'google.chat', - 'grok-2-latest': 'xai.chat', - 'deepseek-v3': 'deepseek.chat', - 'deepseek-r1': 'deepseek.reasoner' + 'grok-2-latest': 'xai.chat' } as const; export const AgentBlock: BlockConfig = { diff --git a/tools/openai/chat.ts b/tools/openai/chat.ts index c65b12c36a..75a7bc3b1d 100644 --- a/tools/openai/chat.ts +++ b/tools/openai/chat.ts @@ -7,6 +7,7 @@ interface ChatParams { model?: string; temperature?: number; maxTokens?: number; + maxCompletionTokens?: number; topP?: number; frequencyPenalty?: number; presencePenalty?: number; @@ -16,6 +17,7 @@ interface ChatParams { interface ChatResponse extends ToolResponse { tokens?: number; model: string; + reasoning_tokens?: number; } export const chatTool: ToolConfig = { @@ -42,12 +44,16 @@ export const chatTool: ToolConfig = { model: { type: 'string', default: 'gpt-4o', - description: 'Model to use (gpt-4o, o1-mini)' + description: 'Model to use (gpt-4o, o1, o1-mini)' }, temperature: { type: 'number', default: 0.7, - description: 'Controls randomness in the response' + description: 'Controls randomness in the response (not supported by o1 models)' + }, + maxCompletionTokens: { + type: 'number', + description: 'Maximum number of tokens to generate (including reasoning tokens) for o1 models' } }, @@ -59,24 +65,37 @@ export const chatTool: ToolConfig = { 'Authorization': `Bearer ${params.apiKey}` }), body: (params) => { - const messages = [ - { role: 'system', content: params.systemPrompt } - ]; + const isO1Model = params.model?.startsWith('o1'); + const messages = []; + + // For o1-mini, we need to use 'user' role instead of 'system' + if (params.model === 'o1-mini') { + messages.push({ role: 'user', content: params.systemPrompt }); + } else { + messages.push({ role: 'system', content: params.systemPrompt }); + } if (params.context) { messages.push({ role: 'user', content: params.context }); } - const body = { + const body: any = { model: params.model || 'gpt-4o', - messages, - temperature: params.temperature, - max_tokens: params.maxTokens, - top_p: params.topP, - frequency_penalty: params.frequencyPenalty, - presence_penalty: params.presencePenalty, - stream: params.stream + messages }; + + // Only add parameters supported by the model type + if (!isO1Model) { + body.temperature = params.temperature; + body.max_tokens = params.maxTokens; + body.top_p = params.topP; + body.frequency_penalty = params.frequencyPenalty; + body.presence_penalty = params.presencePenalty; + } else if (params.maxCompletionTokens) { + body.max_completion_tokens = params.maxCompletionTokens; + } + + body.stream = params.stream; return body; } }, @@ -92,7 +111,8 @@ export const chatTool: ToolConfig = { return { output: data.choices[0].message.content, tokens: data.usage?.total_tokens, - model: data.model + model: data.model, + reasoning_tokens: data.usage?.completion_tokens_details?.reasoning_tokens }; },