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

fix example metadata support #564

Merged
merged 3 commits into from Mar 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/package.json
Expand Up @@ -173,4 +173,4 @@
},
"./package.json": "./package.json"
}
}
}
14 changes: 13 additions & 1 deletion js/src/client.ts
Expand Up @@ -218,6 +218,8 @@ export type CreateExampleOptions = {
datasetName?: string;
createdAt?: Date;
exampleId?: string;

metadata?: KVMap;
};

type AutoBatchQueueItem = {
Expand Down Expand Up @@ -1867,7 +1869,13 @@ export class Client {
public async createExample(
inputs: KVMap,
outputs: KVMap,
{ datasetId, datasetName, createdAt, exampleId }: CreateExampleOptions
hinthornw marked this conversation as resolved.
Show resolved Hide resolved
{
datasetId,
datasetName,
createdAt,
exampleId,
metadata,
}: CreateExampleOptions
): Promise<Example> {
let datasetId_ = datasetId;
if (datasetId_ === undefined && datasetName === undefined) {
Expand All @@ -1886,6 +1894,7 @@ export class Client {
outputs,
created_at: createdAt_?.toISOString(),
id: exampleId,
metadata,
};

const response = await this.caller.call(fetch, `${this.apiUrl}/examples`, {
Expand All @@ -1909,6 +1918,7 @@ export class Client {
public async createExamples(props: {
inputs: Array<KVMap>;
outputs?: Array<KVMap>;
metadata?: Array<KVMap>;
sourceRunIds?: Array<string>;
exampleIds?: Array<string>;
datasetId?: string;
Expand All @@ -1917,6 +1927,7 @@ export class Client {
const {
inputs,
outputs,
metadata,
sourceRunIds,
exampleIds,
datasetId,
Expand All @@ -1937,6 +1948,7 @@ export class Client {
dataset_id: datasetId_,
inputs: input,
outputs: outputs ? outputs[idx] : undefined,
metadata: metadata ? metadata[idx] : undefined,
id: exampleIds ? exampleIds[idx] : undefined,
source_run_id: sourceRunIds ? sourceRunIds[idx] : undefined,
};
Expand Down
2 changes: 2 additions & 0 deletions js/src/schemas.ts
Expand Up @@ -57,6 +57,7 @@ export interface BaseExample {
dataset_id: string;
inputs: KVMap;
outputs?: KVMap;
metadata?: KVMap;
}

/**
Expand Down Expand Up @@ -222,6 +223,7 @@ export interface ExampleUpdate {
dataset_id?: string;
inputs?: KVMap;
outputs?: KVMap;
metadata?: KVMap;
}
export interface BaseDataset {
name: string;
Expand Down
28 changes: 28 additions & 0 deletions js/src/tests/client.int.test.ts
Expand Up @@ -446,12 +446,22 @@ test.concurrent(
{ output: "hi there" },
{
datasetId: dataset.id,
metadata: { key: "value" },
}
);
const exampleValue = await client.readExample(example.id);
const initialVersion = exampleValue.modified_at;
expect(exampleValue.inputs.input).toEqual("hello world");
expect(exampleValue?.outputs?.output).toEqual("hi there");
expect(exampleValue?.metadata?.key).toEqual("value");

// Update the example by modifying the metadata
await client.updateExample(example.id, {
metadata: { key: "new value" },
});
const updatedExampleValue = await client.readExample(example.id);
expect(updatedExampleValue?.metadata?.key).toEqual("new value");

// Create multiple
await client.createExamples({
inputs: [
Expand All @@ -464,6 +474,7 @@ test.concurrent(
{ output: "hi there 2" },
{ output: "hi there 3" },
],
metadata: [{ key: "value 1" }, { key: "value 2" }, { key: "value 3" }],
datasetId: dataset.id,
});
const initialExamplesList = await toArray(
Expand All @@ -488,6 +499,23 @@ test.concurrent(
expect(datasetDiff.examples_modified.length).toEqual(0);
expect(datasetDiff.examples_removed.length).toEqual(1);

// verify the example inputs, outputs, and metadata
const example1 = examplesList2.find(
(e) => e.inputs.input === "hello world 1"
);
expect(example1?.outputs?.output).toEqual("hi there 1");
expect(example1?.metadata?.key).toEqual("value 1");
const example2 = examplesList2.find(
(e) => e.inputs.input === "hello world 2"
);
expect(example2?.outputs?.output).toEqual("hi there 2");
expect(example2?.metadata?.key).toEqual("value 2");
const example3 = examplesList2.find(
(e) => e.inputs.input === "hello world 3"
);
expect(example3?.outputs?.output).toEqual("hi there 3");
expect(example3?.metadata?.key).toEqual("value 3");

await client.deleteDataset({ datasetId: dataset.id });
},
180_000
Expand Down
2 changes: 2 additions & 0 deletions python/langsmith/client.py
Expand Up @@ -3047,6 +3047,8 @@ def update_example(
The input values to update.
outputs : Mapping[str, Any] or None, default=None
The output values to update.
metadata : Dict or None, default=None
The metadata to update.
dataset_id : UUID or None, default=None
The ID of the dataset to update.

Expand Down
1 change: 1 addition & 0 deletions python/langsmith/schemas.py
Expand Up @@ -104,6 +104,7 @@ class ExampleUpdate(BaseModel):
dataset_id: Optional[UUID] = None
inputs: Optional[Dict[str, Any]] = None
outputs: Optional[Dict[str, Any]] = None
metadata: Optional[Dict[str, Any]] = None

class Config:
"""Configuration class for the schema."""
Expand Down
2 changes: 2 additions & 0 deletions python/tests/integration_tests/test_client.py
Expand Up @@ -91,13 +91,15 @@ def test_datasets(langchain_client: Client) -> None:
example_id=example.id,
inputs={"col1": "updatedExampleCol1"},
outputs={"col2": "updatedExampleCol2"},
metadata={"foo": "bar"},
)
updated_example = langchain_client.read_example(example.id)
assert updated_example.id == example.id
updated_example_value = langchain_client.read_example(updated_example.id)
assert updated_example_value.inputs["col1"] == "updatedExampleCol1"
assert updated_example_value.outputs is not None
assert updated_example_value.outputs["col2"] == "updatedExampleCol2"
assert updated_example_value.metadata["foo"] == "bar"

langchain_client.delete_example(example.id)
examples2 = list(
Expand Down