Skip to content

Commit

Permalink
Sentry, OTEL, langchain both directions, fly.toml for deriver
Browse files Browse the repository at this point in the history
  • Loading branch information
VVoruganti committed Mar 20, 2024
1 parent 1aee001 commit 6b04662
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 33 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
# Honcho
# 🫡 Honcho
![Static Badge](https://img.shields.io/badge/Version-0.0.5-blue)
[![Discord](https://img.shields.io/discord/1016845111637839922?style=flat&logo=discord&logoColor=23ffffff&label=Plastic%20Labs&labelColor=235865F2)](https://discord.gg/plasticlabs)
![GitHub License](https://img.shields.io/github/license/plastic-labs/honcho)
![GitHub Repo stars](https://img.shields.io/github/stars/plastic-labs/honcho)
[![X (formerly Twitter) URL](https://img.shields.io/twitter/url?url=https%3A%2F%2Ftwitter.com%2Fplastic_labs)](https://twitter.com/plastic_labs)

A User context management solution for building AI Agents and LLM powered
applications.
Honcho is a platform for making AI agents and LLM powered applications that are personalized
to their end users.

Read about the motivation of this project [here](https://blog.plasticlabs.ai).

Read the user documenation [here](https://docs.honcho.dev)

## Table of Contents

- [Project Structure](#project-structure)
- [Usage](#usage)
- [API](#api)
- [Docker](#docker)
- [Manually](#manually)
- [Deploying on Fly.io](#deploy-on-fly)
- [Client SDK](#client-sdk)
- [Use Locally](#use-locally)
- [Contributing](#contributing)
- [License](#license)

## Project Structure

The Honcho repo is a monorepo containing the server/API that manages database
Expand Down
2 changes: 1 addition & 1 deletion api/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ kill_timeout = "5s"
cpu_kind = "shared"
cpus = 1
memory_mb = 512
processes = ["api"]
processes = ["api", "deriver"]
9 changes: 9 additions & 0 deletions api/src/harvester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid
from typing import List

import sentry_sdk
from dotenv import load_dotenv
from langchain_core.output_parsers import NumberedListOutputParser
from langchain_core.prompts import (
Expand All @@ -20,6 +21,14 @@

load_dotenv()

SENTRY_ENABLED = os.getenv("SENTRY_ENABLED", "False").lower() == "true"
if SENTRY_ENABLED:
sentry_sdk.init(
dsn=os.getenv("SENTRY_DSN"),
enable_tracing=True,
)


SUPABASE_ID = os.getenv("SUPABASE_ID")
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")

Expand Down
13 changes: 7 additions & 6 deletions api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@
from slowapi.util import get_remote_address
from starlette.exceptions import HTTPException as StarletteHTTPException

from .db import engine, scaffold_db
from src.routers import (
apps,
users,
sessions,
messages,
metamessages,
collections,
documents,
messages,
metamessages,
sessions,
users,
)

from .db import engine, scaffold_db

# Otel Setup

DEBUG_LOG_OTEL_TO_PROVIDER = (
Expand Down Expand Up @@ -171,7 +172,7 @@ def otel_logging_init():
otel_trace_init()
otel_logging_init()

SQLAlchemyInstrumentor().instrument(engine=engine)
SQLAlchemyInstrumentor().instrument(engine=engine.sync_engine)

# Sentry Setup

Expand Down
14 changes: 2 additions & 12 deletions example/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from langchain_community.chat_models.fake import FakeListChatModel

from honcho import Honcho
from honcho.ext.langchain import langchain_message_converter
from honcho.ext.langchain import _messages_to_langchain

app_name = str(uuid4())

Expand All @@ -28,16 +28,6 @@
session = user.create_session()


# def langchain_message_converter(messages: List):
# new_messages = []
# for message in messages:
# if message.is_user:
# new_messages.append(HumanMessage(content=message.content))
# else:
# new_messages.append(AIMessage(content=message.content))
# return new_messages


def chat():
while True:
user_input = input("User: ")
Expand All @@ -46,7 +36,7 @@ def chat():
break
user_message = HumanMessage(content=user_input)
history = list(session.get_messages_generator())
langchain_history = langchain_message_converter(history)
langchain_history = _messages_to_langchain(history)
prompt = ChatPromptTemplate.from_messages(
[system, *langchain_history, user_message]
)
Expand Down
4 changes: 2 additions & 2 deletions example/discord/honcho-dspy-personas/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from uuid import uuid1
import discord
from honcho import Honcho
from honcho.ext.langchain import langchain_message_converter
from honcho.ext.langchain import _messages_to_langchain
from graph import chat
from dspy import Example

Expand Down Expand Up @@ -61,7 +61,7 @@ async def on_message(message):
session = user.create_session(location_id)

history = list(session.get_messages_generator())[:5]
chat_history = langchain_message_converter(history)
chat_history = _messages_to_langchain(history)

inp = message.content
user_message = session.create_message(is_user=True, content=inp)
Expand Down
4 changes: 2 additions & 2 deletions example/discord/honcho-fact-memory/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from uuid import uuid1
import discord
from honcho import Honcho
from honcho.ext.langchain import langchain_message_converter
from honcho.ext.langchain import _messages_to_langchain
from chain import LMChain


Expand Down Expand Up @@ -59,7 +59,7 @@ async def on_message(message):
session = user.create_session(location_id)

history = list(session.get_messages_generator())
chat_history = langchain_message_converter(history)
chat_history = _messages_to_langchain(history)

inp = message.content
user_message = session.create_message(is_user=True, content=inp)
Expand Down
4 changes: 2 additions & 2 deletions example/discord/simple-roast-bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from langchain_core.messages import AIMessage, HumanMessage

from honcho import Honcho
from honcho.ext.langchain import langchain_message_converter
from honcho.ext.langchain import _messages_to_langchain

load_dotenv()

Expand Down Expand Up @@ -66,7 +66,7 @@ async def on_message(message):
session = user.create_session(location_id)

history = list(session.get_messages_generator())
chat_history = langchain_message_converter(history)
chat_history = _messages_to_langchain(history)

inp = message.content
session.create_message(is_user=True, content=inp)
Expand Down
33 changes: 28 additions & 5 deletions sdk/honcho/ext/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ def wrapper(*args, **kwargs):

@requires_langchain
def _messages_to_langchain(messages: List[Message]):
"""Converts Honcho messages to Langchain messages"""
"""Converts Honcho messages to Langchain messages
Args:
messages (List[Message]): The list of messages to convert
Returns:
List: The list of converted LangChain messages
"""
from langchain_core.messages import AIMessage, HumanMessage # type: ignore

new_messages = []
Expand All @@ -40,16 +48,31 @@ def _messages_to_langchain(messages: List[Message]):


@requires_langchain
def _langchain_to_messages(messages, session: Union[Session, AsyncSession]):
"""Converts Langchain messages to Langchain messages"""
def _langchain_to_messages(
messages, session: Union[Session, AsyncSession]
) -> List[Message]:
"""Converts Langchain messages to Honcho messages and adds to appropriate session
Args:
messages: The LangChain messages to convert
session: The session to add the messages to
Returns:
List[Message]: The list of converted messages
"""
from langchain_core.messages import HumanMessage # type: ignore

messages = []
for message in messages:
if isinstance(message, HumanMessage):
session.create_message(
message = session.create_message(
is_user=True, content=message.content, metadata=message.metadata
)
messages.append(message)
else:
session.create_message(
message = session.create_message(
is_user=False, content=message.content, metadata=message.metadata
)
messages.append(message)
return messages

0 comments on commit 6b04662

Please sign in to comment.