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

Update list examples #827

Merged
merged 3 commits into from
Jul 2, 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
24 changes: 23 additions & 1 deletion js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,9 @@
splits,
inlineS3Urls,
metadata,
limit,
offset,
filter,
}: {
datasetId?: string;
datasetName?: string;
Expand All @@ -2191,6 +2194,9 @@
splits?: string[];
inlineS3Urls?: boolean;
metadata?: KVMap;
limit?: number;
offset?: number;
filter?: string;
} = {}): AsyncIterable<Example> {
let datasetId_;
if (datasetId !== undefined && datasetName !== undefined) {
Expand Down Expand Up @@ -2228,11 +2234,27 @@
const serializedMetadata = JSON.stringify(metadata);
params.append("metadata", serializedMetadata);
}
if (limit !== undefined) {
params.append("limit", limit.toString());
}
if (offset !== undefined) {
params.append("offset", offset.toString());
}
if (filter !== undefined) {
params.append("filter", filter);
}
let i = 0;
for await (const examples of this._getPaginated<Example>(
"/examples",
params
)) {
yield* examples;
for (const example of examples) {
yield example;
i++;
}
if (limit !== undefined && i >= limit) {
break;
}
}
}

Expand Down Expand Up @@ -2313,7 +2335,7 @@
}

const feedbackResult = await evaluator.evaluateRun(run_, referenceExample);
const [_, feedbacks] = await this._logEvaluationFeedback(

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

View workflow job for this annotation

GitHub Actions / Check linting

'_' is assigned a value but never used
feedbackResult,
run_,
sourceInfo
Expand Down Expand Up @@ -2647,7 +2669,7 @@
async _logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<[results: EvaluationResult[], feedbacks: Feedback[]]> {
const evalResults: Array<EvaluationResult> =
this._selectEvalResults(evaluatorResponse);
Expand Down Expand Up @@ -2686,7 +2708,7 @@
public async logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

Check warning on line 2711 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] = await this._logEvaluationFeedback(
evaluatorResponse,
Expand Down
44 changes: 44 additions & 0 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,22 @@ test.concurrent(
client.listExamples({ datasetId: dataset.id })
);
expect(examplesList.length).toEqual(4);

const examplesListLimited = await toArray(
client.listExamples({ datasetId: dataset.id, limit: 2 })
);
expect(examplesListLimited.length).toEqual(2);

const examplesListOffset = await toArray(
client.listExamples({ datasetId: dataset.id, offset: 2 })
);
expect(examplesListOffset.length).toEqual(2);

const examplesListLimitedOffset = await toArray(
client.listExamples({ datasetId: dataset.id, limit: 1, offset: 2 })
);
expect(examplesListLimitedOffset.length).toEqual(1);

await client.deleteExample(example.id);
const examplesList2 = await toArray(
client.listExamples({ datasetId: dataset.id })
Expand Down Expand Up @@ -583,6 +599,34 @@ test.concurrent(
expect(examplesList3[0].metadata?.foo).toEqual("bar");
expect(examplesList3[0].metadata?.baz).toEqual("qux");

examplesList3 = await toArray(
client.listExamples({
datasetId: dataset.id,
filter: 'exists(metadata, "baz")',
})
);
expect(examplesList3.length).toEqual(1);
expect(examplesList3[0].metadata?.foo).toEqual("bar");
expect(examplesList3[0].metadata?.baz).toEqual("qux");

examplesList3 = await toArray(
client.listExamples({
datasetId: dataset.id,
filter: 'has("metadata", \'{"foo": "bar"}\')',
})
);
expect(examplesList3.length).toEqual(1);
expect(examplesList3[0].metadata?.foo).toEqual("bar");
expect(examplesList3[0].metadata?.baz).toEqual("qux");

examplesList3 = await toArray(
client.listExamples({
datasetId: dataset.id,
filter: 'exists(metadata, "bazzz")',
})
);
expect(examplesList3.length).toEqual(0);

examplesList3 = await toArray(
client.listExamples({
datasetId: dataset.id,
Expand Down
8 changes: 8 additions & 0 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3203,8 +3203,11 @@ def list_examples(
as_of: Optional[Union[datetime.datetime, str]] = None,
splits: Optional[Sequence[str]] = None,
inline_s3_urls: bool = True,
*,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could move this further up... realized we want these keyword only

offset: int = 0,
limit: Optional[int] = None,
metadata: Optional[dict] = None,
filter: Optional[str] = None,
**kwargs: Any,
) -> Iterator[ls_schemas.Example]:
"""Retrieve the example rows of the specified dataset.
Expand All @@ -3225,20 +3228,25 @@ def list_examples(
Returns examples only from the specified splits.
inline_s3_urls (bool, optional): Whether to inline S3 URLs.
Defaults to True.
offset (int): The offset to start from. Defaults to 0.
limit (int, optional): The maximum number of examples to return.
filter (str, optional): A structured fileter string to apply to
the examples.

Yields:
Example: The examples.
"""
params: Dict[str, Any] = {
**kwargs,
"offset": offset,
"id": example_ids,
"as_of": (
as_of.isoformat() if isinstance(as_of, datetime.datetime) else as_of
),
"splits": splits,
"inline_s3_urls": inline_s3_urls,
"limit": min(limit, 100) if limit is not None else 100,
"filter": filter,
}
if metadata is not None:
params["metadata"] = _dumps_json(metadata)
Expand Down
29 changes: 29 additions & 0 deletions python/tests/integration_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ def test_list_examples(langchain_client: Client) -> None:
example_list = list(langchain_client.list_examples(dataset_id=dataset.id))
assert len(example_list) == len(examples)

example_list = list(
langchain_client.list_examples(dataset_id=dataset.id, offset=1, limit=2)
)
assert len(example_list) == 2

example_list = list(langchain_client.list_examples(dataset_id=dataset.id, offset=1))
assert len(example_list) == len(examples) - 1

example_list = list(
langchain_client.list_examples(dataset_id=dataset.id, splits=["train"])
)
Expand Down Expand Up @@ -202,6 +210,27 @@ def test_list_examples(langchain_client: Client) -> None:
)
assert len(example_list) == 0

example_list = list(
langchain_client.list_examples(
dataset_id=dataset.id, filter='exists(metadata, "baz")'
)
)
assert len(example_list) == 1

example_list = list(
langchain_client.list_examples(
dataset_id=dataset.id, filter='has("metadata", \'{"foo": "bar"}\')'
)
)
assert len(example_list) == 1

example_list = list(
langchain_client.list_examples(
dataset_id=dataset.id, filter='exists(metadata, "bazzz")'
)
)
assert len(example_list) == 0

langchain_client.delete_dataset(dataset_id=dataset.id)


Expand Down
Loading