Skip to content

Commit

Permalink
Merge branch 'main' into manu/fix-tracing-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
manugoyal committed Jan 11, 2024
2 parents 5276abd + 215ba6b commit ba92333
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 15 deletions.
2 changes: 2 additions & 0 deletions js/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export interface EnvI {
OPENAI_API_KEY?: string;
OPENAI_BASE_URL?: string;
}

export const Env: EnvI = {
OPENAI_API_KEY: undefined,
OPENAI_BASE_URL: undefined,
};
22 changes: 13 additions & 9 deletions js/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function jsonDiff(
o2: any,
stringScorer: Scorer<string, {}>,
numberScorer: Scorer<number, {}>
): Promise<number> {
): Promise<number | null> {
if (isObject(o1) && isObject(o2)) {
if (Object.keys(o1).length == 0 && Object.keys(o2).length == 0) {
return 1;
Expand All @@ -45,20 +45,24 @@ async function jsonDiff(
)
);

const baseScores = await Promise.all(
allKeys.map((k) => jsonDiff(o1[k], o2[k], stringScorer, numberScorer))
);
const baseScores = (
await Promise.all(
allKeys.map((k) => jsonDiff(o1[k], o2[k], stringScorer, numberScorer))
)
).filter((s) => s !== null) as number[];
return baseScores.reduce((acc, s) => acc + s, 0) / baseScores.length;
} else if (isArray(o1) && isArray(o2)) {
if (o1.length === 0 && o2.length === 0) {
return 1;
}

const baseScores = await Promise.all(
Array.from({
length: Math.min(o1.length, o2.length),
}).map((_, i) => jsonDiff(o1[i], o2[i], stringScorer, numberScorer))
);
const baseScores = (
await Promise.all(
Array.from({
length: Math.min(o1.length, o2.length),
}).map((_, i) => jsonDiff(o1[i], o2[i], stringScorer, numberScorer))
)
).filter((s) => s !== null) as number[];
return (
baseScores.reduce((acc, s) => acc + s, 0) / Math.max(o1.length, o2.length)
);
Expand Down
1 change: 1 addition & 0 deletions js/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Env } from "./env.js";
Env.OPENAI_API_KEY = process.env.OPENAI_API_KEY;
Env.OPENAI_BASE_URL = process.env.OPENAI_BASE_URL;

export * from "./index.js";
2 changes: 1 addition & 1 deletion js/oai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function buildOpenAIClient(options: OpenAIAuth): OpenAI {
return new OpenAI({
apiKey: openAiApiKey || Env.OPENAI_API_KEY,
organization: openAiOrganizationId,
baseURL: openAiBaseUrl || PROXY_URL,
baseURL: openAiBaseUrl || Env.OPENAI_BASE_URL || PROXY_URL,
defaultHeaders: openAiDefaultHeaders,
dangerouslyAllowBrowser: openAiDangerouslyAllowBrowser,
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@braintrust/core": "^0.0.6",
"@braintrust/core": "^0.0.8",
"@types/node": "^20.10.5",
"compute-cosine-similarity": "^1.1.0",
"js-levenshtein": "^1.1.6",
Expand Down
2 changes: 2 additions & 0 deletions py/autoevals/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ def json_diff(self, o1, o2):

all_keys = set(o1.keys()).union(set(o2.keys()))
base_scores = [self.json_diff(o1.get(k), o2.get(k)) for k in all_keys]
base_scores = [s for s in base_scores if s is not None]
return sum(base_scores) / len(base_scores)
elif isinstance(o1, list) and isinstance(o2, list):
if len(o1) == 0 and len(o2) == 0:
return 1
base_scores = [self.json_diff(e1, e2) for (e1, e2) in zip(o1, o2)]
base_scores = [s for s in base_scores if s is not None]
return sum(base_scores) / max(len(o1), len(o2))
elif isinstance(o1, str) and isinstance(o2, str):
return self.string_scorer.eval(o1, o2).score
Expand Down
3 changes: 2 additions & 1 deletion py/autoevals/oai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import os
import sys
import textwrap
import time
Expand All @@ -18,7 +19,7 @@ class OpenAIWrapper:

def prepare_openai(is_async=False, api_key=None, base_url=None):
if base_url is None:
base_url = PROXY_URL
base_url = os.environ.get("OPENAI_BASE_URL", PROXY_URL)

try:
import openai
Expand Down
4 changes: 2 additions & 2 deletions py/autoevals/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ async def _run_eval_async(self, output, expected=None, **kwargs):
if expected is None:
raise ValueError("EmbeddingSimilarity requires an expected value")

output_embedding_p = arun_cached_request(input=f"{self.prefix}{output}", **self.extra_args)
expected_embedding_p = arun_cached_request(input=f"{self.prefix}{expected}", **self.extra_args)
output_embedding_p = arun_cached_request("embed", input=f"{self.prefix}{output}", **self.extra_args)
expected_embedding_p = arun_cached_request("embed", input=f"{self.prefix}{expected}", **self.extra_args)

output_result, expected_result = await output_embedding_p, await expected_embedding_p
return Score(
Expand Down
2 changes: 1 addition & 1 deletion py/autoevals/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.0.40"
VERSION = "0.0.43"

0 comments on commit ba92333

Please sign in to comment.