| 1 | import { streamSse } from "@continuedev/fetch"; |
| 2 | import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; |
| 3 | |
| 4 | import { ChatCompletionCreateParams } from "@continuedev/openai-adapters"; |
| 5 | import { APPLY_UNIQUE_TOKEN } from "../../edit/constants.js"; |
| 6 | import { UNIQUE_TOKEN } from "../../nextEdit/constants.js"; |
| 7 | import OpenAI from "./OpenAI.js"; |
| 8 | |
| 9 | /** |
| 10 | * Inception Labs provider |
| 11 | * |
| 12 | * Integrates with Inception Labs' OpenAI-compatible API endpoints. |
| 13 | * Provides access to Mercury models for autocomplete and other tasks. |
| 14 | * |
| 15 | * Different models use different API endpoints: |
| 16 | * - mercury-editor-mini-experimental: zaragoza.api.inceptionlabs.ai |
| 17 | * - mercury-editor-small-experimental: copenhagen.api.inceptionlabs.ai |
| 18 | * |
| 19 | * More information at: https://docs.inceptionlabs.ai/ |
| 20 | */ |
| 21 | class Inception extends OpenAI { |
| 22 | static providerName = "inception"; |
| 23 | static defaultOptions: Partial<LLMOptions> = { |
| 24 | apiBase: "https://api.inceptionlabs.ai/v1/", |
| 25 | model: "mercury-coder-small", |
| 26 | completionOptions: { |
| 27 | temperature: 0.0, |
| 28 | presencePenalty: 1.5, |
| 29 | stop: ["<|endoftext|>"], |
| 30 | model: "mercury-editor-small-experimental", // Added model to fix TypeScript error |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | supportsFim(): boolean { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | // It seems like this should be inherited automatically from the parent OpenAI class, but it sometimes doesn't. |
| 39 | // protected useOpenAIAdapterFor: (LlmApiRequestType | "*")[] = [ |
| 40 | // "chat", |
| 41 | // "embed", |
| 42 | // "list", |
| 43 | // "rerank", |
| 44 | // "streamChat", |
| 45 | // "streamFim", |
| 46 | // ]; |
| 47 | |
| 48 | protected modifyChatBody( |
| 49 | body: ChatCompletionCreateParams, |
| 50 | ): ChatCompletionCreateParams { |
| 51 | const hasNextEditCapability = this.capabilities?.nextEdit ?? false; |
| 52 | |
| 53 | // Add the nextEdit parameter for Inception-specific routing. |
| 54 | (body as any).nextEdit = hasNextEditCapability; |
| 55 | return body; |
| 56 | } |
| 57 | |
| 58 | async *_streamFim( |
| 59 | prefix: string, |
| 60 | suffix: string, |
| 61 | signal: AbortSignal, |
| 62 | options: CompletionOptions, |
| 63 | ): AsyncGenerator<string> { |
| 64 | const endpoint = new URL("completions", this.apiBase); |
| 65 | const resp = await this.fetch(endpoint, { |
| 66 | method: "POST", |
| 67 | body: JSON.stringify({ |
| 68 | model: options.model, |
| 69 | prompt: prefix, |
| 70 | suffix: suffix.trim() === "" ? "<|endoftext|>" : suffix, |
| 71 | max_tokens: options.maxTokens ?? 150, // Only want this for /fim, not chat |
| 72 | temperature: options.temperature, |
| 73 | top_p: options.topP, |
| 74 | frequency_penalty: options.frequencyPenalty, |
| 75 | presence_penalty: options.presencePenalty, |
| 76 | stop: [...(options.stop ?? []), "\n\n", "\n \n"], |
| 77 | stream: true, |
| 78 | }), |
| 79 | headers: { |
| 80 | "Content-Type": "application/json", |
| 81 | Accept: "application/json", |
| 82 | Authorization: `Bearer ${this.apiKey}`, |
| 83 | }, |
| 84 | signal, |
| 85 | }); |
| 86 | for await (const chunk of streamSse(resp)) { |
| 87 | if (!chunk.choices[0]) { |
| 88 | continue; |
| 89 | } |
| 90 | yield chunk.choices[0].text; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | protected async *_streamChat( |
| 95 | messages: ChatMessage[], |
| 96 | signal: AbortSignal, |
| 97 | options: CompletionOptions, |
| 98 | ): AsyncGenerator<ChatMessage> { |
| 99 | if (this.isNextEdit(messages)) { |
| 100 | messages = this.removeToken(messages, UNIQUE_TOKEN); |
| 101 | yield* this.streamSpecialEndpoint( |
| 102 | "edit/completions", |
| 103 | messages, |
| 104 | signal, |
| 105 | options, |
| 106 | ); |
| 107 | } else if (this.isApply(messages)) { |
| 108 | messages = this.removeToken(messages, APPLY_UNIQUE_TOKEN); |
| 109 | yield* this.streamSpecialEndpoint( |
| 110 | "apply/completions", |
| 111 | messages, |
| 112 | signal, |
| 113 | options, |
| 114 | ); |
| 115 | } else { |
| 116 | // Use regular chat/completions endpoint - call parent OpenAI implementation. |
| 117 | yield* super._streamChat(messages, signal, options); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private isNextEdit(messages: ChatMessage[]): boolean { |
| 122 | // Check if any message contains the unique next edit token. |
| 123 | return messages.some( |
| 124 | (message) => |
| 125 | typeof message.content === "string" && |
| 126 | message.content.endsWith(UNIQUE_TOKEN), |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | private isApply(messages: ChatMessage[]): boolean { |
| 131 | return messages.some( |
| 132 | (message) => |
| 133 | typeof message.content === "string" && |
| 134 | message.content.endsWith(APPLY_UNIQUE_TOKEN), |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | private removeToken(messages: ChatMessage[], token: string): ChatMessage[] { |
| 139 | const lastMessage = messages[messages.length - 1]; |
| 140 | |
| 141 | if ( |
| 142 | typeof lastMessage?.content === "string" && |
| 143 | lastMessage.content.endsWith(token) |
| 144 | ) { |
| 145 | const cleanedMessages = [...messages]; |
| 146 | cleanedMessages[cleanedMessages.length - 1] = { |
| 147 | ...lastMessage, |
| 148 | content: lastMessage.content.slice(0, -token.length), |
| 149 | }; |
| 150 | return cleanedMessages; |
| 151 | } |
| 152 | |
| 153 | return messages; |
| 154 | } |
| 155 | |
| 156 | private async *streamSpecialEndpoint( |
| 157 | path: string, |
| 158 | messages: ChatMessage[], |
| 159 | signal: AbortSignal, |
| 160 | options: CompletionOptions, |
| 161 | ): AsyncGenerator<ChatMessage> { |
| 162 | const endpoint = new URL(path, this.apiBase); |
| 163 | |
| 164 | const resp = await this.fetch(endpoint, { |
| 165 | method: "POST", |
| 166 | body: JSON.stringify({ |
| 167 | model: options.model, |
| 168 | messages, |
| 169 | max_tokens: options.maxTokens, |
| 170 | temperature: options.temperature, |
| 171 | top_p: options.topP, |
| 172 | frequency_penalty: options.frequencyPenalty, |
| 173 | presence_penalty: options.presencePenalty, |
| 174 | stop: options.stop, |
| 175 | stream: true, |
| 176 | }), |
| 177 | headers: { |
| 178 | "Content-Type": "application/json", |
| 179 | Accept: "application/json", |
| 180 | Authorization: `Bearer ${this.apiKey}`, |
| 181 | }, |
| 182 | signal, |
| 183 | }); |
| 184 | |
| 185 | for await (const chunk of streamSse(resp)) { |
| 186 | const content = chunk.choices?.[0]?.delta?.content; |
| 187 | if (content) { |
| 188 | yield { role: "assistant", content }; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | export default Inception; |