Skip to content

Commit

Permalink
feat: metadata filters for few shot (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakerachleff authored Sep 5, 2024
1 parent e78f394 commit daf3368
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
14 changes: 13 additions & 1 deletion python/langsmith/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ async def similar_examples(
*,
limit: int,
dataset_id: ls_client.ID_TYPE,
filter: Optional[str] = None,
**kwargs: Any,
) -> List[ls_schemas.ExampleSearch]:
r"""Retrieve the dataset examples whose inputs best match the current inputs.
Expand All @@ -853,6 +854,9 @@ async def similar_examples(
input schema. Must be JSON serializable.
limit (int): The maximum number of examples to return.
dataset_id (str or UUID): The ID of the dataset to search over.
filter (str, optional): A filter string to apply to the search results. Uses
the same syntax as the `filter` parameter in `list_runs()`. Only a subset
of operations are supported. Defaults to None.
kwargs (Any): Additional keyword args to pass as part of request body.
Returns:
Expand Down Expand Up @@ -898,10 +902,18 @@ async def similar_examples(
""" # noqa: E501
dataset_id = ls_client._as_uuid(dataset_id, "dataset_id")
req = {
"inputs": inputs,
"limit": limit,
**kwargs,
}
if filter:
req["filter"] = filter

resp = await self._arequest_with_retries(
"POST",
f"/datasets/{dataset_id}/search",
content=ls_client._dumps_json({"inputs": inputs, "limit": limit, **kwargs}),
content=ls_client._dumps_json(req),
)
ls_utils.raise_for_status_with_text(resp)
examples = []
Expand Down
17 changes: 16 additions & 1 deletion python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3488,6 +3488,7 @@ def similar_examples(
*,
limit: int,
dataset_id: ID_TYPE,
filter: Optional[str] = None,
**kwargs: Any,
) -> List[ls_schemas.ExampleSearch]:
r"""Retrieve the dataset examples whose inputs best match the current inputs.
Expand All @@ -3500,6 +3501,12 @@ def similar_examples(
input schema. Must be JSON serializable.
limit (int): The maximum number of examples to return.
dataset_id (str or UUID): The ID of the dataset to search over.
filter (str, optional): A filter string to apply to the search results. Uses
the same syntax as the `filter` parameter in `list_runs()`. Only a subset
of operations are supported. Defaults to None.
For example, you can use `and(eq(metadata.some_tag, 'some_value'), neq(metadata.env, 'dev'))`
to filter only examples where some_tag has some_value, and the environment is not dev.
kwargs (Any): Additional keyword args to pass as part of request body.
Returns:
Expand Down Expand Up @@ -3545,11 +3552,19 @@ def similar_examples(
""" # noqa: E501
dataset_id = _as_uuid(dataset_id, "dataset_id")
req = {
"inputs": inputs,
"limit": limit,
**kwargs,
}
if filter is not None:
req["filter"] = filter

resp = self.request_with_retries(
"POST",
f"/datasets/{dataset_id}/search",
headers=self._headers,
data=json.dumps({"inputs": inputs, "limit": limit, **kwargs}),
data=json.dumps(req),
)
ls_utils.raise_for_status_with_text(resp)
examples = []
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langsmith"
version = "0.1.113"
version = "0.1.114"
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
authors = ["LangChain <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit daf3368

Please sign in to comment.