| 1 | import { ChatCompletionCreateParams } from "openai/resources/index"; |
| 2 | import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; |
| 3 | |
| 4 | import OpenAI from "./OpenAI.js"; |
| 5 | |
| 6 | class NCompass extends OpenAI { |
| 7 | static providerName = "ncompass"; |
| 8 | static defaultOptions: Partial<LLMOptions> = { |
| 9 | apiBase: "https://api.ncompass.tech/v1/", |
| 10 | }; |
| 11 | static embeddingsApiEndpoint = "https://api.gcp.ncompass.tech/v1/embeddings"; |
| 12 | |
| 13 | private static modelConversion: { [key: string]: string } = { |
| 14 | "qwen2.5-coder-7b": "Qwen/Qwen2.5-Coder-7B-Instruct", |
| 15 | "qwen2.5-coder:7b": "Qwen/Qwen2.5-Coder-7B-Instruct", |
| 16 | "qwen2.5-coder-32b": "Qwen/Qwen2.5-Coder-32B-Instruct", |
| 17 | "qwen2.5-coder:32b": "Qwen/Qwen2.5-Coder-32B-Instruct", |
| 18 | }; |
| 19 | protected _convertModelName(model: string): string { |
| 20 | return NCompass.modelConversion[model] ?? model; |
| 21 | } |
| 22 | |
| 23 | protected _convertArgs( |
| 24 | options: CompletionOptions, |
| 25 | messages: ChatMessage[], |
| 26 | ): ChatCompletionCreateParams { |
| 27 | const finalOptions = super._convertArgs(options, messages); |
| 28 | finalOptions.model = this._convertModelName(options.model); |
| 29 | return finalOptions; |
| 30 | } |
| 31 | |
| 32 | protected _getHeaders() { |
| 33 | const headers = super._getHeaders() as { |
| 34 | "Content-Type": string; |
| 35 | Authorization: string; |
| 36 | "api-key": string; |
| 37 | Accept?: string; |
| 38 | }; |
| 39 | headers["Accept"] = "text/event-stream"; |
| 40 | return headers; |
| 41 | } |
| 42 | |
| 43 | protected async _embed(chunks: string[]): Promise<number[][]> { |
| 44 | const resp = await this.fetch(NCompass.embeddingsApiEndpoint, { |
| 45 | method: "POST", |
| 46 | body: JSON.stringify({ |
| 47 | input: chunks, |
| 48 | model: this.model, |
| 49 | ...this.extraBodyProperties(), |
| 50 | }), |
| 51 | headers: { |
| 52 | Authorization: `Bearer ${this.apiKey}`, |
| 53 | "Content-Type": "application/json", |
| 54 | }, |
| 55 | }); |
| 56 | |
| 57 | if (!resp.ok) { |
| 58 | throw new Error(await resp.text()); |
| 59 | } |
| 60 | |
| 61 | const data = (await resp.json()) as any; |
| 62 | return data.data.map((result: { embedding: number[] }) => result.embedding); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | export default NCompass; |