fix(minecraft): skip reasoning parameter for Cerebras API to avoid unsupported field errors
Add isCerebrasBaseURL helper detecting cerebras.ai/cerebras.com domains, conditionally omit reasoning parameter when using Cerebras baseURL since their API doesn't support reasoning field, preserve reasoning defaults (effort: low) for non-Cerebras providers
This commit is contained in:
@@ -3,47 +3,56 @@ import type { Message } from '@xsai/shared-chat'
|
||||
import { generateText } from '@xsai/generate-text'
|
||||
|
||||
export interface LLMConfig {
|
||||
baseURL: string
|
||||
apiKey: string
|
||||
model: string
|
||||
baseURL: string
|
||||
apiKey: string
|
||||
model: string
|
||||
}
|
||||
|
||||
export interface LLMCallOptions {
|
||||
messages: Message[]
|
||||
responseFormat?: { type: 'json_object' }
|
||||
reasoning?: { effort: 'low' | 'medium' | 'high' }
|
||||
messages: Message[]
|
||||
responseFormat?: { type: 'json_object' }
|
||||
reasoning?: { effort: 'low' | 'medium' | 'high' }
|
||||
}
|
||||
|
||||
export interface LLMResult {
|
||||
text: string
|
||||
reasoning?: string
|
||||
usage: any
|
||||
text: string
|
||||
reasoning?: string
|
||||
usage: any
|
||||
}
|
||||
|
||||
/**
|
||||
* Lightweight LLM agent for text generation using xsai
|
||||
*/
|
||||
export class LLMAgent {
|
||||
constructor(private config: LLMConfig) { }
|
||||
constructor(private config: LLMConfig) { }
|
||||
|
||||
/**
|
||||
* Call LLM with the given messages
|
||||
*/
|
||||
async callLLM(options: LLMCallOptions): Promise<LLMResult> {
|
||||
const response = await generateText({
|
||||
baseURL: this.config.baseURL,
|
||||
apiKey: this.config.apiKey,
|
||||
model: this.config.model,
|
||||
messages: options.messages,
|
||||
...(options.responseFormat && { responseFormat: options.responseFormat }),
|
||||
// Enable reasoning with configurable effort (default: low)
|
||||
reasoning: options.reasoning ?? { effort: 'low' },
|
||||
} as Parameters<typeof generateText>[0])
|
||||
private isCerebrasBaseURL(baseURL: string): boolean {
|
||||
const normalized = baseURL.toLowerCase()
|
||||
return normalized.includes('cerebras.ai') || normalized.includes('cerebras.com')
|
||||
}
|
||||
|
||||
return {
|
||||
text: response.text ?? '',
|
||||
reasoning: (response as any).reasoningText,
|
||||
usage: response.usage,
|
||||
}
|
||||
/**
|
||||
* Call LLM with the given messages
|
||||
*/
|
||||
async callLLM(options: LLMCallOptions): Promise<LLMResult> {
|
||||
const shouldSendReasoning = !this.isCerebrasBaseURL(this.config.baseURL)
|
||||
const response = await generateText({
|
||||
baseURL: this.config.baseURL,
|
||||
apiKey: this.config.apiKey,
|
||||
model: this.config.model,
|
||||
messages: options.messages,
|
||||
headers: { 'Accept-Encoding': 'identity' },
|
||||
...(options.responseFormat && { responseFormat: options.responseFormat }),
|
||||
...(shouldSendReasoning && {
|
||||
// Enable reasoning with configurable effort (default: low)
|
||||
reasoning: options.reasoning ?? { effort: 'low' },
|
||||
}),
|
||||
} as Parameters<typeof generateText>[0])
|
||||
|
||||
return {
|
||||
text: response.text ?? '',
|
||||
reasoning: (response as any).reasoningText,
|
||||
usage: response.usage,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user