Elffuss BraidHub

kikocis/continue public

main · 2.5 KiBRawPermalink
1import { ChatCompletionCreateParams } from "openai/resources/index";
2import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js";
3
4import OpenAI from "./OpenAI.js";
5
6class Cerebras extends OpenAI {
7 static providerName = "cerebras";
8 static defaultOptions: Partial<LLMOptions> = {
9 apiBase: "https://api.cerebras.ai/v1/",
10 };
11 maxStopWords: number | undefined = 4;
12
13 constructor(options: LLMOptions) {
14 super(options);
15
16 // Set context length based on whether the model is the free version
17 if (options.model === "qwen-3-coder-480b-free") {
18 this._contextLength = 64000;
19 } else if (options.model === "qwen-3-coder-480b") {
20 this._contextLength = 128000;
21 }
22 }
23
24 private filterThinkingTags(content: string): string {
25 // Remove <thinking>...</thinking> tags (including multiline)
26 return content.replace(/<thinking>[\s\S]*?<\/thinking>/gi, "").trim();
27 }
28
29 private filterThinkingFromMessages(messages: ChatMessage[]): ChatMessage[] {
30 return messages.map((message) => {
31 if (typeof message.content === "string") {
32 return {
33 ...message,
34 content: this.filterThinkingTags(message.content),
35 } as ChatMessage;
36 } else if (Array.isArray(message.content)) {
37 return {
38 ...message,
39 content: message.content.map((part) => {
40 if (part.type === "text" && typeof part.text === "string") {
41 return {
42 ...part,
43 text: this.filterThinkingTags(part.text),
44 };
45 }
46 return part;
47 }),
48 } as ChatMessage;
49 }
50 return message;
51 });
52 }
53
54 protected _convertArgs(
55 options: CompletionOptions,
56 messages: ChatMessage[],
57 ): ChatCompletionCreateParams {
58 // Filter thinking tags from messages before processing
59 const filteredMessages = this.filterThinkingFromMessages(messages);
60 return super._convertArgs(options, filteredMessages);
61 }
62
63 private static modelConversion: { [key: string]: string } = {
64 "qwen-3-coder-480b-free": "qwen-3-coder-480b", // Maps free version to base model
65 "qwen-3-coder-480b": "qwen-3-coder-480b",
66 "qwen-3-235b-a22b-instruct-2507": "qwen-3-235b-a22b-instruct-2507",
67 "llama-3.3-70b": "llama-3.3-70b",
68 "qwen-3-32b": "qwen-3-32b",
69 "qwen-3-235b-a22b-thinking-2507": "qwen-3-235b-a22b-thinking-2507",
70 };
71 protected _convertModelName(model: string): string {
72 return Cerebras.modelConversion[model] ?? model;
73 }
74}
75
76export default Cerebras;