Skip to content

Commit

Permalink
Add bulk example creation (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Jan 24, 2024
1 parent 2534cc6 commit df758b3
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 199 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.0.62",
"version": "0.0.63",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"files": [
"dist/",
Expand Down
61 changes: 59 additions & 2 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class Client {
this.apiKey = trimQuotes(config.apiKey ?? defaultConfig.apiKey);
this.webUrl = trimQuotes(config.webUrl ?? defaultConfig.webUrl);
this.validateApiKeyIfHosted();
this.timeout_ms = config.timeout_ms ?? 4000;
this.timeout_ms = config.timeout_ms ?? 12_000;
this.caller = new AsyncCaller(config.callerOptions ?? {});
}

Expand Down Expand Up @@ -1167,7 +1167,7 @@ export class Client {
dataset_id: datasetId_,
inputs,
outputs,
created_at: createdAt_.toISOString(),
created_at: createdAt_?.toISOString(),
id: exampleId,
};

Expand All @@ -1188,6 +1188,63 @@ export class Client {
return result as Example;
}

public async createExamples(props: {
inputs: Array<KVMap>;
outputs?: Array<KVMap>;
sourceRunIds?: Array<string>;
exampleIds?: Array<string>;
datasetId?: string;
datasetName?: string;
}): Promise<Example[]> {
const {
inputs,
outputs,
sourceRunIds,
exampleIds,
datasetId,
datasetName,
} = props;
let datasetId_ = datasetId;
if (datasetId_ === undefined && datasetName === undefined) {
throw new Error("Must provide either datasetName or datasetId");
} else if (datasetId_ !== undefined && datasetName !== undefined) {
throw new Error("Must provide either datasetName or datasetId, not both");
} else if (datasetId_ === undefined) {
const dataset = await this.readDataset({ datasetName });
datasetId_ = dataset.id;
}

const formattedExamples = inputs.map((input, idx) => {
return {
dataset_id: datasetId_,
inputs: input,
outputs: outputs ? outputs[idx] : undefined,
id: exampleIds ? exampleIds[idx] : undefined,
source_run_id: sourceRunIds ? sourceRunIds[idx] : undefined,
};
});

const response = await this.caller.call(
fetch,
`${this.apiUrl}/examples/bulk`,
{
method: "POST",
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(formattedExamples),
signal: AbortSignal.timeout(this.timeout_ms),
}
);

if (!response.ok) {
throw new Error(
`Failed to create examples: ${response.status} ${response.statusText}`
);
}

const result = await response.json();
return result as Example[];
}

public async createLLMExample(
input: string,
generation: string | undefined,
Expand Down
2 changes: 1 addition & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export { Dataset, Example, TracerSession, Run, Feedback } from "./schemas.js";
export { RunTree, RunTreeConfig } from "./run_trees.js";

// Update using yarn bump-version
export const __version__ = "0.0.62";
export const __version__ = "0.0.63";
2 changes: 1 addition & 1 deletion js/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export interface RunUpdate {

export interface ExampleCreate extends BaseExample {
id?: string;
created_at: string;
created_at?: string;
}

export interface Example extends BaseExample {
Expand Down
Loading

0 comments on commit df758b3

Please sign in to comment.