-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
43 lines (36 loc) · 1.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { xmtpClient, type Message } from "@xmtp/agent-starter";
import OpenAI from "openai";
// Initialize OpenAI API
const openai = new OpenAI();
async function main() {
const client = await xmtpClient({
walletKey: process.env.WALLET_KEY as string,
onMessage: async (message: Message) => {
console.log(
`Decoded message: ${message.content.text} by ${message.sender.address}`,
);
// Send message content to GPT API
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "developer", content: "You are a helpful assistant." },
{
role: "user",
content: message.content.text ?? "",
},
],
});
const gptMessage = completion.choices[0]?.message?.content?.trim();
// Use GPT response in your application
await client.send({
message: gptMessage ?? "",
originalMessage: message,
metadata: {},
});
},
});
console.log(
`XMTP agent initialized on ${client.address}\nSend a message on http://xmtp.chat/dm/${client.address}`,
);
}
main().catch(console.error);