feat: continue-proxy openai-adapter
This commit is contained in:
parent
ca8b46d95d
commit
768d375fcc
4 changed files with 192 additions and 26 deletions
120
packages/openai-adapters/src/apis/ContinueProxy.ts
Normal file
120
packages/openai-adapters/src/apis/ContinueProxy.ts
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
import { OpenAI } from "openai/index";
|
||||
import {
|
||||
ChatCompletionCreateParams,
|
||||
CompletionCreateParamsNonStreaming,
|
||||
CompletionCreateParamsStreaming,
|
||||
} from "openai/resources/index";
|
||||
import { z } from "zod";
|
||||
import { ContinueProxyConfigSchema } from "../types.js";
|
||||
import { FimCreateParamsStreaming, RerankCreateParams } from "./base.js";
|
||||
import { OpenAIApi } from "./OpenAI.js";
|
||||
|
||||
export interface ContinueProperties {
|
||||
apiKeyLocation?: string;
|
||||
envSecretLocations?: Record<string, string>;
|
||||
apiBase?: string;
|
||||
orgScopeId?: string | null;
|
||||
env?: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface ProxyModelName {
|
||||
ownerSlug: string;
|
||||
packageSlug: string;
|
||||
provider: string;
|
||||
model: string;
|
||||
}
|
||||
|
||||
export class ContinueProxyApi extends OpenAIApi {
|
||||
// The apiKey and apiBase are set to the values for the proxy,
|
||||
// but we need to keep track of the actual values that the proxy will use
|
||||
// to call whatever LLM API is chosen
|
||||
private actualApiBase?: string;
|
||||
|
||||
// Contains extra properties that we pass along to the proxy. Originally from `env` property on LLMOptions
|
||||
private configEnv?: Record<string, any>;
|
||||
|
||||
// Store the continue proxy config separately
|
||||
private continueProxyConfig: z.infer<typeof ContinueProxyConfigSchema>;
|
||||
|
||||
constructor(config: z.infer<typeof ContinueProxyConfigSchema>) {
|
||||
// Convert ContinueProxyConfigSchema to OpenAIConfigSchema format
|
||||
const openaiConfig = {
|
||||
provider: "openai" as const,
|
||||
apiKey: config.apiKey,
|
||||
apiBase: config.env?.proxyUrl
|
||||
? new URL("model-proxy/v1/", config.env?.proxyUrl).toString()
|
||||
: "https://api.continue.dev/model-proxy/v1/",
|
||||
requestOptions: config.requestOptions,
|
||||
};
|
||||
|
||||
super(openaiConfig);
|
||||
|
||||
this.continueProxyConfig = config;
|
||||
this.configEnv = config.env;
|
||||
this.actualApiBase = config.apiBase;
|
||||
}
|
||||
|
||||
protected extraBodyProperties(): Record<string, any> {
|
||||
const continueProperties: ContinueProperties = {
|
||||
apiKeyLocation: this.continueProxyConfig.env?.apiKeyLocation,
|
||||
envSecretLocations: this.continueProxyConfig.env?.envSecretLocations,
|
||||
apiBase: this.actualApiBase,
|
||||
orgScopeId: this.continueProxyConfig.env?.orgScopeId ?? null,
|
||||
env: this.configEnv,
|
||||
};
|
||||
return {
|
||||
continueProperties,
|
||||
};
|
||||
}
|
||||
|
||||
private modifyBodyWithContinueProperties<T extends Record<string, any>>(
|
||||
body: T,
|
||||
): T {
|
||||
return {
|
||||
...body,
|
||||
...this.extraBodyProperties(),
|
||||
};
|
||||
}
|
||||
|
||||
modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T {
|
||||
// First apply OpenAI-specific modifications
|
||||
const modifiedBody = super.modifyChatBody(body);
|
||||
// Then add Continue properties
|
||||
return this.modifyBodyWithContinueProperties(modifiedBody);
|
||||
}
|
||||
|
||||
modifyCompletionBody<
|
||||
T extends
|
||||
| CompletionCreateParamsNonStreaming
|
||||
| CompletionCreateParamsStreaming,
|
||||
>(body: T): T {
|
||||
return this.modifyBodyWithContinueProperties(body);
|
||||
}
|
||||
|
||||
modifyFimBody<T extends FimCreateParamsStreaming>(body: T): T {
|
||||
const modifiedBody = super.modifyFimBody(body);
|
||||
return this.modifyBodyWithContinueProperties(modifiedBody);
|
||||
}
|
||||
|
||||
protected getHeaders(): Record<string, string> {
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"x-api-key": this.continueProxyConfig.apiKey ?? "",
|
||||
Authorization: `Bearer ${this.continueProxyConfig.apiKey}`,
|
||||
};
|
||||
}
|
||||
|
||||
modifyEmbedBody<T extends OpenAI.Embeddings.EmbeddingCreateParams>(
|
||||
body: T,
|
||||
): T {
|
||||
return this.modifyBodyWithContinueProperties(body);
|
||||
}
|
||||
|
||||
modifyRerankBody<T extends RerankCreateParams>(body: T): T {
|
||||
return {
|
||||
...body,
|
||||
...this.extraBodyProperties(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -57,6 +57,37 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
return body;
|
||||
}
|
||||
|
||||
modifyCompletionBody<
|
||||
T extends
|
||||
| CompletionCreateParamsNonStreaming
|
||||
| CompletionCreateParamsStreaming,
|
||||
>(body: T): T {
|
||||
return body;
|
||||
}
|
||||
|
||||
modifyEmbedBody<T extends OpenAI.Embeddings.EmbeddingCreateParams>(
|
||||
body: T,
|
||||
): T {
|
||||
return body;
|
||||
}
|
||||
|
||||
modifyFimBody<T extends FimCreateParamsStreaming>(body: T): T {
|
||||
return body;
|
||||
}
|
||||
|
||||
modifyRerankBody<T extends RerankCreateParams>(body: T): T {
|
||||
return body;
|
||||
}
|
||||
|
||||
protected getHeaders(): Record<string, string> {
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"x-api-key": this.config.apiKey ?? "",
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
};
|
||||
}
|
||||
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
|
|
@ -88,14 +119,20 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
body: CompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<Completion> {
|
||||
const response = await this.openai.completions.create(body, { signal });
|
||||
const response = await this.openai.completions.create(
|
||||
this.modifyCompletionBody(body),
|
||||
{ signal },
|
||||
);
|
||||
return response;
|
||||
}
|
||||
async *completionStream(
|
||||
body: CompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<Completion, any, unknown> {
|
||||
const response = await this.openai.completions.create(body, { signal });
|
||||
const response = await this.openai.completions.create(
|
||||
this.modifyCompletionBody(body),
|
||||
{ signal },
|
||||
);
|
||||
for await (const result of response) {
|
||||
yield result;
|
||||
}
|
||||
|
|
@ -105,27 +142,23 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const endpoint = new URL("fim/completions", this.apiBase);
|
||||
const modifiedBody = this.modifyFimBody(body);
|
||||
const resp = await customFetch(this.config.requestOptions)(endpoint, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
model: body.model,
|
||||
prompt: body.prompt,
|
||||
suffix: body.suffix,
|
||||
max_tokens: body.max_tokens,
|
||||
max_completion_tokens: (body as any).max_completion_tokens,
|
||||
temperature: body.temperature,
|
||||
top_p: body.top_p,
|
||||
frequency_penalty: body.frequency_penalty,
|
||||
presence_penalty: body.presence_penalty,
|
||||
stop: body.stop,
|
||||
model: modifiedBody.model,
|
||||
prompt: modifiedBody.prompt,
|
||||
suffix: modifiedBody.suffix,
|
||||
max_tokens: modifiedBody.max_tokens,
|
||||
max_completion_tokens: (modifiedBody as any).max_completion_tokens,
|
||||
temperature: modifiedBody.temperature,
|
||||
top_p: modifiedBody.top_p,
|
||||
frequency_penalty: modifiedBody.frequency_penalty,
|
||||
presence_penalty: modifiedBody.presence_penalty,
|
||||
stop: modifiedBody.stop,
|
||||
stream: true,
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"x-api-key": this.config.apiKey ?? "",
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
},
|
||||
headers: this.getHeaders(),
|
||||
signal,
|
||||
});
|
||||
for await (const chunk of streamSse(resp as any)) {
|
||||
|
|
@ -138,21 +171,19 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
async embed(
|
||||
body: OpenAI.Embeddings.EmbeddingCreateParams,
|
||||
): Promise<OpenAI.Embeddings.CreateEmbeddingResponse> {
|
||||
const response = await this.openai.embeddings.create(body);
|
||||
const response = await this.openai.embeddings.create(
|
||||
this.modifyEmbedBody(body),
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
async rerank(body: RerankCreateParams): Promise<CreateRerankResponse> {
|
||||
const endpoint = new URL("rerank", this.apiBase);
|
||||
const modifiedBody = this.modifyRerankBody(body);
|
||||
const response = await customFetch(this.config.requestOptions)(endpoint, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"x-api-key": this.config.apiKey ?? "",
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
},
|
||||
body: JSON.stringify(modifiedBody),
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
const data = await response.json();
|
||||
return data as any;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { AnthropicApi } from "./apis/Anthropic.js";
|
|||
import { AzureApi } from "./apis/Azure.js";
|
||||
import { BedrockApi } from "./apis/Bedrock.js";
|
||||
import { CohereApi } from "./apis/Cohere.js";
|
||||
import { ContinueProxyApi } from "./apis/ContinueProxy.js";
|
||||
import { DeepSeekApi } from "./apis/DeepSeek.js";
|
||||
import { GeminiApi } from "./apis/Gemini.js";
|
||||
import { InceptionApi } from "./apis/Inception.js";
|
||||
|
|
@ -57,6 +58,8 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
|
|||
return new WatsonXApi(config);
|
||||
case "llamastack":
|
||||
return new LlamastackApi(config);
|
||||
case "continue-proxy":
|
||||
return new ContinueProxyApi(config);
|
||||
case "x-ai":
|
||||
return openAICompatible("https://api.x.ai/v1/", config);
|
||||
case "voyage":
|
||||
|
|
|
|||
|
|
@ -91,6 +91,17 @@ export const LlamastackConfigSchema = OpenAIConfigSchema.extend({
|
|||
});
|
||||
export type LlamastackConfig = z.infer<typeof LlamastackConfigSchema>;
|
||||
|
||||
export const ContinueProxyConfigSchema = BasePlusConfig.extend({
|
||||
provider: z.literal("continue-proxy"),
|
||||
env: z.object({
|
||||
apiKeyLocation: z.string().optional(),
|
||||
envSecretLocations: z.record(z.string(), z.string()).optional(),
|
||||
orgScopeId: z.string().nullable(),
|
||||
proxyUrl: z.string().optional(),
|
||||
}),
|
||||
});
|
||||
export type ContinueProxyConfig = z.infer<typeof ContinueProxyConfigSchema>;
|
||||
|
||||
export const MockConfigSchema = BasePlusConfig.extend({
|
||||
provider: z.literal("mock"),
|
||||
});
|
||||
|
|
@ -169,5 +180,6 @@ export const LLMConfigSchema = z.discriminatedUnion("provider", [
|
|||
MockConfigSchema,
|
||||
InceptionConfigSchema,
|
||||
LlamastackConfigSchema,
|
||||
ContinueProxyConfigSchema,
|
||||
]);
|
||||
export type LLMConfig = z.infer<typeof LLMConfigSchema>;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue