Elffuss BraidHub

kikocis/continue public

e8ee99375f3e · 4.6 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.envSecretLocations = options.envSecretLocations;
44 this.orgScopeId = options.orgScopeId;
45 this.onPremProxyUrl = options.onPremProxyUrl;
46 if (this.onPremProxyUrl) {
47 this.apiBase = new URL("model-proxy/v1/", this.onPremProxyUrl).toString();
48 }
49 }
50
51 static providerName = "continue-proxy";
52 static defaultOptions: Partial<LLMOptions> = {
53 useLegacyCompletionsEndpoint: false,
54 };
55
56 get underlyingProviderName(): string {
57 const { provider } = parseProxyModelName(this.model);
58 return provider;
59 }
60
61 protected extraBodyProperties(): Record<string, any> {
62 const continueProperties: ContinueProperties = {
63 apiKeyLocation: this.apiKeyLocation,
64 envSecretLocations: this.envSecretLocations,
65 apiBase: this.actualApiBase,
66 orgScopeId: this.orgScopeId ?? null,
67 env: this.configEnv,
68 };
69 return {
70 continueProperties,
71 };
72 }
73
74 getConfigurationStatus() {
75 if (!this.apiKeyLocation && !this.envSecretLocations) {
76 return LLMConfigurationStatuses.VALID;
77 }
78
79 if (this.apiKeyLocation) {
80 const secretLocation = decodeSecretLocation(this.apiKeyLocation);
81 if (secretLocation.secretType === SecretType.NotFound) {
82 return LLMConfigurationStatuses.MISSING_API_KEY;
83 }
84 }
85
86 if (this.envSecretLocations) {
87 for (const secretLocation of Object.values(this.envSecretLocations)) {
88 const decoded = decodeSecretLocation(secretLocation);
89 if (decoded.secretType === SecretType.NotFound) {
90 return LLMConfigurationStatuses.MISSING_ENV_SECRET;
91 }
92 }
93 }
94
95 return LLMConfigurationStatuses.VALID;
96 }
97
98 protected _getHeaders() {
99 const headers: any = super._getHeaders();
100 headers["x-continue-unique-id"] = Telemetry.uniqueId;
101 return headers;
102 }
103
104 supportsCompletions(): boolean {
105 // This was a hotfix and contains duplicate logic from class-specific completion support methods
106 if (this.underlyingProviderName === "vllm") {
107 return true;
108 }
109 // other providers that don't support completions include groq, mistral, nvidia, deepseek, etc.
110 // For now disabling all except vllm
111 return false;
112 }
113
114 supportsFim(): boolean {
115 const { provider } = parseProxyModelName(this.model);
116 if (provider === "vllm") {
117 return false;
118 }
119 return true;
120 }
121
122 async rerank(query: string, chunks: Chunk[]): Promise<number[]> {
123 const url = new URL("rerank", this.apiBase);
124 const resp = await this.fetch(url, {
125 method: "POST",
126 headers: {
127 "Content-Type": "application/json",
128 Authorization: `Bearer ${this.apiKey}`,
129 },
130 body: JSON.stringify({
131 query,
132 documents: chunks.map((chunk) => chunk.content),
133 model: this.model,
134 ...this.extraBodyProperties(),
135 }),
136 });
137 const data: any = await resp.json();
138 const results = data.data.sort((a: any, b: any) => a.index - b.index);
139 return results.map((result: any) => result.relevance_score);
140 }
141}
142
143export default ContinueProxy;