adjust for on-prem proxy

This commit is contained in:
Nate 2025-01-20 14:00:15 -08:00
parent cbdc79e601
commit a219ab58ef
6 changed files with 61 additions and 21 deletions

4
core/index.d.ts vendored
View file

@ -460,7 +460,7 @@ export interface LLMOptions {
writeLog?: (str: string) => Promise<void>;
llmRequestHook?: (model: string, prompt: string) => any;
apiKey?: string;
apiKeySecret?: string;
apiKeyLocation?: string;
aiGatewaySlug?: string;
apiBase?: string;
cacheBehavior?: CacheBehavior;
@ -896,7 +896,7 @@ export interface ModelDescription {
provider: string;
model: string;
apiKey?: string;
apiKeySecret?: string;
apiKeyLocation?: string;
apiBase?: string;
contextLength?: number;
maxStopWords?: number;

View file

@ -117,7 +117,7 @@ export abstract class BaseLLM implements ILLM {
writeLog?: (str: string) => Promise<void>;
llmRequestHook?: (model: string, prompt: string) => any;
apiKey?: string;
apiKeySecret?: string;
apiKeyLocation?: string;
apiBase?: string;
cacheBehavior?: CacheBehavior;
capabilities?: ModelCapability;
@ -195,7 +195,7 @@ export abstract class BaseLLM implements ILLM {
this.writeLog = options.writeLog;
this.llmRequestHook = options.llmRequestHook;
this.apiKey = options.apiKey;
this.apiKeySecret = options.apiKeySecret;
this.apiKeyLocation = options.apiKeyLocation;
this.aiGatewaySlug = options.aiGatewaySlug;
this.apiBase = options.apiBase;
this.cacheBehavior = options.cacheBehavior;

View file

@ -14,12 +14,11 @@ class ContinueProxy extends OpenAI {
// 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;
private actualApiKey?: string;
constructor(options: LLMOptions) {
super(options);
this.actualApiBase = options.apiBase;
this.actualApiKey = options.apiKey;
this.apiKeyLocation = options.apiKeyLocation;
}
static providerName = "continue-proxy";
@ -30,9 +29,8 @@ class ContinueProxy extends OpenAI {
protected extraBodyProperties(): Record<string, any> {
return {
continueProperties: {
apiKey: this.actualApiKey,
apiKeyLocation: this.apiKeyLocation,
apiBase: this.actualApiBase,
apiKeySecret: this.apiKeySecret,
},
};
}

View file

@ -87,8 +87,11 @@ function getUnrenderedSecretLocation(
const secretLocation = decodeSecretLocation(secretLocationEncoded);
return secretLocation;
} catch (e) {
// If it's templated but not a valid secret location, leave it be
// If it's a templated secret but not a valid secret location, leave it be
// in case on-prem proxy has the secret in an env variable
if (templateVars[0].startsWith("secrets.")) {
return undefined; // TODO
}
return undefined;
}
}

View file

@ -1,6 +1,10 @@
import * as fs from "fs";
import * as YAML from "yaml";
import { decodeSecretLocation, resolveSecretLocationInProxy } from "../dist";
import {
decodeSecretLocation,
encodeSecretLocation,
resolveSecretLocationInProxy,
} from "../dist";
import {
clientRender,
FQSN,
@ -24,11 +28,13 @@ describe("E2E Scenarios", () => {
};
const packageSecrets: Record<string, string> = {
ANTHROPIC_API_KEY: "sk-ant",
"test-org/assistant/ANTHROPIC_API_KEY": "sk-ant",
"test-org/models/GEMINI_API_KEY": "gemini-api-key",
};
const proxyEnvSecrets: Record<string, string> = {
ANTHROPIC_API_KEY: "sk-ant-env",
GEMINI_API_KEY: "gemini-api-key-env",
};
const localUserSecretStore: SecretStore = {
@ -66,7 +72,9 @@ describe("E2E Scenarios", () => {
secretLocation: SecretLocation,
): Promise<string | undefined> {
if (secretLocation.secretType === SecretType.Package) {
return packageSecrets[secretLocation.secretName];
return packageSecrets[
encodeSecretLocation(secretLocation).split(":")[1]
];
} else if (secretLocation.secretType === SecretType.User) {
return userSecrets[secretLocation.secretName];
} else {
@ -92,7 +100,7 @@ describe("E2E Scenarios", () => {
);
// Test that packages were correctly unrolled and params replaced
expect(unrolledConfig.models?.length).toBe(3);
expect(unrolledConfig.models?.length).toBe(4);
expect(unrolledConfig.models?.[0].apiKey).toBe(
"${{ secrets.test-org/assistant/OPENAI_API_KEY }}",
);
@ -100,6 +108,9 @@ describe("E2E Scenarios", () => {
expect(unrolledConfig.models?.[2].apiKey).toBe(
"${{ secrets.test-org/assistant/test-org/models/ANTHROPIC_API_KEY }}",
);
expect(unrolledConfig.models?.[3].apiKey).toBe(
"${{ secrets.test-org/assistant/test-org/models/GEMINI_API_KEY }}",
);
expect(unrolledConfig.rules?.length).toBe(3);
expect(unrolledConfig.docs?.[0].startUrl).toBe(
@ -113,7 +124,9 @@ describe("E2E Scenarios", () => {
);
// Test that user secrets were injected and others were changed to use proxy
const anthropicSecretLocation = "package:test-org/models/ANTHROPIC_API_KEY";
const anthropicSecretLocation =
"package:test-org/assistant/ANTHROPIC_API_KEY";
const geminiSecretLocation = "package:test-org/models/GEMINI_API_KEY";
expect(clientRendered.models?.[0].apiKey).toBe("sk-123");
expect(clientRendered.models?.[1].apiKey).toBe("sk-456");
expect(clientRendered.models?.[2].provider).toBe("continue-proxy");
@ -121,24 +134,45 @@ describe("E2E Scenarios", () => {
anthropicSecretLocation,
);
expect(clientRendered.models?.[2].apiKey).toBeUndefined();
expect(clientRendered.models?.[3].provider).toBe("continue-proxy");
expect((clientRendered.models?.[3] as any).apiKeyLocation).toBe(
geminiSecretLocation,
);
expect(clientRendered.models?.[3].apiKey).toBeUndefined();
// Test that proxy can correctly resolve secrets
const secretLocation = decodeSecretLocation(anthropicSecretLocation);
const decodedAnthropicSecretLocation = decodeSecretLocation(
anthropicSecretLocation,
);
const decodedGeminiSecretLocation =
decodeSecretLocation(geminiSecretLocation);
// With environment
const secretValue = await resolveSecretLocationInProxy(
secretLocation,
const antSecretValue = await resolveSecretLocationInProxy(
decodedAnthropicSecretLocation,
platformSecretStore,
environmentSecretStore,
);
expect(secretValue).toBe("sk-ant-env");
expect(antSecretValue).toBe("sk-ant-env");
const geminiSecretValue = await resolveSecretLocationInProxy(
decodedGeminiSecretLocation,
platformSecretStore,
environmentSecretStore,
);
expect(geminiSecretValue).toBe("gemini-api-key-env");
// Without environment
const secretValue2 = await resolveSecretLocationInProxy(
secretLocation,
const antSecretValue2 = await resolveSecretLocationInProxy(
decodedAnthropicSecretLocation,
platformSecretStore,
undefined,
);
expect(secretValue2).toBe("sk-ant");
expect(antSecretValue2).toBe("sk-ant");
const geminiSecretValue2 = await resolveSecretLocationInProxy(
decodedGeminiSecretLocation,
platformSecretStore,
undefined,
);
expect(geminiSecretValue2).toBe("gemini-api-key");
});
});

View file

@ -11,3 +11,8 @@ models:
provider: anthropic
model: claude-3-5-sonnet-latest
apiKey: ${{ secrets.ANTHROPIC_API_KEY }}
- name: gemini
provider: gemini
model: gemini
apiKey: ${{ secrets.GEMINI_API_KEY }}