|
| 1 | +--- |
| 2 | +title: AI Powered Commands |
| 3 | +description: Learn how to use a large language model to power your commands. |
| 4 | +--- |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +CommandKit's `@commandkit/ai` plugin allows you to execute your bot commands using large language models. This enables you to use your bot's features entirely through natural language. |
| 9 | + |
| 10 | +:::warning |
| 11 | +This is an experimental feature and is subject to change. |
| 12 | +::: |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @commandkit/ai |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +```typescript title="commandkit.config.ts" |
| 23 | +import { defineConfig } from 'commandkit'; |
| 24 | +import { ai } from '@commandkit/ai'; |
| 25 | + |
| 26 | +export default defineConfig({ |
| 27 | + plugins: [ai()], |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +## Setting up the AI model |
| 32 | + |
| 33 | +CommandKit allows you to dynamically specify the AI model to use for your bot. |
| 34 | + |
| 35 | +```typescript title="src/ai.ts" |
| 36 | +import { createGoogleGenerativeAI } from '@ai-sdk/google'; |
| 37 | +import { configureAI } from '@commandkit/ai'; |
| 38 | + |
| 39 | +const google = createGoogleGenerativeAI({ |
| 40 | + apiKey: process.env.GOOGLE_API_KEY, |
| 41 | +}); |
| 42 | + |
| 43 | +const model = google.languageModel('gemini-2.0-flash'); |
| 44 | + |
| 45 | +configureAI({ |
| 46 | + // commandkit will call this function |
| 47 | + // to determine which AI model to use |
| 48 | + selectAiModel: async () => { |
| 49 | + return { model }; |
| 50 | + }, |
| 51 | + messageFilter: async (message) => { |
| 52 | + // only respond to messages in guilds that mention the bot |
| 53 | + return ( |
| 54 | + message.inGuild() && message.mentions.users.has(message.client.user.id) |
| 55 | + ); |
| 56 | + }, |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +Now you can simply import this file in your `app.ts`, |
| 61 | + |
| 62 | +```ts title="app.ts" |
| 63 | +import { Client } from 'discord.js'; |
| 64 | +// simply import the ai file |
| 65 | +import './ai'; |
| 66 | + |
| 67 | +const client = new Client({...}); |
| 68 | + |
| 69 | +export default client; |
| 70 | +``` |
| 71 | + |
| 72 | +## Creating AI commands |
| 73 | + |
| 74 | +AI commands can be created by exporting a function called `ai` from your command file. You can also export `aiConfig` object along with the `ai` function to specify the parameters for the command. |
| 75 | + |
| 76 | +```typescript title="src/commands/balance.ts" |
| 77 | +import { ApplicationCommandOptionType } from 'discord.js'; |
| 78 | +import { CommandData, ChatInputCommand } from 'commandkit'; |
| 79 | +import { AiCommand } from '@commandkit/ai'; |
| 80 | +import { z } from 'zod'; |
| 81 | + |
| 82 | +export const command: CommandData = { |
| 83 | + name: 'balance', |
| 84 | + description: 'Get the current balance of the user.', |
| 85 | + options: [ |
| 86 | + { |
| 87 | + name: 'user', |
| 88 | + description: 'The user to get the balance for.', |
| 89 | + type: ApplicationCommandOptionType.User, |
| 90 | + }, |
| 91 | + ], |
| 92 | +}; |
| 93 | + |
| 94 | +export const aiConfig: AiConfig = { |
| 95 | + parameters: z.object({ |
| 96 | + userId: z.string().describe('The ID of the user to get the balance of'), |
| 97 | + }), |
| 98 | +}; |
| 99 | + |
| 100 | +export const chatInput: ChatInputCommand = async (ctx) => { |
| 101 | + const { interaction } = ctx; |
| 102 | + const user = interaction.options.getUser('user'); |
| 103 | + const balance = await db.getBalance(user.id); |
| 104 | + |
| 105 | + await interaction.reply({ |
| 106 | + content: `The balance of ${user.username} is ${balance}`, |
| 107 | + }); |
| 108 | +}; |
| 109 | + |
| 110 | +// AI will call this function to get the balance of the user |
| 111 | +export const ai: AiCommand = async (ctx) => { |
| 112 | + const { userId } = ctx.params; |
| 113 | + const balance = await db.getBalance(userId); |
| 114 | + |
| 115 | + // return object with the balance |
| 116 | + return { |
| 117 | + userId, |
| 118 | + balance, |
| 119 | + }; |
| 120 | +}; |
| 121 | +``` |
| 122 | + |
| 123 | +Now, you can simply mention the bot in a message to get the balance of the user. Eg: |
| 124 | + |
| 125 | +```text |
| 126 | +@bot what is the balance of @user? |
| 127 | +``` |
| 128 | + |
| 129 | +AI can also call multiple commands in a single message. Eg: |
| 130 | + |
| 131 | +```text |
| 132 | +@bot show me the basic details of the user @user and also include the balance of that user. |
| 133 | +``` |
| 134 | + |
| 135 | +The above prompt will call the built-in `getUserInfo` tool and the `balance` command. |
0 commit comments