Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
humanagent committed Jan 31, 2025
1 parent a25eaeb commit ca3e648
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
WALLET_KEY= # the private key of the wallet
ENCRYPTION_KEY= # a second random 32 bytes encryption key for local db encryption
ENCRYPTION_KEY= # a second random 32 bytes encryption key for local db encryption
OPENAI_API_KEY= # the API key for the OpenAI API
62 changes: 62 additions & 0 deletions examples/gpt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# GPT Agent

This example uses the [OpenAI](https://openai.com) API for GPT-based responses and the [XMTP](https://xmtp.org) protocol for secure messaging. You can test your agent on [xmtp.chat](https://xmtp.chat) or any other XMTP-compatible client.

## Environment variables

Add the following keys to a `.env` file:

```bash
WALLET_KEY= # the private key of the wallet
ENCRYPTION_KEY= # a second random 32 bytes encryption key for local db encryption
OPENAI_API_KEY= # the API key for the OpenAI API
```

## Usage

```tsx
import { Message, xmtpClient } from "@xmtp/agent-starter";
import OpenAI from "openai";

// Initialize OpenAI
const openai = new OpenAI();

async function main() {
const agent = await xmtpClient({
encryptionKey: process.env.ENCRYPTION_KEY as string,
onMessage: async (message: Message) => {
console.log(
`Decoded message: ${message?.content.text} from ${message.sender.address}`,
);

// Send user text to OpenAI
const completion = await openai.chat.completions.create({
model: "gpt-4",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: message?.content.text ?? "" },
],
});

// Extract GPT response
const gptMessage = completion.choices[0]?.message?.content?.trim();

// Send GPT response back via XMTP
await agent.send({
message: gptMessage ?? "",
originalMessage: message,
});
},
});

console.log(
`XMTP agent initialized on ${agent.address}\n` +
`Try sending a message at https://xmtp.chat/dm/${agent.address}`,
);
}

main().catch(console.error);
```

Run the agent and send a test message from [xmtp.chat](https://xmtp.chat).
Enjoy your GPT-powered XMTP agent!
12 changes: 2 additions & 10 deletions examples/gpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@ import { createSigner, getEncryptionKeyFromHex } from "@/helpers";

const { WALLET_KEY, ENCRYPTION_KEY, OPENAI_API_KEY } = process.env;

if (!WALLET_KEY) {
throw new Error("WALLET_KEY must be set");
}

if (!ENCRYPTION_KEY) {
throw new Error("ENCRYPTION_KEY must be set");
}

if (!OPENAI_API_KEY) {
throw new Error("OPENAI_API_KEY must be set");
if (!WALLET_KEY || !ENCRYPTION_KEY || !OPENAI_API_KEY) {
throw new Error("Environment variables must be set");
}

const signer = createSigner(WALLET_KEY);
Expand Down

0 comments on commit ca3e648

Please sign in to comment.