Skip to content

Commit

Permalink
Accept temporary credentials for chat completions API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin1 committed Nov 2, 2024
1 parent b111ce9 commit ada85c8
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions packages/proxy/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ import { ExperimentLogPartialArgs } from "@braintrust/core";
import { MessageParam } from "@anthropic-ai/sdk/resources";
import { getCurrentUnixTimestamp, parseOpenAIStream } from "utils";
import { openAIChatCompletionToChatEvent } from "./providers/openai";
import { makeTempCredentials } from "utils/tempCredentials";
import {
isTempCredential,
makeTempCredentials,
verifyTempCredentials,
} from "utils/tempCredentials";

type CachedData = {
headers: Record<string, string>;
Expand Down Expand Up @@ -370,9 +374,38 @@ export async function proxyV1({
bodyData,
setOverriddenHeader,
async (model) => {
// First, try to use temp credentials, because then we'll get access
// to the model.
let cachedAuthToken: string | undefined;
if (
useCredentialsCacheMode !== "never" &&
isTempCredential(authToken)
) {
const { credentialCacheValue, jwtPayload } =
await verifyTempCredentials({
jwt: authToken,
cacheGet,
});
// Unwrap the API key here to avoid a duplicate call to
// `verifyTempCredentials` inside `getApiSecrets`. That call will
// use Redis which is not available in Cloudflare.
cachedAuthToken = credentialCacheValue.authToken;
if (jwtPayload.bt.logging) {
console.warn(
`Logging was requested, but not supported on ${method} ${url}`,
);
}
if (jwtPayload.bt.model && jwtPayload.bt.model !== model) {
console.warn(
`Temp credential allows model "${jwtPayload.bt.model}", but "${model}" was requested`,
);
return [];
}
}

const secrets = await getApiSecrets(
useCredentialsCacheMode !== "never",
authToken,
cachedAuthToken || authToken,
model,
orgName,
);
Expand Down

0 comments on commit ada85c8

Please sign in to comment.