feat: handle Claude API; update temperature default

This commit is contained in:
Simon
2025-10-17 19:04:24 +08:00
parent 6f332aa24a
commit 1e3e089656
2 changed files with 21 additions and 12 deletions

View File

@@ -24,7 +24,7 @@ export function parseLLMConfig(config: LLMConfig): Required<LLMConfig> {
baseURL: config.baseURL ?? DEFAULT_BASE_URL,
apiKey: config.apiKey ?? DEFAULT_API_KEY,
modelName: config.modelName ?? DEFAULT_MODEL_NAME,
temperature: config.temperature ?? 0.0,
temperature: config.temperature ?? 0.5, // higher randomness helps auto-recovery
maxTokens: config.maxTokens ?? 4096,
maxRetries: config.maxRetries ?? LLM_MAX_RETRIES,
}

View File

@@ -5,6 +5,12 @@ import { InvokeError, InvokeErrorType } from './errors'
import type { InvokeResult, LLMClient, Message, OpenAIClientConfig, Tool } from './types'
import { zodToOpenAITool } from './utils'
// Claude's openAI-API has different format for some fields
const CLAUDE_PATCH = {
tool_choice: { type: 'tool', name: 'AgentOutput' },
thinking: { type: 'disabled' },
}
export class OpenAIClient implements LLMClient {
config: OpenAIClientConfig
@@ -21,7 +27,7 @@ export class OpenAIClient implements LLMClient {
const openaiTools = Object.entries(tools).map(([name, tool]) => zodToOpenAITool(name, tool))
// 2. Detect if Claude (auto-compatibility)
// const isClaude = this.config.model.toLowerCase().includes('claude')
const isClaude = this.config.model.toLowerCase().startsWith('claude')
// 3. Call API
let response: Response
@@ -34,18 +40,21 @@ export class OpenAIClient implements LLMClient {
},
body: JSON.stringify({
model: this.config.model,
messages,
parallel_tool_calls: false,
tools: openaiTools,
tool_choice: 'required',
// tool_choice: { type: 'function', function: { name: 'my_function' } },
// reasoning_effort: 'minimal',
// verbosity: 'low',
// Claude doesn't support tool_choice: 'required', auto-omit
// ...(isClaude ? {} : { tool_choice: 'required' }),
temperature: this.config.temperature,
max_tokens: this.config.maxTokens,
messages,
tools: openaiTools,
// tool_choice: 'required',
tool_choice: { type: 'function', function: { name: 'AgentOutput' } },
// model specific params
// reasoning_effort: 'minimal',
// verbosity: 'low',
parallel_tool_calls: false,
...(isClaude ? CLAUDE_PATCH : {}),
}),
signal: abortSignal,
})