Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use openai wrapper #52

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/embeddings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ test("Embeddings Test", async () => {
expect(result.score).toBeLessThan(0.5);
}
}
});
}, 600000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 minutes seems like a pretty long timeout for a unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as others -- it is just when the openai cache misses

50 changes: 13 additions & 37 deletions js/oai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { OpenAI } from "openai";

import { Env } from "./env";
import { currentSpanTraced, SpanLogFn } from "./util";

export interface CachedLLMParams {
model: string;
Expand Down Expand Up @@ -41,52 +40,29 @@ export function buildOpenAIClient(options: OpenAIAuth): OpenAI {
openAiDangerouslyAllowBrowser,
} = options;

return new OpenAI({
const client = new OpenAI({
apiKey: openAiApiKey || Env.OPENAI_API_KEY,
organization: openAiOrganizationId,
baseURL: openAiBaseUrl || Env.OPENAI_BASE_URL || PROXY_URL,
defaultHeaders: openAiDefaultHeaders,
dangerouslyAllowBrowser: openAiDangerouslyAllowBrowser,
});

if (globalThis.__inherited_braintrust_wrap_openai) {
return globalThis.__inherited_braintrust_wrap_openai(client);
} else {
return client;
}
}

declare global {
var __inherited_braintrust_wrap_openai: ((openai: any) => any) | undefined;
}

export async function cachedChatCompletion(
params: CachedLLMParams,
options: { cache?: ChatCache } & OpenAIAuth
): Promise<ChatCompletion> {
const { cache } = options;

return await currentSpanTraced(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! Can we delete this now from utils.ts?

async (spanLog: SpanLogFn) => {
let cached = false;
let ret = await cache?.get(params);
if (ret) {
cached = true;
} else {
const openai = buildOpenAIClient(options);
const completion = await openai.chat.completions.create(params);

await cache?.set(params, completion);
ret = completion;
}

const { messages, ...rest } = params;
spanLog({
input: messages,
metadata: {
...rest,
cached,
},
output: ret.choices[0],
metrics: {
tokens: ret.usage?.total_tokens,
prompt_tokens: ret.usage?.prompt_tokens,
completion_tokens: ret.usage?.completion_tokens,
},
});

return ret;
},
{ name: "OpenAI Completion" }
);
let openai = buildOpenAIClient(options);
return await openai.chat.completions.create(params);
}
33 changes: 1 addition & 32 deletions js/string.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Scorer } from "@braintrust/core";
import levenshtein from "js-levenshtein";
import { OpenAIAuth, buildOpenAIClient } from "./oai";
import { CreateEmbeddingResponse } from "openai/resources/embeddings";
import { SpanLogFn, currentSpanTraced } from "./util";
import { OpenAI } from "openai";
import cossim from "compute-cosine-similarity";

/**
Expand Down Expand Up @@ -76,7 +73,7 @@ export const EmbeddingSimilarity: Scorer<

const [outputResult, expectedResult] = await Promise.all(
[output, expected].map((input) =>
embed(openai, {
openai.embeddings.create({
input,
model: args.model ?? "text-embedding-ada-002",
})
Expand All @@ -103,31 +100,3 @@ Object.defineProperty(EmbeddingSimilarity, "name", {
function scaleScore(score: number, expectedMin: number): number {
return Math.max((score - expectedMin) / (1 - expectedMin), 0);
}

async function embed(
openai: OpenAI,
params: OpenAI.Embeddings.EmbeddingCreateParams
): Promise<CreateEmbeddingResponse> {
return await currentSpanTraced(
async (spanLog: SpanLogFn) => {
const result = await openai.embeddings.create(params);
const output = result.data[0].embedding;

const { input, ...rest } = params;
spanLog({
input,
output,
metadata: {
...rest,
},
metrics: {
tokens: result.usage?.total_tokens,
prompt_tokens: result.usage?.prompt_tokens,
},
});

return result;
},
{ name: "OpenAI Embedding" }
);
}
33 changes: 0 additions & 33 deletions js/util.ts

This file was deleted.

Loading