From 70b65524011f234d1cc060a5f2acbc342ab587ed Mon Sep 17 00:00:00 2001 From: Kevin Taylor Date: Fri, 1 Aug 2025 10:57:16 +0900 Subject: [PATCH] Update available Cerebras models + handling functions (#6915) * Update available Cerebras models + handling functions * Format + lint --- core/llm/llms/Cerebras.ts | 62 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/core/llm/llms/Cerebras.ts b/core/llm/llms/Cerebras.ts index a8a39f77b..534bc04ea 100644 --- a/core/llm/llms/Cerebras.ts +++ b/core/llm/llms/Cerebras.ts @@ -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 ... tags (including multiline) + return content.replace(/[\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;