From 1aee001c053d6b87405c0bca20e847e70e4a2555 Mon Sep 17 00:00:00 2001 From: Vineeth Voruganti <13438633+VVoruganti@users.noreply.github.com> Date: Sun, 17 Mar 2024 11:43:33 -0700 Subject: [PATCH] Docstrings and langchain message converter in reverse --- sdk/honcho/client.py | 188 ++++++++++++++++++++++++--------- sdk/honcho/ext/langchain.py | 26 ++++- sdk/honcho/sync_client.py | 204 ++++++++++++++++++++++++++---------- sdk/pyproject.toml | 25 +++-- 4 files changed, 318 insertions(+), 125 deletions(-) diff --git a/sdk/honcho/client.py b/sdk/honcho/client.py index 2e6ed7b..4dd96ce 100644 --- a/sdk/honcho/client.py +++ b/sdk/honcho/client.py @@ -49,6 +49,7 @@ def __init__( Args: response (dict): Response from API with pagination information honcho (AsyncHoncho): Honcho Client + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -99,7 +100,10 @@ def __init__( response (dict): Response from API with pagination information user (AsyncUser): Honcho User associated with the session reverse (bool): Whether to reverse the order of the results or not - location_id (str): ID of the location associated with the session + filter (dict, optional): Key value filter to apply to the results + location_id (str, optional): ID of the location associated with the session + is_active (bool): boolean filter for restricint results to only active + sessions """ super().__init__(response) self.user = user @@ -152,6 +156,7 @@ def __init__( Args: response (dict): Response from API with pagination information session (AsyncSession): Session the returned messages are associated with + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -202,9 +207,10 @@ def __init__( response (dict): Response from API with pagination information session (AsyncSession): Session the returned messages are associated with + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results - message_id (Optional[str]): ID of the message associated with the - metamessage_type (Optional[str]): Type of the metamessage + message_id (str, optional): ID of the message associated with the + metamessage_type (str, optional): Type of the metamessage """ super().__init__(response) self.session = session @@ -255,6 +261,7 @@ def __init__( response (dict): Response from API with pagination information collection (AsyncCollection): Collection the returned documents are associated with + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -300,6 +307,7 @@ def __init__( Args: response (dict): Response from API with pagination information user (AsyncUser): Honcho Client + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -338,7 +346,13 @@ class AsyncHoncho: """Honcho API Client Object""" def __init__(self, app_name: str, base_url: str = "https://demo.honcho.dev"): - """Constructor for Client""" + """Constructor for Client + + Args: + app_name (str): Name of the application + base_url (str): Base URL for the instance of the Honcho API defaults to + https://demo.honcho.dev + """ self.server_url: str = base_url # Base URL for the instance of the Honcho API self.client: httpx.AsyncClient = httpx.AsyncClient() self.app_name: str = app_name # Representing name of the client application @@ -346,7 +360,7 @@ def __init__(self, app_name: str, base_url: str = "https://demo.honcho.dev"): self.metadata: dict async def initialize(self): - """Initialize the Honcho client from the server""" + """Run initialization tasks for the Honcho client""" res = await self.client.get( f"{self.server_url}/apps/get_or_create/{self.app_name}" ) @@ -355,6 +369,10 @@ async def initialize(self): self.app_id: uuid.UUID = data["id"] self.metadata: dict = data["metadata"] + async def init(self): + """Synonym for initialize""" + await self.initialize() + @property def base_url(self): """Shortcut for common API prefix. made a property to prevent tampering""" @@ -452,9 +470,11 @@ async def get_users( """Get Paginated list of users Args: - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return. Defaults to 50 + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to False Returns: AsyncGetUserPage: Paginated list of users @@ -483,7 +503,9 @@ async def get_users_generator( all users in an app Args: - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply. Defaults to None + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to False Yields: AsyncUser: The User object @@ -519,7 +541,7 @@ async def get_users_generator( class AsyncUser: - """Represents a single user in an app""" + """Represents a single user in an app and associated methods""" def __init__( self, @@ -528,7 +550,18 @@ def __init__( metadata: dict, created_at: datetime.datetime, ): - """Constructor for User""" + """Constructor for User + + Args: + honcho (AsyncHoncho): The honcho object + id (uuid.UUID): The id of the user + metadata (dict): The metadata for the user + created_at (datetime.datetime): The time the user was created + + Returns: + AsyncUser: The created User object + + """ # self.base_url: str = honcho.base_url self.honcho: AsyncHoncho = honcho self.id: uuid.UUID = id @@ -542,7 +575,7 @@ def base_url(self): def __str__(self): """String representation of User""" - return f"AsyncUser(id={self.id}, app_id={self.honcho.app_id}, metadata={self.metadata})" # noqa: E501 + return f"AsyncUser(id={self.id}, app_id={self.honcho.app_id}, metadata={self.metadata})" async def update(self, metadata: dict): """Updates a user's metadata @@ -600,11 +633,13 @@ async def get_sessions( Args: location_id (str, optional): Optional Location ID representing the - location of a session - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return - reverse (bool): Whether to reverse the order of the results - is_active (bool): Whether to only return active sessions + location of a session. Defaults to None + filter (dict, optional): The key value filter to apply. Defaults to None + page (int, optional): The page of results to return. Defaults to 1 + page_size (int, optional): The number of results to return. Defaults to 50 + reverse (bool): Whether to reverse the order of the results. Defaults to + False + is_active (bool): Whether to only return active sessions. Defaults to False Returns: AsyncGetSessionPage: Page or results for get_sessions query @@ -644,8 +679,10 @@ async def get_sessions_generator( Args: location_id (str, optional): Optional Location ID representing the location of a session - reverse (bool): Whether to reverse the order of the results - is_active (bool): Whether to only return active sessions + reverse (bool): Whether to reverse the order of the results. Defaults to + False + is_active (bool): Whether to only return active sessions. Defaults to False + filter (dict, optional): The key value filter to apply. Defaults to None Yields: AsyncSession: The Session object of the requested Session @@ -673,8 +710,8 @@ async def create_session( Args: location_id (str, optional): Optional Location ID representing the - location of a session - metadata (dict, optional): Optional session metadata + location of a session. Defaults to "default" + metadata (dict, optional): Optional session metadata. Defaults to None Returns: AsyncSession: The Session object of the new Session @@ -705,6 +742,7 @@ async def create_collection( Args: name (str): unique name for the collection for the user + metadata (dict, optional): Optional metadata. Defaults to None Returns: AsyncCollection: The Collection object of the new Collection @@ -757,15 +795,16 @@ async def get_collections( """Return collections associated with a user paginated Args: - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return + filter (dict, optional): The key value filter to apply. Defaults to None + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return reverse (bool): Whether to reverse the order of the results Returns: AsyncGetCollectionPage: Page or results for get_collections query """ - # url = f"{self.base_url}/collections?page={page}&size={page_size}&reverse={reverse}" # noqa: E501 + # url = f"{self.base_url}/collections?page={page}&size={page_size}&reverse={reverse}" url = f"{self.base_url}/collections" params = { "page": page, @@ -787,7 +826,9 @@ async def get_collections_generator( all sessions for a user in an app Args: - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply. Defaults to None + reverse (bool): Whether to reverse the order of the results. Defaults to + False Yields: AsyncCollection: The Session object of the requested Session @@ -821,7 +862,20 @@ def __init__( is_active: bool, created_at: datetime.datetime, ): - """Constructor for Session""" + """Constructor for Session + + Args: + user (AsyncUser): The user object + id (uuid.UUID): The id of the session + location_id (str): The id of the location associated with the session + metadata (dict): The metadata associated with the session + is_active (bool): Whether the session is active + created_at (datetime.datetime): When the session was created + + Returns: + AsyncSession: The Session object + + """ self.user: AsyncUser = user self.id: uuid.UUID = id self.location_id: str = location_id @@ -836,7 +890,7 @@ def base_url(self): def __str__(self): """String representation of Session""" - return f"AsyncSession(id={self.id}, location_id={self.location_id}, metadata={self.metadata}, is_active={self.is_active})" # noqa: E501 + return f"AsyncSession(id={self.id}, location_id={self.location_id}, metadata={self.metadata}, is_active={self.is_active})" @property def is_active(self): @@ -851,6 +905,7 @@ async def create_message( Args: is_user (bool): Whether the message is from the user content (str): The content of the message + metadata (dict, optional): The metadata of the message. Defaults to None Returns: Message: The Message object of the added message @@ -907,9 +962,11 @@ async def get_messages( """Get all messages for a session Args: - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return per page - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply. Defaults to None + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return per page. Defaults to 50 + reverse (bool): Whether to reverse the order of the results. Defaults to + False Returns: AsyncGetMessagePage: Page of Message objects @@ -935,6 +992,11 @@ async def get_messages_generator( """Shortcut Generator for get_messages. Generator to iterate through all messages for a session in an app + Args: + filter (dict, optional): The key value filter to apply. Defaults to None + reverse (bool): Whether to reverse the order of the results. Defaults to + False + Yields: Message: The Message object of the next Message @@ -965,6 +1027,7 @@ async def create_metamessage( message (Message): A message to associate the metamessage with metamessage_type (str): The type of the metamessage arbitrary identifier content (str): The content of the metamessage + metadata (dict, optional): The metadata of the metamessage. Defaults to None Returns: Metamessage: The Metamessage object of the added metamessage @@ -997,7 +1060,7 @@ async def get_metamessage(self, metamessage_id: uuid.UUID) -> Metamessage: """Get a specific metamessage Args: - message_id (uuid.UUID): The ID of the Message to retrieve + metamessage_id (uuid.UUID): The ID of the Metamessage to retrieve Returns: Message: The Message object @@ -1028,17 +1091,21 @@ async def get_metamessages( """Get all messages for a session Args: - metamessage_type (str, optional): The type of the metamessage - message (Message, optional): The message to associate the metamessage with - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return per page - reverse (bool): Whether to reverse the order of the results + metamessage_type (str, optional): The type of the metamessage. Defaults to + None + message (Message, optional): The message to associate the metamessage with. + Defaults to None + filter (dict, optional): The key value filter to apply. Defaults to None + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return per page. Defaults to 50 + reverse (bool): Whether to reverse the order of the results. Defaults to + False Returns: list[dict]: List of Message objects """ - # url = f"{self.base_url}/metamessages?page={page}&size={page_size}&reverse={reverse}" # noqa: E501 + # url = f"{self.base_url}/metamessages?page={page}&size={page_size}&reverse={reverse}" url = f"{self.base_url}/metamessages" params = { "page": page, @@ -1073,8 +1140,12 @@ async def get_metamessages_generator( through all metamessages for a session in an app Args: - metamessage_type (str, optional): Optional Metamessage type to filter by - message (Message, optional): Optional Message to filter by + metamessage_type (str, optional): Optional Metamessage type to filter by. + Defaults to None + message (Message, optional): Optional Message to filter by. Defaults to None + filter (dict, optional): Optional key value filter. Defaults to None + reverse (bool): Whether to reverse the order of the results. Defaults to + False Yields: Metamessage: The next Metamessage object of the requested query @@ -1143,7 +1214,8 @@ async def update_metamessage( Args: metamessage (Metamessage): The metamessage to update - metadata (dict): The new metadata for the metamessage + metamessage_type (str, optional): The new type of the metamessage + metadata (dict, optional): The new metadata for the metamessage Returns: boolean: Whether the metamessage was successfully updated @@ -1161,14 +1233,23 @@ async def update_metamessage( return success async def close(self): - """Closes a session by marking it as inactive""" + """Closes a session by marking it as inactive. An inactive session can no longer + create new messages""" url = f"{self.base_url}" response = await self.user.honcho.client.delete(url) response.raise_for_status() self._is_active = False async def chat(self, query) -> str: - """Ask Honcho for information about the user session""" + """Chat with the dialectic Honcho Agent that asynchonously reasons about users + + Args: + query (str): The query to send to the agent + + Returns: + str: The response from the agent + + """ url = f"{self.base_url}/chat" params = {"query": query} response = await self.user.honcho.client.get(url, params=params) @@ -1202,13 +1283,14 @@ def base_url(self): def __str__(self): """String representation of Collection""" - return f"AsyncCollection(id={self.id}, name={self.name}, created_at={self.created_at})" # noqa: E501 + return f"AsyncCollection(id={self.id}, name={self.name}, created_at={self.created_at})" async def update(self, name: Optional[str] = None, metadata: Optional[dict] = None): - """Update the name of the collection + """Update the name of the collection. Atleast one argument is required Args: - name (str): The new name of the document + name (str, optional): The new name of the document + metadata (dict, optional): The new metadata of the document Returns: boolean: Whether the session was successfully updated @@ -1237,7 +1319,7 @@ async def create_document(self, content: str, metadata: Optional[dict] = None): Args: content (str): The content of the document - metadata (dict): The metadata of the document + metadata (dict, optional): The metadata of the document Returns: Document: The Document object of the added document @@ -1290,14 +1372,17 @@ async def get_documents( """Get all documents for a collection Args: + filter (dict, optional): The key value filter to apply page (int, optional): The page of results to return page_size (int, optional): The number of results to return per page + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to None Returns: AsyncGetDocumentPage: Page of Document objects """ - # url = f"{self.base_url}/documents?page={page}&size={page_size}&reverse={reverse}" # noqa: E501 + # url = f"{self.base_url}/documents?page={page}&size={page_size}&reverse={reverse}" url = f"{self.base_url}/documents" params = { "page": page, @@ -1318,6 +1403,11 @@ async def get_documents_generator( """Shortcut Generator for get_documents. Generator to iterate through all documents for a collection in an app + Args: + filter (dict, optional): The key value filter to apply + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to None + Yields: Document: The Document object of the next Document @@ -1371,8 +1461,8 @@ async def update_document( Args: document (Document): The Document to update - metadata (dict): The metadata of the document - content (str): The content of the document + content (str, optional): The content of the document + metadata (dict, optional): The metadata of the document Returns: Document: The newly updated Document diff --git a/sdk/honcho/ext/langchain.py b/sdk/honcho/ext/langchain.py index 765b05b..844387e 100644 --- a/sdk/honcho/ext/langchain.py +++ b/sdk/honcho/ext/langchain.py @@ -4,8 +4,9 @@ import functools import importlib -from typing import List +from typing import List, Union +from honcho import AsyncSession, Session from honcho.schemas import Message @@ -16,7 +17,7 @@ def requires_langchain(func): def wrapper(*args, **kwargs): """Check if langchain is installed before running a function""" - if importlib.util.find_spec("langchain") is None: + if importlib.util.find_spec("langchain") is None: # type: ignore raise ImportError("Langchain must be installed to use this feature") # raise RuntimeError("langchain is not installed") return func(*args, **kwargs) @@ -25,10 +26,9 @@ def wrapper(*args, **kwargs): @requires_langchain -def langchain_message_converter(messages: List[Message]): +def _messages_to_langchain(messages: List[Message]): """Converts Honcho messages to Langchain messages""" - - from langchain_core.messages import AIMessage, HumanMessage + from langchain_core.messages import AIMessage, HumanMessage # type: ignore new_messages = [] for message in messages: @@ -37,3 +37,19 @@ def langchain_message_converter(messages: List[Message]): else: new_messages.append(AIMessage(content=message.content)) return new_messages + + +@requires_langchain +def _langchain_to_messages(messages, session: Union[Session, AsyncSession]): + """Converts Langchain messages to Langchain messages""" + from langchain_core.messages import HumanMessage # type: ignore + + for message in messages: + if isinstance(message, HumanMessage): + session.create_message( + is_user=True, content=message.content, metadata=message.metadata + ) + else: + session.create_message( + is_user=False, content=message.content, metadata=message.metadata + ) diff --git a/sdk/honcho/sync_client.py b/sdk/honcho/sync_client.py index a587a77..f8ab7d0 100644 --- a/sdk/honcho/sync_client.py +++ b/sdk/honcho/sync_client.py @@ -49,6 +49,7 @@ def __init__( Args: response (dict): Response from API with pagination information honcho (Honcho): Honcho Client + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -99,7 +100,10 @@ def __init__( response (dict): Response from API with pagination information user (User): Honcho User associated with the session reverse (bool): Whether to reverse the order of the results or not - location_id (str): ID of the location associated with the session + filter (dict, optional): Key value filter to apply to the results + location_id (str, optional): ID of the location associated with the session + is_active (bool): boolean filter for restricint results to only active + sessions """ super().__init__(response) self.user = user @@ -152,6 +156,7 @@ def __init__( Args: response (dict): Response from API with pagination information session (Session): Session the returned messages are associated with + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -202,9 +207,10 @@ def __init__( response (dict): Response from API with pagination information session (Session): Session the returned messages are associated with + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results - message_id (Optional[str]): ID of the message associated with the - metamessage_type (Optional[str]): Type of the metamessage + message_id (str, optional): ID of the message associated with the + metamessage_type (str, optional): Type of the metamessage """ super().__init__(response) self.session = session @@ -255,6 +261,7 @@ def __init__( response (dict): Response from API with pagination information collection (Collection): Collection the returned documents are associated with + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -300,6 +307,7 @@ def __init__( Args: response (dict): Response from API with pagination information user (User): Honcho Client + filter (dict, optional): Key value filter to apply to the results reverse (bool): Whether to reverse the order of the results or not """ super().__init__(response) @@ -338,7 +346,13 @@ class Honcho: """Honcho API Client Object""" def __init__(self, app_name: str, base_url: str = "https://demo.honcho.dev"): - """Constructor for Client""" + """Constructor for Client + + Args: + app_name (str): Name of the application + base_url (str): Base URL for the instance of the Honcho API defaults to + https://demo.honcho.dev + """ self.server_url: str = base_url # Base URL for the instance of the Honcho API self.client: httpx.Client = httpx.Client() self.app_name: str = app_name # Representing name of the client application @@ -346,15 +360,17 @@ def __init__(self, app_name: str, base_url: str = "https://demo.honcho.dev"): self.metadata: dict def initialize(self): - """Initialize the Honcho client from the server""" - res = self.client.get( - f"{self.server_url}/apps/get_or_create/{self.app_name}" - ) + """Run initialization tasks for the Honcho client""" + res = self.client.get(f"{self.server_url}/apps/get_or_create/{self.app_name}") res.raise_for_status() data = res.json() self.app_id: uuid.UUID = data["id"] self.metadata: dict = data["metadata"] + def init(self): + """Synonym for initialize""" + self.initialize() + @property def base_url(self): """Shortcut for common API prefix. made a property to prevent tampering""" @@ -390,9 +406,7 @@ def create_user(self, name: str, metadata: Optional[dict] = None): if metadata is None: metadata = {} url = f"{self.base_url}/users" - response = self.client.post( - url, json={"name": name, "metadata": metadata} - ) + response = self.client.post(url, json={"name": name, "metadata": metadata}) response.raise_for_status() data = response.json() return User( @@ -452,9 +466,11 @@ def get_users( """Get Paginated list of users Args: - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return. Defaults to 50 + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to False Returns: GetUserPage: Paginated list of users @@ -483,7 +499,9 @@ def get_users_generator( all users in an app Args: - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply. Defaults to None + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to False Yields: User: The User object @@ -519,7 +537,7 @@ def get_users_generator( class User: - """Represents a single user in an app""" + """Represents a single user in an app and associated methods""" def __init__( self, @@ -528,7 +546,18 @@ def __init__( metadata: dict, created_at: datetime.datetime, ): - """Constructor for User""" + """Constructor for User + + Args: + honcho (Honcho): The honcho object + id (uuid.UUID): The id of the user + metadata (dict): The metadata for the user + created_at (datetime.datetime): The time the user was created + + Returns: + User: The created User object + + """ # self.base_url: str = honcho.base_url self.honcho: Honcho = honcho self.id: uuid.UUID = id @@ -542,7 +571,9 @@ def base_url(self): def __str__(self): """String representation of User""" - return f"User(id={self.id}, app_id={self.honcho.app_id}, metadata={self.metadata})" # noqa: E501 + return ( + f"User(id={self.id}, app_id={self.honcho.app_id}, metadata={self.metadata})" + ) def update(self, metadata: dict): """Updates a user's metadata @@ -600,11 +631,13 @@ def get_sessions( Args: location_id (str, optional): Optional Location ID representing the - location of a session - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return - reverse (bool): Whether to reverse the order of the results - is_active (bool): Whether to only return active sessions + location of a session. Defaults to None + filter (dict, optional): The key value filter to apply. Defaults to None + page (int, optional): The page of results to return. Defaults to 1 + page_size (int, optional): The number of results to return. Defaults to 50 + reverse (bool): Whether to reverse the order of the results. Defaults to + False + is_active (bool): Whether to only return active sessions. Defaults to False Returns: GetSessionPage: Page or results for get_sessions query @@ -644,8 +677,10 @@ def get_sessions_generator( Args: location_id (str, optional): Optional Location ID representing the location of a session - reverse (bool): Whether to reverse the order of the results - is_active (bool): Whether to only return active sessions + reverse (bool): Whether to reverse the order of the results. Defaults to + False + is_active (bool): Whether to only return active sessions. Defaults to False + filter (dict, optional): The key value filter to apply. Defaults to None Yields: Session: The Session object of the requested Session @@ -673,8 +708,8 @@ def create_session( Args: location_id (str, optional): Optional Location ID representing the - location of a session - metadata (dict, optional): Optional session metadata + location of a session. Defaults to "default" + metadata (dict, optional): Optional session metadata. Defaults to None Returns: Session: The Session object of the new Session @@ -705,6 +740,7 @@ def create_collection( Args: name (str): unique name for the collection for the user + metadata (dict, optional): Optional metadata. Defaults to None Returns: Collection: The Collection object of the new Collection @@ -757,15 +793,16 @@ def get_collections( """Return collections associated with a user paginated Args: - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return + filter (dict, optional): The key value filter to apply. Defaults to None + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return reverse (bool): Whether to reverse the order of the results Returns: GetCollectionPage: Page or results for get_collections query """ - # url = f"{self.base_url}/collections?page={page}&size={page_size}&reverse={reverse}" # noqa: E501 + # url = f"{self.base_url}/collections?page={page}&size={page_size}&reverse={reverse}" url = f"{self.base_url}/collections" params = { "page": page, @@ -787,7 +824,9 @@ def get_collections_generator( all sessions for a user in an app Args: - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply. Defaults to None + reverse (bool): Whether to reverse the order of the results. Defaults to + False Yields: Collection: The Session object of the requested Session @@ -795,9 +834,7 @@ def get_collections_generator( """ page = 1 page_size = 50 - get_collection_response = self.get_collections( - filter, page, page_size, reverse - ) + get_collection_response = self.get_collections(filter, page, page_size, reverse) while True: for collection in get_collection_response.items: yield collection @@ -821,7 +858,20 @@ def __init__( is_active: bool, created_at: datetime.datetime, ): - """Constructor for Session""" + """Constructor for Session + + Args: + user (User): The user object + id (uuid.UUID): The id of the session + location_id (str): The id of the location associated with the session + metadata (dict): The metadata associated with the session + is_active (bool): Whether the session is active + created_at (datetime.datetime): When the session was created + + Returns: + Session: The Session object + + """ self.user: User = user self.id: uuid.UUID = id self.location_id: str = location_id @@ -836,7 +886,7 @@ def base_url(self): def __str__(self): """String representation of Session""" - return f"Session(id={self.id}, location_id={self.location_id}, metadata={self.metadata}, is_active={self.is_active})" # noqa: E501 + return f"Session(id={self.id}, location_id={self.location_id}, metadata={self.metadata}, is_active={self.is_active})" @property def is_active(self): @@ -851,6 +901,7 @@ def create_message( Args: is_user (bool): Whether the message is from the user content (str): The content of the message + metadata (dict, optional): The metadata of the message. Defaults to None Returns: Message: The Message object of the added message @@ -907,9 +958,11 @@ def get_messages( """Get all messages for a session Args: - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return per page - reverse (bool): Whether to reverse the order of the results + filter (dict, optional): The key value filter to apply. Defaults to None + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return per page. Defaults to 50 + reverse (bool): Whether to reverse the order of the results. Defaults to + False Returns: GetMessagePage: Page of Message objects @@ -935,6 +988,11 @@ def get_messages_generator( """Shortcut Generator for get_messages. Generator to iterate through all messages for a session in an app + Args: + filter (dict, optional): The key value filter to apply. Defaults to None + reverse (bool): Whether to reverse the order of the results. Defaults to + False + Yields: Message: The Message object of the next Message @@ -965,6 +1023,7 @@ def create_metamessage( message (Message): A message to associate the metamessage with metamessage_type (str): The type of the metamessage arbitrary identifier content (str): The content of the metamessage + metadata (dict, optional): The metadata of the metamessage. Defaults to None Returns: Metamessage: The Metamessage object of the added metamessage @@ -997,7 +1056,7 @@ def get_metamessage(self, metamessage_id: uuid.UUID) -> Metamessage: """Get a specific metamessage Args: - message_id (uuid.UUID): The ID of the Message to retrieve + metamessage_id (uuid.UUID): The ID of the Metamessage to retrieve Returns: Message: The Message object @@ -1028,17 +1087,21 @@ def get_metamessages( """Get all messages for a session Args: - metamessage_type (str, optional): The type of the metamessage - message (Message, optional): The message to associate the metamessage with - page (int, optional): The page of results to return - page_size (int, optional): The number of results to return per page - reverse (bool): Whether to reverse the order of the results + metamessage_type (str, optional): The type of the metamessage. Defaults to + None + message (Message, optional): The message to associate the metamessage with. + Defaults to None + filter (dict, optional): The key value filter to apply. Defaults to None + page (int): The page of results to return. Defaults to 1 + page_size (int): The number of results to return per page. Defaults to 50 + reverse (bool): Whether to reverse the order of the results. Defaults to + False Returns: list[dict]: List of Message objects """ - # url = f"{self.base_url}/metamessages?page={page}&size={page_size}&reverse={reverse}" # noqa: E501 + # url = f"{self.base_url}/metamessages?page={page}&size={page_size}&reverse={reverse}" url = f"{self.base_url}/metamessages" params = { "page": page, @@ -1073,8 +1136,12 @@ def get_metamessages_generator( through all metamessages for a session in an app Args: - metamessage_type (str, optional): Optional Metamessage type to filter by - message (Message, optional): Optional Message to filter by + metamessage_type (str, optional): Optional Metamessage type to filter by. + Defaults to None + message (Message, optional): Optional Message to filter by. Defaults to None + filter (dict, optional): Optional key value filter. Defaults to None + reverse (bool): Whether to reverse the order of the results. Defaults to + False Yields: Metamessage: The next Metamessage object of the requested query @@ -1143,7 +1210,8 @@ def update_metamessage( Args: metamessage (Metamessage): The metamessage to update - metadata (dict): The new metadata for the metamessage + metamessage_type (str, optional): The new type of the metamessage + metadata (dict, optional): The new metadata for the metamessage Returns: boolean: Whether the metamessage was successfully updated @@ -1161,14 +1229,23 @@ def update_metamessage( return success def close(self): - """Closes a session by marking it as inactive""" + """Closes a session by marking it as inactive. An inactive session can no longer + create new messages""" url = f"{self.base_url}" response = self.user.honcho.client.delete(url) response.raise_for_status() self._is_active = False def chat(self, query) -> str: - """Ask Honcho for information about the user session""" + """Chat with the dialectic Honcho Agent that asynchonously reasons about users + + Args: + query (str): The query to send to the agent + + Returns: + str: The response from the agent + + """ url = f"{self.base_url}/chat" params = {"query": query} response = self.user.honcho.client.get(url, params=params) @@ -1202,13 +1279,16 @@ def base_url(self): def __str__(self): """String representation of Collection""" - return f"Collection(id={self.id}, name={self.name}, created_at={self.created_at})" # noqa: E501 + return ( + f"Collection(id={self.id}, name={self.name}, created_at={self.created_at})" + ) def update(self, name: Optional[str] = None, metadata: Optional[dict] = None): - """Update the name of the collection + """Update the name of the collection. Atleast one argument is required Args: - name (str): The new name of the document + name (str, optional): The new name of the document + metadata (dict, optional): The new metadata of the document Returns: boolean: Whether the session was successfully updated @@ -1237,7 +1317,7 @@ def create_document(self, content: str, metadata: Optional[dict] = None): Args: content (str): The content of the document - metadata (dict): The metadata of the document + metadata (dict, optional): The metadata of the document Returns: Document: The Document object of the added document @@ -1290,14 +1370,17 @@ def get_documents( """Get all documents for a collection Args: + filter (dict, optional): The key value filter to apply page (int, optional): The page of results to return page_size (int, optional): The number of results to return per page + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to None Returns: GetDocumentPage: Page of Document objects """ - # url = f"{self.base_url}/documents?page={page}&size={page_size}&reverse={reverse}" # noqa: E501 + # url = f"{self.base_url}/documents?page={page}&size={page_size}&reverse={reverse}" url = f"{self.base_url}/documents" params = { "page": page, @@ -1318,6 +1401,11 @@ def get_documents_generator( """Shortcut Generator for get_documents. Generator to iterate through all documents for a collection in an app + Args: + filter (dict, optional): The key value filter to apply + reverse (bool): Whether to pull from the beginning or end of the list — + latest or earliest. Defaults to None + Yields: Document: The Document object of the next Document @@ -1371,8 +1459,8 @@ def update_document( Args: document (Document): The Document to update - metadata (dict): The metadata of the document - content (str): The content of the document + content (str, optional): The content of the document + metadata (dict, optional): The metadata of the document Returns: Document: The newly updated Document diff --git a/sdk/pyproject.toml b/sdk/pyproject.toml index ee8c3e9..601f4c0 100644 --- a/sdk/pyproject.toml +++ b/sdk/pyproject.toml @@ -24,20 +24,19 @@ furo = "^2024.1.29" [tool.ruff.lint] # from https://docs.astral.sh/ruff/linter/#rule-selection example select = [ - # pycodestyle - "E", - # Pyflakes - "F", - # pyupgrade - "UP", - # flake8-bugbear - "B", - # flake8-simplify - "SIM", - # isort - "I", + "E", # pycodestyle + "F", # Pyflakes + "UP", # pyupgrade + "B", # flake8-bugbear + "SIM", # flake8-simplify + "S", # flake8-bandit + "I", # isort + "RUF", # ruff +] +ignore = [ + "UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation/ + "E501", # line too long ] -ignore = ["UP007"] [build-system]