Beranda>Terkait pemrograman>Kode sumber lainnya
setText(e.target.value)} onSubmit={submit} placeholder="Type something..." style={{ border: 0, outline: 'none', resize: 'none', width: '100%', marginTop: '10px', }} />
) } export default function ChatArea() { return ( ) } // 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: , 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}` }, }, ], }) }">
 // 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 } `
        } ,
      } ,
    ] ,
  } )
}

Contoh Dasar Next.js

Contoh singkat ini menunjukkan cara menggunakan SDK dalam proyek Next.js:

bot: thinking...
)} setText(e.target.value)} onKeyDown={handleOnKeyDown} placeholder="Type something..." style={{ border: 0, outline: 'none', resize: 'none', width: '100%', marginTop: '10px', }} /> ) } // 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', }">
 // 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' ,
} 

Contoh

Jelajahi berbagai contoh di sini.

Beberapa contoh penting meliputi:

Platform Contoh Keterangan
Berikutnya.js Obrolan Tanpa Status (Router Aplikasi + RSC + Fungsi + Permintaan Fungsi) Contoh chatbot stateless, dimana percakapan dikelola oleh klien dan server. Contoh ini menggunakan App Router dan Tindakan Server serta fungsi AI dengan permintaan fungsi. Ini adalah contoh yang kuat untuk menunjukkan kemampuan penuh platform AI percakapan ChatBotKit.
Berikutnya.js Obrolan Tanpa Status (Router Aplikasi + RSC + Fungsi) Contoh chatbot stateless, dimana percakapan dikelola oleh klien dan server. Contoh ini menggunakan App Router dan Tindakan Server serta fungsi AI.
Berikutnya.js Obrolan Tanpa Status (Router Aplikasi + RSC) Contoh chatbot stateless, dimana percakapan dikelola oleh klien dan server. Contoh ini menggunakan App Router dan Tindakan Server.
Berikutnya.js Obrolan Tanpa Kewarganegaraan (Router Aplikasi) Contoh chatbot stateless, yang percakapannya dikelola oleh klien. Contoh ini menggunakan App Router.
Berikutnya.js Obrolan Tanpa Kewarganegaraan Contoh chatbot tanpa kewarganegaraan, yang percakapannya dikelola oleh klien.
Berikutnya.js Obrolan Dasar Contoh chatbot dasar, dimana percakapan dikelola oleh ChatBotKit.
Berikutnya.js Contoh Otentikasi Berikutnya Menunjukkan cara menggabungkan NextAuth dan ChatBotKit.
simpul Bot obrolan AI Streaming GPT4 Contoh chatbot AI streaming sederhana.
Pekerja Cloudflare Bot obrolan AI GPT4 Contoh chatbot AI streaming untuk Cloudflare Workers.

Stabilitas

Semua fitur SDK dianggap tidak stabil kecuali secara eksplisit ditandai sebagai stabil. Stabilitas ditunjukkan dengan adanya tag @stable di dokumentasi.

Dokumentasi

  • Dokumentasi Tipe : Informasi lengkap tentang tipe yang tersedia di sini.
  • Dokumentasi Platform : Panduan komprehensif untuk ChatBotKit di sini.
  • Tutorial Platform : Tutorial langkah demi langkah untuk ChatBotKit di sini.

Berkontribusi

Mengalami bug atau ingin berkontribusi? Buka terbitan atau kirimkan permintaan penarikan di repositori GitHub resmi kami.

Memperluas
Informasi Tambahan