Skip to content

Commit

Permalink
[JS]: Add evaluate() API (#621)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: William Fu-Hinthorn <[email protected]>
  • Loading branch information
bracesproul and hinthornw authored Apr 30, 2024
1 parent 580584e commit d9ef5db
Show file tree
Hide file tree
Showing 17 changed files with 2,827 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"cSpell.words": ["atee"],
"python.testing.pytestArgs": ["python"],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.formatting.provider": "black",
"eslint.workingDirectories": [
"./js"
]
Expand Down
2 changes: 1 addition & 1 deletion js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ yarn add langchain
Tracing can be activated by setting the following environment variables or by manually specifying the LangChainTracer.

```typescript
process.env["LANGCHAIN_TRACING_V2"] = "true";
process.env["LANGSMITH_TRACING"] = "true";
process.env["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com";
process.env["LANGCHAIN_API_KEY"] = "<YOUR-LANGSMITH-API-KEY>";
// process.env["LANGCHAIN_PROJECT"] = "My Project Name"; // Optional: "default" is used if not set
Expand Down
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.1.21",
"version": "0.1.22",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"packageManager": "[email protected]",
"files": [
Expand Down Expand Up @@ -195,4 +195,4 @@
},
"./package.json": "./package.json"
}
}
}
73 changes: 63 additions & 10 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ import {
getRuntimeEnvironment,
} from "./utils/env.js";

import { RunEvaluator } from "./evaluation/evaluator.js";
import {
EvaluationResult,
EvaluationResults,
RunEvaluator,
} from "./evaluation/evaluator.js";
import { __version__ } from "./index.js";
import { assertUuid } from "./utils/_uuid.js";

interface ClientConfig {
apiUrl?: string;
Expand Down Expand Up @@ -152,7 +157,6 @@ interface ListRunsParams {
treeFilter?: string;
/**
* The values to include in the response.
*
*/
select?: string[];
}
Expand All @@ -174,14 +178,15 @@ interface feedback_source {

interface FeedbackCreate {
id: string;
run_id: string;
run_id: string | null;
key: string;
score?: ScoreType;
value?: ValueType;
correction?: object | null;
comment?: string | null;
feedback_source?: feedback_source | KVMap | null;
feedbackConfig?: FeedbackConfig;
session_id?: string;
}

interface FeedbackUpdate {
Expand Down Expand Up @@ -317,12 +322,6 @@ function trimQuotes(str?: string): string | undefined {
.replace(/^'(.*)'$/, "$1");
}

function assertUuid(str: string): void {
if (!uuid.validate(str)) {
throw new Error(`Invalid UUID: ${str}`);
}
}

const handle429 = async (response?: Response) => {
if (response?.status === 429) {
const retryAfter =
Expand Down Expand Up @@ -2193,7 +2192,7 @@ export class Client {
}

public async createFeedback(
runId: string,
runId: string | null,
key: string,
{
score,
Expand All @@ -2205,6 +2204,7 @@ export class Client {
sourceRunId,
feedbackId,
feedbackConfig,
projectId,
}: {
score?: ScoreType;
value?: ValueType;
Expand All @@ -2216,8 +2216,15 @@ export class Client {
sourceRunId?: string;
feedbackId?: string;
eager?: boolean;
projectId?: string;
}
): Promise<Feedback> {
if (!runId && !projectId) {
throw new Error("One of runId or projectId must be provided");
}
if (runId && projectId) {
throw new Error("Only one of runId or projectId can be provided");
}
const feedback_source: feedback_source = {
type: feedbackSourceType ?? "api",
metadata: sourceInfo ?? {},
Expand Down Expand Up @@ -2245,6 +2252,7 @@ export class Client {
comment,
feedback_source: feedback_source,
feedbackConfig,
session_id: projectId,
};
const url = `${this.apiUrl}/feedback`;
const response = await this.caller.call(fetch, url, {
Expand Down Expand Up @@ -2430,4 +2438,49 @@ export class Client {
yield* tokens;
}
}

_selectEvalResults(
results: EvaluationResult | EvaluationResults
): Array<EvaluationResult> {
let results_: Array<EvaluationResult>;
if ("results" in results) {
results_ = results.results;
} else {
results_ = [results];
}
return results_;
}

public async logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

Check warning on line 2457 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<EvaluationResult[]> {
const results: Array<EvaluationResult> =
this._selectEvalResults(evaluatorResponse);
for (const res of results) {
let sourceInfo_ = sourceInfo || {};
if (res.evaluatorInfo) {
sourceInfo_ = { ...res.evaluatorInfo, ...sourceInfo_ };
}
let runId_: string | null = null;
if (res.targetRunId) {
runId_ = res.targetRunId;
} else if (run) {
runId_ = run.id;
}

await this.createFeedback(runId_, res.key, {
score: res.score,
value: res.value,
comment: res.comment,
correction: res.correction,
sourceInfo: sourceInfo_,
sourceRunId: res.sourceRunId,
feedbackConfig: res.feedbackConfig as FeedbackConfig | undefined,
feedbackSourceType: "model",
});
}
return results;
}
}
Loading

0 comments on commit d9ef5db

Please sign in to comment.