diff --git a/python/langsmith/async_client.py b/python/langsmith/async_client.py index 4e1e2f9aa..faa5cf901 100644 --- a/python/langsmith/async_client.py +++ b/python/langsmith/async_client.py @@ -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. @@ -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: @@ -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 = [] diff --git a/python/langsmith/client.py b/python/langsmith/client.py index 11627143a..3e398c808 100644 --- a/python/langsmith/client.py +++ b/python/langsmith/client.py @@ -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. @@ -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: @@ -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 = [] diff --git a/python/pyproject.toml b/python/pyproject.toml index 66b8e5262..f860e587b 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -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 "] license = "MIT"