.d8888b. 888888b. 888 d8P
d88P Y88b 888 "88b 888 d8P
888 888 888 .88P 888 d8P
888 8888888K. 888d88K
888 888 "Y88b 8888888b
888 888 888 888 888 Y88b
Y88b d88P 888 d88P 888 Y88b
"Y8888P" 8888888P" 888 Y88b .ai
Welcome to the ChatBotKit Node SDK. This SDK offers a JavaScript-based platform for effortlessly building conversational AI bots and agents. With ChatBotKit, you can swiftly develop and deploy AI bots capable of natural language interactions.
This is a meta repository for the ChatBotKit Node SDK. It contains the SDK packages for a number of popular platforms and frameworks such as React, Next.js, NextAuth and more.
The ChatBotKit Node SDK is comprised of the following packages:
Package | Version | Description |
---|---|---|
@chatbotkit/cli | The ChatBotKit CLI. | |
@chatbotkit/sdk | The ChatBotKit API SDK. | |
@chatbotkit/react | The ChatBotKit React SDK. | |
@chatbotkit/next | The ChatBotKit Next.js SDK. | |
@chatbotkit/nextauth | The ChatBotKit NextAuth.js SDK. | |
@chatbotkit/fetch | The ChatBotKit isometric fetch implementation. |
This repository also contains the following tools:
Package | Version | Description |
---|---|---|
create-cbk-app | A quick tool to create a new CBK application. |
Follow these steps to start with ChatBotKit:
npm install @chatbotkit/sdk
This example demonstrates streaming capabilities in Edge and Serverless environments:
import { ConversationClient } from '@chatbotkit/sdk/conversation/index.js'
const client = new ConversationClient(/* configuration */)
for await (const { type, data } of client
.complete(null, { model: 'gpt-4', messages })
.stream()) {
if (type === 'token') {
process.stdout.write(data.token)
}
}
This example showcases how to build advanced conversational AI with streaming, function calls, server-side rendering and much more in a Next.js project:
// file: ./app/page.jsx import ChatArea from '../components/ChatArea.jsx' export default function Page() { return <ChatArea /> } // file: ./components/ChatArea.jsx 'use client' import { useContext } from 'react' import { complete } from '../actions/conversation.jsx' import { ChatInput, ConversationContext } from '@chatbotkit/react' import ConversationManager from '@chatbotkit/react/components/ConversationManager' export function ChatMessages() { const { thinking, text, setText, messages, submit, } = useContext(ConversationContext) return ( <div> <div> {messages.map(({ id, type, text, children }) => { switch (type) { case 'user': return ( <div key={id}> <div> <strong>user:</strong> {text} </div> </div> ) case 'bot': return ( <div key={id}> <div> <strong>bot:</strong> {text} </div> {children ? <div>{children}</div> : null} </div> ) } })} {thinking ? ( <div key="thinking"> <strong>bot:</strong> thinking... </div> ) : null} </div> <ChatInput value={text} onChange={(e) => setText(e.target.value)} onSubmit={submit} placeholder="Type something..." style={{ border: 0, outline: 'none', resize: 'none', width: '100%', marginTop: '10px', }} /> </div> ) } export default function ChatArea() { return ( <ConversationManager endpoint={complete}> <ChatMessages /> </ConversationManager> ) } // file: ./actions/conversation.jsx 'use server' import CalendarEvents from '../components/CalendarEvents.jsx' import { streamComplete } from '@chatbotkit/react/actions/complete' import { ChatBotKit } from '@chatbotkit/sdk' const cbk = new ChatBotKit({ secret: process.env.CHATBOTKIT_API_SECRET, }) export async function complete({ messages }) { return streamComplete({ client: cbk.conversation, messages, functions: [ { name: 'getUserName', description: 'Get the authenticated user name', parameters: {}, handler: async () => { return 'John Doe' }, }, { name: 'getCalendarEvents', description: 'Get a list of calendar events', parameters: {}, handler: async () => { const events = [ { id: 1, title: 'Meeting with Jane Doe' }, { id: 2, title: 'Meeting with Jill Doe' }, ] return { children: <CalendarEvents events={events} />, result: { events, }, } }, }, { name: 'declineCalendarEvent', description: 'Decline a calendar event', parameters: { type: 'object', properties: { id: { type: 'number', description: 'The ID of the event to decline', }, }, required: ['id'], }, handler: async ({ id }) => { return `You have declined the event with ID ${id}` }, }, ], }) }
This quick example demonstrates how to use the SDK in a Next.js project:
// file: ./pages/index.js import { AutoTextarea, useConversationManager } from '@chatbotkit/react' export default function Index() { const { thinking, text, setText, messages, submit, } = useConversationManager({ endpoint: '/api/conversation/complete', }) function handleOnKeyDown(event) { if (event.keyCode === 13) { event.preventDefault() submit() } } return ( <div style={{ fontFamily: 'monospace', padding: '10px' }}> {messages.map(({ id, type, text }) => ( <div key={id}> <strong>{type}:</strong> {text} </div> ))} {thinking && ( <div key="thinking"> <strong>bot:</strong> thinking... </div> )} <AutoTextarea value={text} onChange={(e) => setText(e.target.value)} onKeyDown={handleOnKeyDown} placeholder="Type something..." style={{ border: 0, outline: 'none', resize: 'none', width: '100%', marginTop: '10px', }} /> </div> ) } // file: ./pages/api/conversation/complete.js import { ChatBotKit } from '@chatbotkit/sdk' import { stream } from '@chatbotkit/next/edge' const cbk = new ChatBotKit({ secret: process.env.CHATBOTKIT_API_SECRET, }) export default async function handler(req) { const { messages } = await req.json() return stream(cbk.conversation.complete(null, { messages })) } export const config = { runtime: 'edge', }
Explore a range of examples here.
Some notable examples include:
Platform | Example | Description |
---|---|---|
Next.js | Stateless Chat (App Router + RSC + Functions + Function Request) | A stateless chatbot example, where the conversation is managed by the client and the server. This example uses the App Router and Server Actions as well AI functions with function requests. This is a powerful example to demonstrate the full capabilities of the ChatBotKit conversational AI platform. |
Next.js | Stateless Chat (App Router + RSC + Functions) | A stateless chatbot example, where the conversation is managed by the client and the server. This example uses the App Router and Server Actions as well AI functions. |
Next.js | Stateless Chat (App Router + RSC) | A stateless chatbot example, where the conversation is managed by the client and the server. This example uses the App Router and Server Actions. |
Next.js | Stateless Chat (App Router) | A stateless chatbot example, where the conversation is managed by the client. This example uses the App Router. |
Next.js | Stateless Chat | A stateless chatbot example, where the conversation is managed by the client. |
Next.js | Basic Chat | A basic chatbot example, where the conversation is managed by ChatBotKit. |
Next.js | NextAuth Example | Shows how to combine NextAuth and ChatBotKit. |
Node | GPT4 Streaming AI chatbot | A simple streaming AI chatbot example. |
Cloudflare Workers | GPT4 AI chatbot | A streaming AI chatbot example for Cloudflare Workers. |
All SDK features are considered unstable unless explicitly marked as stable. Stability is indicated by the presence of a @stable
tag in the documentation.
Encounter a bug or want to contribute? Open an issue or submit a pull request on our official GitHub repository.