Skip to content

Commit

Permalink
Search sessions (#185)
Browse files Browse the repository at this point in the history
* SDK regeneration

* SDK regeneration

* chore: Add search multiple sessions example

* SDK regeneration

* chore: Update search sessions example signature

---------

Co-authored-by: fern-api <115122769+fern-api[bot]@users.noreply.github.com>
  • Loading branch information
paul-paliychuk and fern-api[bot] authored Jun 7, 2024
1 parent c866dbc commit a9e7928
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 5 deletions.
8 changes: 8 additions & 0 deletions examples/chat_history/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ async def main() -> None:
)
print("messages_result: ", messages_result)

user_messages_result = await client.memory.search_sessions(
limit=3,
user_id=user_id,
text=query,
search_scope="messages",
)
print("user_messages_result: ", user_messages_result)

# End session - this will trigger summarization and other background tasks on the completed session
print(f"\n5---end_session for Session: {session_id}")
await client.memory.end_session(session_id)
Expand Down
4 changes: 4 additions & 0 deletions src/zep_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
SearchType,
Session,
SessionListResponse,
SessionSearchResponse,
SessionSearchResult,
SuccessResponse,
Summary,
SummaryListResponse,
Expand Down Expand Up @@ -60,6 +62,8 @@
"SearchType",
"Session",
"SessionListResponse",
"SessionSearchResponse",
"SessionSearchResult",
"SuccessResponse",
"Summary",
"SummaryListResponse",
Expand Down
12 changes: 10 additions & 2 deletions src/zep_cloud/document/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ def add_documents(
)
client.document.add_documents(
collection_name="collectionName",
request=[CreateDocumentRequest()],
request=[
CreateDocumentRequest(
content="content",
)
],
)
"""
_response = self._client_wrapper.httpx_client.request(
Expand Down Expand Up @@ -1164,7 +1168,11 @@ async def add_documents(
)
await client.document.add_documents(
collection_name="collectionName",
request=[CreateDocumentRequest()],
request=[
CreateDocumentRequest(
content="content",
)
],
)
"""
_response = await self._client_wrapper.httpx_client.request(
Expand Down
173 changes: 173 additions & 0 deletions src/zep_cloud/memory/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..types.search_type import SearchType
from ..types.session import Session
from ..types.session_list_response import SessionListResponse
from ..types.session_search_response import SessionSearchResponse
from ..types.success_response import SuccessResponse
from ..types.summary_list_response import SummaryListResponse
from .types.memory_get_request_memory_type import MemoryGetRequestMemoryType
Expand Down Expand Up @@ -221,6 +222,92 @@ def end_sessions(
raise core_api_error_ApiError(status_code=_response.status_code, body=_response.text)
raise core_api_error_ApiError(status_code=_response.status_code, body=_response_json)

def search_sessions(
self,
*,
limit: typing.Optional[int] = None,
min_score: typing.Optional[float] = OMIT,
mmr_lambda: typing.Optional[float] = OMIT,
record_filter: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
search_scope: typing.Optional[SearchScope] = OMIT,
search_type: typing.Optional[SearchType] = OMIT,
session_ids: typing.Optional[typing.Sequence[str]] = OMIT,
text: typing.Optional[str] = OMIT,
user_id: typing.Optional[str] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> SessionSearchResponse:
"""
Search sessions for the specified query.
Parameters
----------
limit : typing.Optional[int]
The maximum number of search results to return. Defaults to None (no limit).
min_score : typing.Optional[float]
mmr_lambda : typing.Optional[float]
record_filter : typing.Optional[typing.Dict[str, typing.Any]]
filter on the metadata
search_scope : typing.Optional[SearchScope]
search_type : typing.Optional[SearchType]
session_ids : typing.Optional[typing.Sequence[str]]
the session ids to search
text : typing.Optional[str]
user_id : typing.Optional[str]
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
SessionSearchResponse
A SessionSearchResponse object representing the search results.
Examples
--------
from zep_cloud.client import Zep
client = Zep(
api_key="YOUR_API_KEY",
)
client.memory.search_sessions()
"""
_response = self._client_wrapper.httpx_client.request(
"sessions/search",
method="POST",
params={"limit": limit},
json={
"min_score": min_score,
"mmr_lambda": mmr_lambda,
"record_filter": record_filter,
"search_scope": search_scope,
"search_type": search_type,
"session_ids": session_ids,
"text": text,
"user_id": user_id,
},
request_options=request_options,
omit=OMIT,
)
if 200 <= _response.status_code < 300:
return pydantic_v1.parse_obj_as(SessionSearchResponse, _response.json()) # type: ignore
if _response.status_code == 500:
raise InternalServerError(
pydantic_v1.parse_obj_as(types_api_error_ApiError, _response.json()) # type: ignore
)
try:
_response_json = _response.json()
except JSONDecodeError:
raise core_api_error_ApiError(status_code=_response.status_code, body=_response.text)
raise core_api_error_ApiError(status_code=_response.status_code, body=_response_json)

def get_session(self, session_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Session:
"""
get session by id
Expand Down Expand Up @@ -1198,6 +1285,92 @@ async def end_sessions(
raise core_api_error_ApiError(status_code=_response.status_code, body=_response.text)
raise core_api_error_ApiError(status_code=_response.status_code, body=_response_json)

async def search_sessions(
self,
*,
limit: typing.Optional[int] = None,
min_score: typing.Optional[float] = OMIT,
mmr_lambda: typing.Optional[float] = OMIT,
record_filter: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
search_scope: typing.Optional[SearchScope] = OMIT,
search_type: typing.Optional[SearchType] = OMIT,
session_ids: typing.Optional[typing.Sequence[str]] = OMIT,
text: typing.Optional[str] = OMIT,
user_id: typing.Optional[str] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> SessionSearchResponse:
"""
Search sessions for the specified query.
Parameters
----------
limit : typing.Optional[int]
The maximum number of search results to return. Defaults to None (no limit).
min_score : typing.Optional[float]
mmr_lambda : typing.Optional[float]
record_filter : typing.Optional[typing.Dict[str, typing.Any]]
filter on the metadata
search_scope : typing.Optional[SearchScope]
search_type : typing.Optional[SearchType]
session_ids : typing.Optional[typing.Sequence[str]]
the session ids to search
text : typing.Optional[str]
user_id : typing.Optional[str]
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
SessionSearchResponse
A SessionSearchResponse object representing the search results.
Examples
--------
from zep_cloud.client import AsyncZep
client = AsyncZep(
api_key="YOUR_API_KEY",
)
await client.memory.search_sessions()
"""
_response = await self._client_wrapper.httpx_client.request(
"sessions/search",
method="POST",
params={"limit": limit},
json={
"min_score": min_score,
"mmr_lambda": mmr_lambda,
"record_filter": record_filter,
"search_scope": search_scope,
"search_type": search_type,
"session_ids": session_ids,
"text": text,
"user_id": user_id,
},
request_options=request_options,
omit=OMIT,
)
if 200 <= _response.status_code < 300:
return pydantic_v1.parse_obj_as(SessionSearchResponse, _response.json()) # type: ignore
if _response.status_code == 500:
raise InternalServerError(
pydantic_v1.parse_obj_as(types_api_error_ApiError, _response.json()) # type: ignore
)
try:
_response_json = _response.json()
except JSONDecodeError:
raise core_api_error_ApiError(status_code=_response.status_code, body=_response.text)
raise core_api_error_ApiError(status_code=_response.status_code, body=_response_json)

async def get_session(self, session_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Session:
"""
get session by id
Expand Down
4 changes: 4 additions & 0 deletions src/zep_cloud/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from .search_type import SearchType
from .session import Session
from .session_list_response import SessionListResponse
from .session_search_response import SessionSearchResponse
from .session_search_result import SessionSearchResult
from .success_response import SuccessResponse
from .summary import Summary
from .summary_list_response import SummaryListResponse
Expand Down Expand Up @@ -48,6 +50,8 @@
"SearchType",
"Session",
"SessionListResponse",
"SessionSearchResponse",
"SessionSearchResult",
"SuccessResponse",
"Summary",
"SummaryListResponse",
Expand Down
3 changes: 1 addition & 2 deletions src/zep_cloud/types/create_document_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@


class CreateDocumentRequest(pydantic_v1.BaseModel):
content: typing.Optional[str] = None
content: str
document_id: typing.Optional[str] = None
embedding: typing.Optional[typing.List[float]] = None
metadata: typing.Optional[typing.Dict[str, typing.Any]] = None

def json(self, **kwargs: typing.Any) -> str:
Expand Down
1 change: 0 additions & 1 deletion src/zep_cloud/types/memory_search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class MemorySearchResult(pydantic_v1.BaseModel):
embedding: typing.Optional[typing.List[float]] = None
message: typing.Optional[Message] = None
metadata: typing.Optional[typing.Dict[str, typing.Any]] = None
score: typing.Optional[float] = None
Expand Down
30 changes: 30 additions & 0 deletions src/zep_cloud/types/session_search_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file was auto-generated by Fern from our API Definition.

import datetime as dt
import typing

from ..core.datetime_utils import serialize_datetime
from ..core.pydantic_utilities import deep_union_pydantic_dicts, pydantic_v1
from .session_search_result import SessionSearchResult


class SessionSearchResponse(pydantic_v1.BaseModel):
results: typing.Optional[typing.List[SessionSearchResult]] = None

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
return super().json(**kwargs_with_defaults)

def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
kwargs_with_defaults_exclude_unset: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults_exclude_none: typing.Any = {"by_alias": True, "exclude_none": True, **kwargs}

return deep_union_pydantic_dicts(
super().dict(**kwargs_with_defaults_exclude_unset), super().dict(**kwargs_with_defaults_exclude_none)
)

class Config:
frozen = True
smart_union = True
extra = pydantic_v1.Extra.allow
json_encoders = {dt.datetime: serialize_datetime}
34 changes: 34 additions & 0 deletions src/zep_cloud/types/session_search_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file was auto-generated by Fern from our API Definition.

import datetime as dt
import typing

from ..core.datetime_utils import serialize_datetime
from ..core.pydantic_utilities import deep_union_pydantic_dicts, pydantic_v1
from .message import Message
from .summary import Summary


class SessionSearchResult(pydantic_v1.BaseModel):
message: typing.Optional[Message] = None
score: typing.Optional[float] = None
session_id: typing.Optional[str] = None
summary: typing.Optional[Summary] = None

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
return super().json(**kwargs_with_defaults)

def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
kwargs_with_defaults_exclude_unset: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults_exclude_none: typing.Any = {"by_alias": True, "exclude_none": True, **kwargs}

return deep_union_pydantic_dicts(
super().dict(**kwargs_with_defaults_exclude_unset), super().dict(**kwargs_with_defaults_exclude_none)
)

class Config:
frozen = True
smart_union = True
extra = pydantic_v1.Extra.allow
json_encoders = {dt.datetime: serialize_datetime}

0 comments on commit a9e7928

Please sign in to comment.