Elffuss BraidHub

kikocis/continue public

a36aa2a5e5e6 · 4.1 KiBRawPermalink
1import {
2 ContinueProperties,
3 decodeSecretLocation,
4 parseProxyModelName,
5 SecretType,
6} from "@continuedev/config-yaml";
7
8import { ControlPlaneProxyInfo } from "../../../control-plane/analytics/IAnalyticsProvider.js";
9import { Telemetry } from "../../../util/posthog.js";
10import OpenAI from "../OpenAI.js";
11
12import type { Chunk, LLMOptions } from "../../../index.js";
13import { LLMConfigurationStatuses } from "../../constants.js";
14
15class ContinueProxy extends OpenAI {
16 set controlPlaneProxyInfo(value: ControlPlaneProxyInfo) {
17 this.apiKey = value.workOsAccessToken;
18 if (!this.onPremProxyUrl) {
19 this.apiBase = new URL(
20 "model-proxy/v1/",
21 value.controlPlaneProxyUrl,
22 ).toString();
23 }
24 }
25
26 // The apiKey and apiBase are set to the values for the proxy,
27 // but we need to keep track of the actual values that the proxy will use
28 // to call whatever LLM API is chosen
29 private actualApiBase?: string;
30
31 // Contains extra properties that we pass along to the proxy. Originally from `env` property on LLMOptions
32 private configEnv?: Record<string, any>;
33
34 constructor(options: LLMOptions) {
35 super(options);
36 this.configEnv = options.env;
37 // This it set to `undefined` to handle the case where we are proxying requests to Azure. We pass the correct env vars
38 // needed to do this in `extraBodyProperties` below, but if we don't set `apiType` to `undefined`, we end up proxying to
39 // `/openai/deployments/` which is invalid since that URL construction happens on the proxy.
40 this.apiType = undefined;
41 this.actualApiBase = options.apiBase;
42 this.apiKeyLocation = options.apiKeyLocation;
43 this.orgScopeId = options.orgScopeId;
44 this.onPremProxyUrl = options.onPremProxyUrl;
45 if (this.onPremProxyUrl) {
46 this.apiBase = new URL("model-proxy/v1/", this.onPremProxyUrl).toString();
47 }
48 }
49
50 static providerName = "continue-proxy";
51 static defaultOptions: Partial<LLMOptions> = {
52 useLegacyCompletionsEndpoint: false,
53 };
54
55 get underlyingProviderName(): string {
56 const { provider } = parseProxyModelName(this.model);
57 return provider;
58 }
59
60 protected extraBodyProperties(): Record<string, any> {
61 const continueProperties: ContinueProperties = {
62 apiKeyLocation: this.apiKeyLocation,
63 apiBase: this.actualApiBase,
64 orgScopeId: this.orgScopeId ?? null,
65 env: this.configEnv,
66 };
67 return {
68 continueProperties,
69 };
70 }
71
72 getConfigurationStatus() {
73 if (!this.apiKeyLocation) {
74 return LLMConfigurationStatuses.VALID;
75 }
76
77 const secretLocation = decodeSecretLocation(this.apiKeyLocation);
78 if (secretLocation.secretType === SecretType.NotFound) {
79 return LLMConfigurationStatuses.MISSING_API_KEY;
80 }
81
82 return LLMConfigurationStatuses.VALID;
83 }
84
85 protected _getHeaders() {
86 const headers: any = super._getHeaders();
87 headers["x-continue-unique-id"] = Telemetry.uniqueId;
88 return headers;
89 }
90
91 supportsCompletions(): boolean {
92 // This was a hotfix and contains duplicate logic from class-specific completion support methods
93 if (this.underlyingProviderName === "vllm") {
94 return true;
95 }
96 // other providers that don't support completions include groq, mistral, nvidia, deepseek, etc.
97 // For now disabling all except vllm
98 return false;
99 }
100
101 supportsFim(): boolean {
102 const { provider } = parseProxyModelName(this.model);
103 if (provider === "vllm") {
104 return false;
105 }
106 return true;
107 }
108
109 async rerank(query: string, chunks: Chunk[]): Promise<number[]> {
110 const url = new URL("rerank", this.apiBase);
111 const resp = await this.fetch(url, {
112 method: "POST",
113 headers: {
114 "Content-Type": "application/json",
115 Authorization: `Bearer ${this.apiKey}`,
116 },
117 body: JSON.stringify({
118 query,
119 documents: chunks.map((chunk) => chunk.content),
120 model: this.model,
121 ...this.extraBodyProperties(),
122 }),
123 });
124 const data: any = await resp.json();
125 const results = data.data.sort((a: any, b: any) => a.index - b.index);
126 return results.map((result: any) => result.relevance_score);
127 }
128}
129
130export default ContinueProxy;