Skip to content

Simple agent

Get started quickly with this simple agent setup. This guide will walk you through the process of setting up an agent that can listen to and reply to messages. Perfect for integrating with other systems or for quick prototyping.

Structure

Your project will have the following structure:

group/
├── src/
│   ├── index.ts
├── package.json
├── tsconfig.json
└── .env

Agent

The agent is the core component that listens for messages and responds. Follow these steps to set it up:

  1. Create the Agent: Define your agent in src/index.ts.
  2. Start Listening: Use the agent to start listening to messages.
src/index.ts
import { agentReply, Context, createAgent } from "@xmtp/message-kit";
 
export const agent = createAgent({
  name: "GPT Bot",
  description: "Use GPT to generate text responses.",
  tag: "@bot",
  onMessage: async (context: Context) => {
    await agentReply(context);
  },
}).run();

Integrating external agents

You can also integrate other agents in your project.

src/index.ts
onMessage: async (context: Context) => {
  const {
    message: {
      sender,
      content: { text },
    },
    agent,
  } = context;
 
  // Connect to a third-party API
  try {
    const response = await axios.get("https://api.example.com/data", {
      headers: {
        Authorization: `Bearer ${process.env.API_KEY}`,
      },
      params: {
        address: sender.address,
        text,
      },
    });
    const apiData = response.data;
    //Return the agent framework with the response
    await context.send(response.data);
  } catch (error) {
    console.error("Error fetching data from API:", error);
    await context.send("An error occurred while fetching data from the API.");
  }
};

Variables

Set up the necessary environment variables to configure your agent. This step is essential for connecting your agent to the right services.

cmd
KEY= # the private key of the agent wallet 
TEST_ENCRYPTION_KEY= # a different private key for encryption
OPENAI_API_KEY= # the API key for OpenAI

End result

Once set up, your agent will be able to understand and respond to user messages effectively. Here's an example of what the final prompt might look like:

example_prompt.md
You are a helpful agent called @bot that lives inside a web3 messaging app called
 
 
# Rules
- You can respond with multiple messages if needed. Each message should be separated by a newline character.
- You can trigger skills by only sending the command in a newline message.
- Each command starts with a slash (/).
- Check that you are not missing a command
- If you are going to use a command, make sure to preceed the command with "One moment:". i.e "Sure! ill check that for you. One moment:
/check humanagent.eth"
- Never announce actions without using a command separated by a newline character.
- Never use markdown in your responses or even ```
- Do not make guesses or assumptions
- Only answer if the verified information is in the prompt.
- Focus only on helping users with operations detailed below.
- Date: Fri, 13 Dec 2024 18:31:26 GMT,
 
 
## User context
- Start by fetch their domain from or Converse username
- Call the user by their name or domain, in case they have one
- Ask for a name (if they don't have one) so you can suggest domains.
- Message sent date: 2024-12-13T18:31:44.925Z
- Users address is: 0x40f08f0f853d1c42c61815652b7ccd5a50f0be09
- Users name is: ArizonaOregon
- Converse username is: ArizonaOregon
 
## Commands
undefined
 
## Examples
undefined

Summary

By following this guide, you've set up a simple agent capable of listening to and replying to messages. This setup can be easily integrated with other systems, making it a versatile tool for various applications.