Elffuss BraidHub

kikocis/continue public

main · 2.5 KiBRawPermalink
1import { streamSse } from "@continuedev/fetch";
2import { Chunk, CompletionOptions, LLMOptions } from "../../index.js";
3import { osModelsEditPrompt } from "../templates/edit.js";
4
5import OpenAI from "./OpenAI.js";
6
7class SiliconFlow extends OpenAI {
8 static providerName = "siliconflow";
9 static defaultOptions: Partial<LLMOptions> = {
10 apiBase: "https://api.siliconflow.cn/v1/",
11 model: "Qwen/Qwen2.5-Coder-32B-Instruct",
12 promptTemplates: {
13 edit: osModelsEditPrompt,
14 },
15 useLegacyCompletionsEndpoint: false,
16 };
17 maxStopWords: number | undefined = 16;
18
19 supportsFim(): boolean {
20 return true;
21 }
22
23 async *_streamFim(
24 prefix: string,
25 suffix: string,
26 signal: AbortSignal,
27 options: CompletionOptions,
28 ): AsyncGenerator<string> {
29 const endpoint = new URL("completions", this.apiBase);
30 const resp = await this.fetch(endpoint, {
31 method: "POST",
32 body: JSON.stringify({
33 model: options.model,
34 prompt: prefix,
35 suffix,
36 max_tokens: options.maxTokens,
37 temperature: options.temperature,
38 top_p: options.topP,
39 frequency_penalty: options.frequencyPenalty,
40 presence_penalty: options.presencePenalty,
41 stop: options.stop,
42 stream: true,
43 }),
44 headers: {
45 "Content-Type": "application/json",
46 Accept: "application/json",
47 Authorization: `Bearer ${this.apiKey}`,
48 },
49 signal,
50 });
51 for await (const chunk of streamSse(resp)) {
52 yield chunk.choices[0].text;
53 }
54 }
55
56 async rerank(query: string, chunks: Chunk[]): Promise<number[]> {
57 if (!query || query.trim() === "") {
58 console.warn("[SiliconFlow] rerank: query is empty");
59 return [];
60 }
61
62 if (!chunks || chunks.length === 0) {
63 console.warn("[SiliconFlow] rerank: chunks is empty");
64 return [];
65 }
66
67 const endpoint = new URL("rerank", this.apiBase);
68 const resp = await this.fetch(endpoint, {
69 method: "POST",
70 headers: {
71 "Content-Type": "application/json",
72 Accept: "application/json",
73 Authorization: `Bearer ${this.apiKey}`,
74 },
75 body: JSON.stringify({
76 model: this.model,
77 query,
78 documents: chunks.map((chunk) => chunk.content),
79 }),
80 });
81
82 if (!resp.ok) {
83 throw new Error(await resp.text());
84 }
85
86 const data = (await resp.json()) as any;
87 const results = data.results.sort((a: any, b: any) => a.index - b.index);
88 return results.map((result: any) => result.relevance_score);
89 }
90}
91
92export default SiliconFlow;