Example code is available on GitHubAny application interface that defines logic based on events and supports special commands can work easily with Honcho. Here’s how to use Honcho with Discord as an interface. If you’re not familiar with Discord bot application logic, the py-cord docs would be a good place to start.
Events
Most Discord bots have async functions that listen for specific events, the most common one being messages. We can use Honcho to store messages by user and session based on an interface’s event logic. Take the following function definition for example:py-cord
that listens for messages. We use a helper function validate_message()
to check if the message should be processed.
Helper Functions
The code uses several helper functions to keep the main logic clean and readable. Let’s examine each one:Message Validation
- The message isn’t from the bot itself
- The message isn’t in a DM channel
- The bot is mentioned in the message
Message Sanitization
Peer ID Generation
LLM Integration
to_openai()
method to automatically convert the session context into the format expected by OpenAI’s chat completions API.
Message Sending
Honcho Integration
The new Honcho peer/session API makes integration much simpler:add_messages()
method. The peer.message()
creates a message from the user, while assistant.message()
creates a message from the assistant.
Slash Commands
Discord bots also offer slash command functionality. Here’s an example using Honcho’s dialectic feature:Setup and Configuration
The bot requires several environment variables and setup:honcho_client
: The main Honcho clientassistant
: A peer representing the bot/assistantopenai
: OpenAI client configured to use OpenRouter
Recap
The new Honcho peer/session API makes Discord bot integration much simpler and more intuitive. Key patterns we learned:- Peer/Session Model: Users are represented as peers, conversations as sessions
- Automatic Context Management:
session.get_context().to_openai()
automatically formats chat history - Message Storage:
session.add_messages()
stores both user and assistant messages - Dialectic Queries:
peer.chat()
enables querying conversation history - Helper Functions: Clean code organization with focused helper functions