Skip to content

Commit 58bc4cf

Browse files
authored
feat: allow syncing indexed dataset (#1714)
Fixes LS-3592
1 parent bfa9bd4 commit 58bc4cf

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

python/langsmith/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from langsmith.utils import ContextThreadPoolExecutor
2121

2222
# Avoid calling into importlib on every call to __version__
23-
__version__ = "0.3.41"
23+
__version__ = "0.3.42"
2424
version = __version__ # for backwards compatibility
2525

2626

python/langsmith/async_client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,32 @@ async def index_dataset(
11081108
)
11091109
ls_utils.raise_for_status_with_text(resp)
11101110

1111+
@ls_beta.warn_beta
1112+
async def sync_indexed_dataset(
1113+
self,
1114+
*,
1115+
dataset_id: ls_client.ID_TYPE,
1116+
**kwargs: Any,
1117+
) -> None:
1118+
"""Sync dataset index. This already happens automatically every 5 minutes, but you can call this to force a sync.
1119+
1120+
Args:
1121+
dataset_id (UUID): The ID of the dataset to sync.
1122+
1123+
Returns:
1124+
None
1125+
1126+
Raises:
1127+
requests.HTTPError
1128+
""" # noqa: E501
1129+
dataset_id = ls_client._as_uuid(dataset_id, "dataset_id")
1130+
resp = await self._arequest_with_retries(
1131+
"POST",
1132+
f"/datasets/{dataset_id}/index/sync",
1133+
content=ls_client._dumps_json({**kwargs}),
1134+
)
1135+
ls_utils.raise_for_status_with_text(resp)
1136+
11111137
@ls_beta.warn_beta
11121138
async def similar_examples(
11131139
self,

python/langsmith/client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4920,6 +4920,30 @@ def index_dataset(
49204920
)
49214921
ls_utils.raise_for_status_with_text(resp)
49224922

4923+
@warn_beta
4924+
def sync_indexed_dataset(
4925+
self,
4926+
*,
4927+
dataset_id: ID_TYPE,
4928+
**kwargs: Any,
4929+
) -> None:
4930+
"""Sync dataset index. This already happens automatically every 5 minutes, but you can call this to force a sync.
4931+
4932+
Args:
4933+
dataset_id (Union[UUID, str]): The ID of the dataset to sync.
4934+
4935+
Returns:
4936+
None
4937+
""" # noqa: E501
4938+
dataset_id = _as_uuid(dataset_id, "dataset_id")
4939+
resp = self.request_with_retries(
4940+
"POST",
4941+
f"/datasets/{dataset_id}/index/sync",
4942+
headers=self._headers,
4943+
data=json.dumps({**kwargs}),
4944+
)
4945+
ls_utils.raise_for_status_with_text(resp)
4946+
49234947
# NOTE: dataset_name arg explicitly not supported to avoid extra API calls.
49244948
@warn_beta
49254949
def similar_examples(

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "langsmith"
3-
version = "0.3.41"
3+
version = "0.3.42"
44
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
55
authors = ["LangChain <[email protected]>"]
66
license = "MIT"

python/tests/integration_tests/test_async_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ async def check_similar_examples():
4444
{"name": "Alice", "age": 30}, dataset_id=dataset.id, limit=1
4545
)
4646
assert examples[0].id == example.id
47+
48+
example2 = await client.create_example(
49+
inputs={"name": "Bobby", "age": 30},
50+
outputs={"hi": "there"},
51+
dataset_id=dataset.id,
52+
)
53+
54+
await client.sync_indexed_dataset(dataset_id=dataset.id)
55+
56+
async def check_similar_examples():
57+
examples = await client.similar_examples(
58+
{"name": "Bobby", "age": 30}, dataset_id=dataset.id, limit=2
59+
)
60+
return len(examples) == 2
61+
62+
await wait_for(check_similar_examples, timeout=20)
63+
examples = await client.similar_examples(
64+
{"name": "Bobby", "age": 30}, dataset_id=dataset.id, limit=2
65+
)
66+
assert examples[0].id == example2.id
67+
assert examples[1].id == example.id
4768
finally:
4869
if dataset:
4970
await client.delete_dataset(dataset_id=dataset.id)

python/tests/integration_tests/test_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ def test_similar_examples(langchain_client: Client) -> None:
389389
)
390390
assert len(similar_list) == 2
391391

392+
langchain_client.create_example(
393+
inputs={"text": "howdy"},
394+
outputs={"response": "howdy"},
395+
dataset_id=dataset.id,
396+
)
397+
398+
langchain_client.sync_indexed_dataset(dataset_id=dataset.id)
399+
time.sleep(5)
400+
401+
similar_list = langchain_client.similar_examples(
402+
{"text": "howdy"}, limit=5, dataset_id=dataset.id
403+
)
404+
assert len(similar_list) == 4
405+
392406
langchain_client.delete_dataset(dataset_id=dataset.id)
393407

394408

0 commit comments

Comments
 (0)