Elffuss BraidHub

kikocis/continue public

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