Skip to content

Commit 3fafb23

Browse files
authored
GPT Readme (#16)
* readme * polished readme * fix copy
1 parent a25eaeb commit 3fafb23

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

.env.example

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
WALLET_KEY= # the private key of the wallet
2-
ENCRYPTION_KEY= # a second random 32 bytes encryption key for local db encryption
2+
ENCRYPTION_KEY= # a second random 32 bytes encryption key for local db encryption
3+
4+
# GPT agent example
5+
OPENAI_API_KEY= # the API key for the OpenAI API

environment.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare global {
33
interface ProcessEnv {
44
WALLET_KEY: `0x${string}` | undefined;
55
ENCRYPTION_KEY: string | undefined;
6+
OPENAI_API_KEY: string | undefined;
67
}
78
}
89
}

examples/gpt/README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# GPT agent example
2+
3+
This example uses the [OpenAI](https://openai.com) API for GPT-based responses and [XMTP](https://xmtp.org) for secure messaging. You can test your agent on [xmtp.chat](https://xmtp.chat) or any other XMTP-compatible client.
4+
5+
## Environment variables
6+
7+
Add the following keys to a `.env` file:
8+
9+
```bash
10+
WALLET_KEY= # the private key of the wallet
11+
ENCRYPTION_KEY= # a second random 32 bytes encryption key for local db encryption
12+
OPENAI_API_KEY= # the API key for the OpenAI API
13+
```
14+
15+
You can generate random keys with the following command:
16+
17+
```bash
18+
yarn gen:keys
19+
```
20+
21+
> [!WARNING]
22+
> Running the `gen:keys` script will overwrite the existing `.env` file.
23+
24+
## Usage
25+
26+
```tsx
27+
import { ContentTypeText } from "@xmtp/content-type-text";
28+
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
29+
import OpenAI from "openai";
30+
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
31+
32+
const { WALLET_KEY, ENCRYPTION_KEY, OPENAI_API_KEY } = process.env;
33+
34+
if (!WALLET_KEY) {
35+
throw new Error("WALLET_KEY must be set");
36+
}
37+
38+
if (!ENCRYPTION_KEY) {
39+
throw new Error("ENCRYPTION_KEY must be set");
40+
}
41+
42+
if (!OPENAI_API_KEY) {
43+
throw new Error("OPENAI_API_KEY must be set");
44+
}
45+
46+
const signer = createSigner(WALLET_KEY);
47+
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
48+
const openai = new OpenAI({ apiKey: OPENAI_API_KEY });
49+
50+
const env: XmtpEnv = "dev";
51+
52+
async function main() {
53+
console.log(`Creating client on the '${env}' network...`);
54+
const client = await Client.create(signer, encryptionKey, {
55+
env,
56+
});
57+
58+
console.log("Syncing conversations...");
59+
await client.conversations.sync();
60+
61+
console.log(
62+
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}`,
63+
);
64+
65+
console.log("Waiting for messages...");
66+
const stream = client.conversations.streamAllMessages();
67+
68+
for await (const message of await stream) {
69+
if (
70+
!message ||
71+
!message.contentType ||
72+
!ContentTypeText.sameAs(message.contentType)
73+
) {
74+
console.log("Invalid message, skipping", message);
75+
continue;
76+
}
77+
78+
// Ignore own messages
79+
if (message.senderInboxId === client.inboxId) {
80+
continue;
81+
}
82+
83+
console.log(
84+
`Received message: ${message.content as string} by ${message.senderInboxId}`,
85+
);
86+
87+
const conversation = client.conversations.getConversationById(
88+
message.conversationId,
89+
);
90+
91+
if (!conversation) {
92+
console.log("Unable to find conversation, skipping");
93+
continue;
94+
}
95+
96+
try {
97+
const completion = await openai.chat.completions.create({
98+
messages: [{ role: "user", content: message.content as string }],
99+
model: "gpt-3.5-turbo",
100+
});
101+
102+
const response =
103+
completion.choices[0]?.message?.content ||
104+
"I'm not sure how to respond to that.";
105+
console.log(`Sending AI response: ${response}`);
106+
await conversation.send(response);
107+
} catch (error) {
108+
console.error("Error getting AI response:", error);
109+
await conversation.send(
110+
"Sorry, I encountered an error processing your message.",
111+
);
112+
}
113+
114+
console.log("Waiting for messages...");
115+
}
116+
}
117+
118+
main().catch(console.error);
119+
```
120+
121+
Run the agent and send a test message from [xmtp.chat](https://xmtp.chat).
122+
123+
Enjoy your GPT-powered XMTP agent!

0 commit comments

Comments
 (0)