Update available Cerebras models + handling functions (#6915)

* Update available Cerebras models + handling functions

* Format + lint
This commit is contained in:
Kevin Taylor 2025-08-01 10:57:16 +09:00 • committed by GitHub
parent d194266bf9
commit 70b6552401
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,4 +1,5 @@
import { LLMOptions } from "../../index.js";
import { ChatCompletionCreateParams } from "openai/resources/index";
import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js";
import OpenAI from "./OpenAI.js";
@ -9,10 +10,63 @@ class Cerebras extends OpenAI {
};
maxStopWords: number | undefined = 4;
constructor(options: LLMOptions) {
super(options);
// Set context length based on whether the model is the free version
if (options.model === "qwen-3-coder-480b-free") {
this._contextLength = 64000;
} else if (options.model === "qwen-3-coder-480b") {
this._contextLength = 128000;
}
}
private filterThinkingTags(content: string): string {
// Remove <thinking>...</thinking> tags (including multiline)
return content.replace(/<thinking>[\s\S]*?<\/thinking>/gi, "").trim();
}
private filterThinkingFromMessages(messages: ChatMessage[]): ChatMessage[] {
return messages.map((message) => {
if (typeof message.content === "string") {
return {
...message,
content: this.filterThinkingTags(message.content),
} as ChatMessage;
} else if (Array.isArray(message.content)) {
return {
...message,
content: message.content.map((part) => {
if (part.type === "text" && typeof part.text === "string") {
return {
...part,
text: this.filterThinkingTags(part.text),
};
}
return part;
}),
} as ChatMessage;
}
return message;
});
}
protected _convertArgs(
options: CompletionOptions,
messages: ChatMessage[],
): ChatCompletionCreateParams {
// Filter thinking tags from messages before processing
const filteredMessages = this.filterThinkingFromMessages(messages);
return super._convertArgs(options, filteredMessages);
}
private static modelConversion: { [key: string]: string } = {
"llama3.1-8b": "llama3.1-8b",
"llama3.1-70b": "llama3.1-70b",
"llama-4-scout-17b-16e-instruct": "llama-4-scout-17b-16e-instruct",
"qwen-3-coder-480b-free": "qwen-3-coder-480b", // Maps free version to base model
"qwen-3-coder-480b": "qwen-3-coder-480b",
"qwen-3-235b-a22b-instruct-2507": "qwen-3-235b-a22b-instruct-2507",
"llama-3.3-70b": "llama-3.3-70b",
"qwen-3-32b": "qwen-3-32b",
"qwen-3-235b-a22b-thinking-2507": "qwen-3-235b-a22b-thinking-2507",
};
protected _convertModelName(model: string): string {
return Cerebras.modelConversion[model] ?? model;