From 2c58415dd4aae759fc313626d727846ef461664f Mon Sep 17 00:00:00 2001 From: hyusap Date: Sun, 17 Mar 2024 17:31:51 -0400 Subject: [PATCH] full docstring coverage --- scripts/syncronizer.py | 1 + sdk/honcho/__init__.py | 2 ++ sdk/honcho/cache.py | 7 +++++++ sdk/honcho/client.py | 15 ++++++++++++++- sdk/honcho/ext/__init__.py | 1 + sdk/honcho/ext/langchain.py | 10 ++++++++++ sdk/honcho/schemas.py | 13 +++++++++++++ sdk/honcho/sync_client.py | 15 ++++++++++++++- 8 files changed, 62 insertions(+), 2 deletions(-) diff --git a/scripts/syncronizer.py b/scripts/syncronizer.py index 0740bb4..830f02c 100755 --- a/scripts/syncronizer.py +++ b/scripts/syncronizer.py @@ -13,6 +13,7 @@ sync_code = re.sub(r"async\s", "", source_code) sync_code = re.sub(r"await\s", "", sync_code) sync_code = re.sub(r"Async", "", sync_code) +sync_code = re.sub(r"asynchronous", "synchronous", sync_code) # Write the modified code to the destination file destination_file_path = os.path.join(this_dir, "../sdk/honcho/sync_client.py") diff --git a/sdk/honcho/__init__.py b/sdk/honcho/__init__.py index 6ab9451..61da0ff 100644 --- a/sdk/honcho/__init__.py +++ b/sdk/honcho/__init__.py @@ -1,3 +1,5 @@ +"""Honcho is a Python client for the Honcho API.""" + from .client import ( AsyncHoncho, AsyncUser, diff --git a/sdk/honcho/cache.py b/sdk/honcho/cache.py index f9488a0..f8e6b5e 100644 --- a/sdk/honcho/cache.py +++ b/sdk/honcho/cache.py @@ -1,11 +1,18 @@ +""" +This module provides an LRU (Least Recently Used) cache implementation as part of the Honcho SDK's caching mechanisms. +""" + from collections import OrderedDict + class LRUCache: """ An implementation of a basic LRUcache that utilizes the built in OrderedDict data structure. """ + def __init__(self, capacity: int): + """Initialize the cache""" self.capacity = capacity self.cache = OrderedDict() diff --git a/sdk/honcho/client.py b/sdk/honcho/client.py index b35ec1e..2e6ed7b 100644 --- a/sdk/honcho/client.py +++ b/sdk/honcho/client.py @@ -1,3 +1,7 @@ +""" +This module provides asynchronous client functionality for interacting with the Honcho API. +""" + from __future__ import annotations import datetime @@ -62,6 +66,11 @@ def __init__( ] async def next(self): + """Get the next page of results + + Returns: + AsyncGetUserPage | None: Next Page of Results or None if there are no more users to retreive from a query + """ if self.page >= self.pages: return None return await self.honcho.get_users( @@ -176,6 +185,8 @@ async def next(self): class AsyncGetMetamessagePage(AsyncGetPage): + """Paginated Results for Get Metamessage Requests""" + def __init__( self, response: dict, @@ -335,6 +346,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""" res = await self.client.get( f"{self.server_url}/apps/get_or_create/{self.app_name}" ) @@ -345,7 +357,7 @@ async def initialize(self): @property def base_url(self): - """Shorcut for common API prefix. made a property to prevent tampering""" + """Shortcut for common API prefix. made a property to prevent tampering""" return f"{self.server_url}/apps/{self.app_id}" async def update(self, metadata: dict): @@ -1156,6 +1168,7 @@ async def close(self): self._is_active = False async def chat(self, query) -> str: + """Ask Honcho for information about the user session""" url = f"{self.base_url}/chat" params = {"query": query} response = await self.user.honcho.client.get(url, params=params) diff --git a/sdk/honcho/ext/__init__.py b/sdk/honcho/ext/__init__.py index e69de29..bd84b90 100644 --- a/sdk/honcho/ext/__init__.py +++ b/sdk/honcho/ext/__init__.py @@ -0,0 +1 @@ +"""Extensions/utilities for the Honcho Ecosystem""" diff --git a/sdk/honcho/ext/langchain.py b/sdk/honcho/ext/langchain.py index c84a0be..765b05b 100644 --- a/sdk/honcho/ext/langchain.py +++ b/sdk/honcho/ext/langchain.py @@ -1,3 +1,7 @@ +""" +Utilities to integrate Honcho with Langchain projects +""" + import functools import importlib from typing import List @@ -6,8 +10,12 @@ def requires_langchain(func): + """A utility to check if langchain is installed before running a function""" + @functools.wraps(func) def wrapper(*args, **kwargs): + """Check if langchain is installed before running a function""" + if importlib.util.find_spec("langchain") is None: raise ImportError("Langchain must be installed to use this feature") # raise RuntimeError("langchain is not installed") @@ -18,6 +26,8 @@ def wrapper(*args, **kwargs): @requires_langchain def langchain_message_converter(messages: List[Message]): + """Converts Honcho messages to Langchain messages""" + from langchain_core.messages import AIMessage, HumanMessage new_messages = [] diff --git a/sdk/honcho/schemas.py b/sdk/honcho/schemas.py index 1a0a2fe..3eaead7 100644 --- a/sdk/honcho/schemas.py +++ b/sdk/honcho/schemas.py @@ -1,8 +1,14 @@ +""" +This module defines the schema classes for various entities such as Message, Metamessage, and Document. +""" + import uuid import datetime class Message: + """Class representing a Message""" + def __init__( self, session_id: uuid.UUID, @@ -21,10 +27,13 @@ def __init__( self.created_at = created_at def __str__(self): + """String representation of Message object""" return f"Message(id={self.id}, is_user={self.is_user}, content={self.content})" class Metamessage: + """Class representing a Metamessage""" + def __init__( self, id: uuid.UUID, @@ -43,10 +52,13 @@ def __init__( self.created_at = created_at def __str__(self): + """String representation of Metamessage object""" return f"Metamessage(id={self.id}, message_id={self.message_id}, metamessage_type={self.metamessage_type}, content={self.content})" class Document: + """Class representing a Document""" + def __init__( self, id: uuid.UUID, @@ -63,4 +75,5 @@ def __init__( self.created_at = created_at def __str__(self) -> str: + """String representation of Document object""" return f"Document(id={self.id}, metadata={self.metadata}, content={self.content}, created_at={self.created_at})" diff --git a/sdk/honcho/sync_client.py b/sdk/honcho/sync_client.py index 5322812..a587a77 100644 --- a/sdk/honcho/sync_client.py +++ b/sdk/honcho/sync_client.py @@ -1,3 +1,7 @@ +""" +This module provides synchronous client functionality for interacting with the Honcho API. +""" + from __future__ import annotations import datetime @@ -62,6 +66,11 @@ def __init__( ] def next(self): + """Get the next page of results + + Returns: + GetUserPage | None: Next Page of Results or None if there are no more users to retreive from a query + """ if self.page >= self.pages: return None return self.honcho.get_users( @@ -176,6 +185,8 @@ def next(self): class GetMetamessagePage(GetPage): + """Paginated Results for Get Metamessage Requests""" + def __init__( self, response: dict, @@ -335,6 +346,7 @@ 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}" ) @@ -345,7 +357,7 @@ def initialize(self): @property def base_url(self): - """Shorcut for common API prefix. made a property to prevent tampering""" + """Shortcut for common API prefix. made a property to prevent tampering""" return f"{self.server_url}/apps/{self.app_id}" def update(self, metadata: dict): @@ -1156,6 +1168,7 @@ def close(self): self._is_active = False def chat(self, query) -> str: + """Ask Honcho for information about the user session""" url = f"{self.base_url}/chat" params = {"query": query} response = self.user.honcho.client.get(url, params=params)