Elffuss BraidHub

kikocis/continue public

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