Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add full docstring coverage #45

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/syncronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions sdk/honcho/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Honcho is a Python client for the Honcho API."""

from .client import (
AsyncHoncho,
AsyncUser,
Expand Down
7 changes: 7 additions & 0 deletions sdk/honcho/cache.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
15 changes: 14 additions & 1 deletion sdk/honcho/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
This module provides asynchronous client functionality for interacting with the Honcho API.
"""

from __future__ import annotations

import datetime
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -176,6 +185,8 @@ async def next(self):


class AsyncGetMetamessagePage(AsyncGetPage):
"""Paginated Results for Get Metamessage Requests"""

def __init__(
self,
response: dict,
Expand Down Expand Up @@ -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}"
)
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions sdk/honcho/ext/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Extensions/utilities for the Honcho Ecosystem"""
10 changes: 10 additions & 0 deletions sdk/honcho/ext/langchain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Utilities to integrate Honcho with Langchain projects
"""

import functools
import importlib
from typing import List
Expand All @@ -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")
Expand All @@ -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 = []
Expand Down
13 changes: 13 additions & 0 deletions sdk/honcho/schemas.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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})"
15 changes: 14 additions & 1 deletion sdk/honcho/sync_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
This module provides synchronous client functionality for interacting with the Honcho API.
"""

from __future__ import annotations

import datetime
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -176,6 +185,8 @@ def next(self):


class GetMetamessagePage(GetPage):
"""Paginated Results for Get Metamessage Requests"""

def __init__(
self,
response: dict,
Expand Down Expand Up @@ -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}"
)
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
Loading