|
| 1 | +# GPT agent example |
| 2 | + |
| 3 | +This example uses a [Gaia](https://docs.gaianet.ai) API for AI 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 | +Using Gaia, you can also run your own [node](https://docs.gaianet.ai/getting-started/quick-start) and use the OpenAI compatible API in this library. |
| 6 | + |
| 7 | +## Environment variables |
| 8 | + |
| 9 | +Add the following keys to a `.env` file: |
| 10 | + |
| 11 | +```bash |
| 12 | +WALLET_KEY= # the private key of the wallet |
| 13 | +ENCRYPTION_KEY= # a second random 32 bytes encryption key for local db encryption |
| 14 | +GAIA_API_KEY= # Your API key from https://gaianet.ai |
| 15 | +GAIA_NODE_URL= # Your custom Gaia node URL or a public node, ex: https://llama8b.gaia.domains/v1 |
| 16 | +GAIA_MODEL_NAME= # Model name running in your Gaia node or a public node, ex: llama |
| 17 | +``` |
| 18 | + |
| 19 | +You can generate random keys with the following command: |
| 20 | + |
| 21 | +```bash |
| 22 | +yarn gen:keys |
| 23 | +``` |
| 24 | + |
| 25 | +> [!WARNING] |
| 26 | +> Running the `gen:keys` script will overwrite the existing `.env` file. |
| 27 | +
|
| 28 | +## Usage |
| 29 | + |
| 30 | +```tsx |
| 31 | +import { Client, type XmtpEnv } from "@xmtp/node-sdk"; |
| 32 | +import OpenAI from "openai"; |
| 33 | +import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; |
| 34 | + |
| 35 | +const { WALLET_KEY, ENCRYPTION_KEY, GAIA_NODE_URL, GAIA_API_KEY, GAIA_MODEL_NAME } = process.env; |
| 36 | + |
| 37 | +if (!WALLET_KEY) { |
| 38 | + throw new Error("WALLET_KEY must be set"); |
| 39 | +} |
| 40 | + |
| 41 | +if (!ENCRYPTION_KEY) { |
| 42 | + throw new Error("ENCRYPTION_KEY must be set"); |
| 43 | +} |
| 44 | + |
| 45 | +/* Check if the Gaia API key is set */ |
| 46 | +if (!GAIA_API_KEY) { |
| 47 | + throw new Error("GAIA_API_KEY must be set"); |
| 48 | +} |
| 49 | + |
| 50 | +/* Check if the Gaia node's base URL is set */ |
| 51 | +if (!GAIA_NODE_URL) { |
| 52 | + throw new Error("GAIA_NODE_URL must be set"); |
| 53 | +} |
| 54 | + |
| 55 | +/* Check if the the model name for the Gaia node is set */ |
| 56 | +if (!GAIA_MODEL_NAME) { |
| 57 | + throw new Error("GAIA_MODEL_NAME must be set"); |
| 58 | +} |
| 59 | + |
| 60 | +const signer = createSigner(WALLET_KEY); |
| 61 | +const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY); |
| 62 | +const openai = new OpenAI({ |
| 63 | + baseURL: GAIA_NODE_URL, |
| 64 | + apiKey: GAIA_API_KEY |
| 65 | +}); |
| 66 | + |
| 67 | +/* Set the environment to dev or production */ |
| 68 | +const env: XmtpEnv = "dev"; |
| 69 | + |
| 70 | +async function main() { |
| 71 | + console.log(`Creating client on the '${env}' network...`); |
| 72 | + const client = await Client.create(signer, encryptionKey, { |
| 73 | + env, |
| 74 | + }); |
| 75 | + |
| 76 | + console.log("Syncing conversations..."); |
| 77 | + /* Sync the conversations from the network to update the local db */ |
| 78 | + await client.conversations.sync(); |
| 79 | + |
| 80 | + console.log( |
| 81 | + `Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}`, |
| 82 | + ); |
| 83 | + |
| 84 | + console.log("Waiting for messages..."); |
| 85 | + const stream = client.conversations.streamAllMessages(); |
| 86 | + |
| 87 | + for await (const message of await stream) { |
| 88 | + /* Ignore messages from the same agent or non-text messages */ |
| 89 | + if ( |
| 90 | + message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || |
| 91 | + message?.contentType?.typeId !== "text" |
| 92 | + ) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + console.log( |
| 97 | + `Received message: ${message.content as string} by ${message.senderInboxId}`, |
| 98 | + ); |
| 99 | + |
| 100 | + const conversation = client.conversations.getConversationById( |
| 101 | + message.conversationId, |
| 102 | + ); |
| 103 | + |
| 104 | + if (!conversation) { |
| 105 | + console.log("Unable to find conversation, skipping"); |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + try { |
| 110 | + const completion = await openai.chat.completions.create({ |
| 111 | + messages: [{ role: "user", content: message.content as string }], |
| 112 | + model: GAIA_MODEL_NAME, |
| 113 | + }); |
| 114 | + |
| 115 | + /* Get the AI response */ |
| 116 | + const response = |
| 117 | + completion.choices[0]?.message?.content || |
| 118 | + "I'm not sure how to respond to that."; |
| 119 | + |
| 120 | + console.log(`Sending AI response: ${response}`); |
| 121 | + await conversation.send(response); |
| 122 | + } catch (error) { |
| 123 | + console.error("Error getting AI response:", error); |
| 124 | + await conversation.send( |
| 125 | + "Sorry, I encountered an error processing your message.", |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + console.log("Waiting for messages..."); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +main().catch(console.error); |
| 134 | +``` |
| 135 | + |
| 136 | +## Run the agent |
| 137 | + |
| 138 | +```bash |
| 139 | +# git clone repo |
| 140 | +git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git |
| 141 | +# go to the folder |
| 142 | +cd xmtp-agent-examples |
| 143 | +# install packages |
| 144 | +yarn |
| 145 | +# generate random keys (optional) |
| 146 | +yarn gen:keys |
| 147 | +# run the example |
| 148 | +yarn examples:gpt |
| 149 | +``` |
0 commit comments