Skip to content

GPT

This plugin is already included in MessageKit, but you can import it directly in your files for more customization.

Overview

The idea behind this plugin is to abstract the complexities of creating agents by handling:

  • Memory
  • Skills execution
  • Personality
  • Rules
  • Reliability
  • User context

Requirements

  • openai: The OpenAI package (already installed in MessageKit).
  • OPENAI_API_KEY: The OpenAI API key.

Key methods

  • agentReply: Processes incoming messages, generates a response using GPT, and sends the reply back to the user.

  • parsePrompt: Parses the system prompt using the agent's system prompt or a default one. It customizes the prompt based on the sender's address and the agent's configuration.

  • textGeneration: Generates a reply using the provided user prompt and system prompt. It utilizes the OpenAI API to create a response based on the conversation context.

  • processMultilineResponse: Processes the generated reply and sends it back to the user. It handles multiline responses to execute the Skills defined in the agent.

Default prompt

Here is the prompt for the agent.

src/prompt.ts
export const systemPrompt = `{intro}
 
{vibe}
 
{rules}
 
{user_context}
 
{skills}
`;

Properties

  • {rules}: The rules of the agent.
  • {user_context}: The user context.
  • {skills}: The skills of the agent.
  • {scenarios}: The scenarios of the agent.
  • {agent_name}: The name of the agent.
  • {name}: The name of the user.
  • {address}: The address of the user.
  • {domain}: The ENS domain of the user.
  • {username}: The Converse username of the user.

Default rules

This is how the prompt rules are implemented in MessageKit.

plugins/gpt.ts
export const PROMPT_RULES = `# 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:\n/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: ${new Date().toUTCString()},
`;

Memory

This is how the memory is implemented in MessageKit. For now, it's only in memory.

plugins/gpt.ts
//Could be user address, conversation id, etc.
// By default, the memory key is the sender address
let memoryKey = "any memory key";
 
// create memory
chatMemory.createMemory(key, systemPrompt);
// add memory
chatMemory.addEntry(
  context.getMemoryKey()
  text,
  "user | assistant | system",
);
// clear memory
chatMemory.clear(key);
// get memory
chatMemory.getHistory(key);

getMemoryKey

This is how the memory key is generated in MessageKit.

lib/core.ts
getMemoryKey() {
  return this.getConversationKey() + ":" + this.message?.sender?.address;
}