Skip to content

Commit

Permalink
[SDK] Add SearchItem (#2567)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Nov 28, 2024
1 parent 12486d9 commit ee8653d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
32 changes: 24 additions & 8 deletions libs/sdk-py/langgraph_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Dict,
Iterator,
List,
Literal,
Optional,
Sequence,
Union,
Expand Down Expand Up @@ -1946,7 +1947,7 @@ async def create(
Example Usage:
cron_run = await client.crons.create(
cron_run = client.crons.create(
assistant_id="agent",
schedule="27 15 * * *",
input={"messages": [{"role": "user", "content": "hello!"}]},
Expand Down Expand Up @@ -2070,14 +2071,20 @@ def __init__(self, http: HttpClient) -> None:
self.http = http

async def put_item(
self, namespace: Sequence[str], /, key: str, value: dict[str, Any]
self,
namespace: Sequence[str],
/,
key: str,
value: dict[str, Any],
index: Optional[Union[Literal[False], list[str]]] = None,
) -> None:
"""Store or update an item.
Args:
namespace: A list of strings representing the namespace path.
key: The unique identifier for the item within the namespace.
value: A dictionary containing the item's data.
index: Controls search indexing - None (use defaults), False (disable), or list of field paths to index.
Returns:
None
Expand All @@ -2095,11 +2102,7 @@ async def put_item(
raise ValueError(
f"Invalid namespace label '{label}'. Namespace labels cannot contain periods ('.')."
)
payload = {
"namespace": namespace,
"key": key,
"value": value,
}
payload = {"namespace": namespace, "key": key, "value": value, "index": index}
await self.http.put("/store/items", json=payload)

async def get_item(self, namespace: Sequence[str], /, key: str) -> Item:
Expand Down Expand Up @@ -2167,6 +2170,7 @@ async def search_items(
filter: Optional[dict[str, Any]] = None,
limit: int = 10,
offset: int = 0,
query: Optional[str] = None,
) -> SearchItemsResponse:
"""Search for items within a namespace prefix.
Expand All @@ -2175,6 +2179,7 @@ async def search_items(
filter: Optional dictionary of key-value pairs to filter results.
limit: Maximum number of items to return (default is 10).
offset: Number of items to skip before returning results (default is 0).
query: Optional query for natural language search.
Returns:
List[Item]: A list of items matching the search criteria.
Expand Down Expand Up @@ -2212,6 +2217,7 @@ async def search_items(
"filter": filter,
"limit": limit,
"offset": offset,
"query": query,
}

return await self.http.post("/store/items/search", json=_provided_vals(payload))
Expand Down Expand Up @@ -4154,14 +4160,20 @@ def __init__(self, http: SyncHttpClient) -> None:
self.http = http

def put_item(
self, namespace: Sequence[str], /, key: str, value: dict[str, Any]
self,
namespace: Sequence[str],
/,
key: str,
value: dict[str, Any],
index: Optional[Union[Literal[False], list[str]]] = None,
) -> None:
"""Store or update an item.
Args:
namespace: A list of strings representing the namespace path.
key: The unique identifier for the item within the namespace.
value: A dictionary containing the item's data.
index: Controls search indexing - None (use defaults), False (disable), or list of field paths to index.
Returns:
None
Expand All @@ -4183,6 +4195,7 @@ def put_item(
"namespace": namespace,
"key": key,
"value": value,
"index": index,
}
self.http.put("/store/items", json=payload)

Expand Down Expand Up @@ -4250,6 +4263,7 @@ def search_items(
filter: Optional[dict[str, Any]] = None,
limit: int = 10,
offset: int = 0,
query: Optional[str] = None,
) -> SearchItemsResponse:
"""Search for items within a namespace prefix.
Expand All @@ -4258,6 +4272,7 @@ def search_items(
filter: Optional dictionary of key-value pairs to filter results.
limit: Maximum number of items to return (default is 10).
offset: Number of items to skip before returning results (default is 0).
query: Optional query for natural language search.
Returns:
List[Item]: A list of items matching the search criteria.
Expand Down Expand Up @@ -4295,6 +4310,7 @@ def search_items(
"filter": filter,
"limit": limit,
"offset": offset,
"query": query,
}
return self.http.post("/store/items/search", json=_provided_vals(payload))

Expand Down
13 changes: 12 additions & 1 deletion libs/sdk-py/langgraph_sdk/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,21 @@ class ListNamespaceResponse(TypedDict):
"""A list of namespace paths, where each path is a list of strings."""


class SearchItem(Item, total=False):
"""Item with an optional relevance score from search operations.
Attributes:
score (Optional[float]): Relevance/similarity score. Included when
searching a compatible store with a natural language query.
"""

score: Optional[float]


class SearchItemsResponse(TypedDict):
"""Response structure for searching items."""

items: list[Item]
items: list[SearchItem]
"""A list of items matching the search criteria."""


Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langgraph-sdk"
version = "0.1.39"
version = "0.1.40"
description = "SDK for interacting with LangGraph API"
authors = []
license = "MIT"
Expand Down

0 comments on commit ee8653d

Please sign in to comment.