Discord Agent
This tutorial will walk you through building a Discord agent using the Galadriel framework. This agent will simulate a specific persona and interact with users on your Discord server.
Prerequisites
- Discord Bot: You’ll need a Discord bot account with its token and your server’s ID. Follow Discord’s documentation to create an application, add a bot to it, and invite it to your server. Ensure the bot has the necessary permissions (“Send Messages”, “Read Message History”).
- Galadriel Environment: Make sure you have the Galadriel framework installed and set up as per the Quickstart Guide. This includes Python 3.10 or higher, and the Galadriel library.
- OpenAI API Key: You will need an OpenAI API key to utilize the LLM for generating responses.
- (Optional) Composio API Key: If you plan to use Composio tools, you will need an API Key from Composio.
Step 1: Set up the Discord Bot
-
Create a Discord Application: Go to the Discord Developer Portal and create a new application.
-
Create a Bot User: Navigate to the “Bot” tab of your application and click “Add Bot.”
-
Obtain your Bot Token: In the “Bot” tab, click “Reset Token” and copy the token. Store this token securely. You’ll need it later. Keep this token secure! Enable the “Message Content Intent” under the “Privileged Gateway Intents” section.
-
Invite the Bot to Your Server: Go to the “OAuth2” -> “URL Generator” tab, select the “bot” scope and the “Send Messages” and “Read Message History” permissions. Copy the generated URL and open it in your browser to add the bot to your server.
Step 2: Create a Galadriel Agent Project
-
Clone the Galadriel repository:
Replace
<repository_url>
with the actual URL of the Galadriel repository. -
Create a virtual environment:
-
Install the Galadriel framework:
Step 3: Configure Your Agent
-
Set Environment Variables: Create a
.env
file in the root of your agent directory. Populate it with the necessary environment variables:- Replace
<YOUR_DISCORD_BOT_TOKEN>
with the bot token you obtained from the Discord Developer Portal. - Replace
<YOUR_DISCORD_SERVER_ID>
with the ID of your Discord server. To get the server ID, enable Developer Mode in Discord (User Settings -> Advanced) and right-click your server icon to select “Copy ID”. - Replace
<YOUR_OPENAI_API_KEY>
with your OpenAI API key. - Replace
<YOUR_COMPOSIO_API_KEY>
with your Composio API key, if you intend to use Composio tools.
- Replace
-
Create a Character Agent Configuration: This step involves creating a
agent.json
file containing the personality settings for your agent. This file dictates the agent’s behavior and style of communication.Create a
agent.json
file in your agent directory. You can customize this file to define the agent’s persona. Here’s an example configuration for an Elon Musk persona: -
Modify the
agent/character_agent.py
file:Replace the content of
agent/character_agent.py
with the following code: -
Modify the
agent/agent.py
file: Replace the content with following code. Ensurecharacter_json_path="agent.json"
points to the correct location of theagent.json
file.
Step 4: Run the Agent
-
Start the agent: In your terminal, navigate to the project directory and execute:
Step 5: Interact with the Agent on Discord
- Go to your Discord server.
- Send a message in a channel where the bot has access.
- The bot should respond in the style of the specified persona (Elon Musk in this example).
Code Explanation:
-
The
CharacterAgent
is aToolCallingAgent
that’s designed to embody a specific persona. It loads its personality from theagent.json
file. -
The
agent.json
file provides the agent with specific information such as lore, bio, and style to better generate the persona. -
Tools are functions that the Agent can leverage. The time tool is listed here,
-
The model defines the LLM that the Agent will use to compute and perform tasks.
Conclusion
You have now successfully created a Discord agent using the Galadriel framework that embodies a specific persona and interacts with users on a Discord server. Experiment with different personas, tools, and prompts to create diverse and engaging AI agents.