Elffuss BraidHub

kikocis/continue public

71fd2e7d4456 · 3.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 constructor(options: LLMOptions) {
32 super(options);
33 this.actualApiBase = options.apiBase;
34 this.apiKeyLocation = options.apiKeyLocation;
35 this.orgScopeId = options.orgScopeId;
36 this.onPremProxyUrl = options.onPremProxyUrl;
37 if (this.onPremProxyUrl) {
38 this.apiBase = new URL("model-proxy/v1/", this.onPremProxyUrl).toString();
39 }
40 }
41
42 static providerName = "continue-proxy";
43 static defaultOptions: Partial<LLMOptions> = {
44 useLegacyCompletionsEndpoint: false,
45 };
46
47 get underlyingProviderName(): string {
48 const { provider } = parseProxyModelName(this.model);
49 return provider;
50 }
51
52 protected extraBodyProperties(): Record<string, any> {
53 const continueProperties: ContinueProperties = {
54 apiKeyLocation: this.apiKeyLocation,
55 apiBase: this.actualApiBase,
56 orgScopeId: this.orgScopeId ?? null,
57 };
58 return {
59 continueProperties,
60 };
61 }
62
63 getConfigurationStatus() {
64 if (!this.apiKeyLocation) {
65 return LLMConfigurationStatuses.VALID;
66 }
67
68 const secretLocation = decodeSecretLocation(this.apiKeyLocation);
69 if (secretLocation.secretType === SecretType.NotFound) {
70 return LLMConfigurationStatuses.MISSING_API_KEY;
71 }
72
73 return LLMConfigurationStatuses.VALID;
74 }
75
76 protected _getHeaders() {
77 const headers: any = super._getHeaders();
78 headers["x-continue-unique-id"] = Telemetry.uniqueId;
79 return headers;
80 }
81
82 supportsCompletions(): boolean {
83 return false;
84 }
85
86 supportsFim(): boolean {
87 return true;
88 }
89
90 async rerank(query: string, chunks: Chunk[]): Promise<number[]> {
91 const url = new URL("rerank", this.apiBase);
92 const resp = await this.fetch(url, {
93 method: "POST",
94 headers: {
95 "Content-Type": "application/json",
96 Authorization: `Bearer ${this.apiKey}`,
97 },
98 body: JSON.stringify({
99 query,
100 documents: chunks.map((chunk) => chunk.content),
101 model: this.model,
102 ...this.extraBodyProperties(),
103 }),
104 });
105 const data: any = await resp.json();
106 const results = data.data.sort((a: any, b: any) => a.index - b.index);
107 return results.map((result: any) => result.relevance_score);
108 }
109}
110
111export default ContinueProxy;