首頁>編程相關>其他源碼
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 } `
        } ,
      } ,
    ] ,
  } )
}

基本 Next.js 範例

此快速範例示範如何在 Next.js 專案中使用 SDK:

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' ,
} 

範例

在這裡探索一系列範例。

一些值得注意的例子包括:

平台例子描述
Next.js無狀態聊天(應用程式路由器 + RSC + 函數 + 函數請求)無狀態聊天機器人範例,其中對話由客戶端和伺服器管理。此範例使用應用程式路由器和伺服器操作以及具有函數請求的 AI 函數。這是一個強而有力的範例,展示了 ChatBotKit 對話式 AI 平台的全部功能。
Next.js無狀態聊天(應用程式路由器 + RSC + 功能)無狀態聊天機器人範例,其中對話由客戶端和伺服器管理。此範例使用應用程式路由器和伺服器操作以及 AI 功能。
Next.js無狀態聊天(應用程式路由器 + RSC)無狀態聊天機器人範例,其中對話由客戶端和伺服器管理。此範例使用應用程式路由器和伺服器操作。
Next.js無狀態聊天(應用程式路由器)無狀態聊天機器人範例,其中對話由客戶端管理。此範例使用應用程式路由器。
Next.js無狀態聊天無狀態聊天機器人範例,其中對話由客戶端管理。
Next.js基本聊天一個基本的聊天機器人範例,其中對話由 ChatBotKit 管理。
Next.js下一個驗證範例展示如何結合 NextAuth 和 ChatBotKit。
節點GPT4 串流 AI 聊天機器人一個簡單的串流人工智慧聊天機器人範例。
Cloudflare 工人GPT4 人工智慧聊天機器人Cloudflare Workers 的串流 AI 聊天機器人範例。

穩定

除非明確標記為穩定,否則所有 SDK 功能都被視為不穩定。文件中存在@stable標記表示穩定性。

文件

  • 類型文件:此處有關可用類型的詳細資訊。
  • 平台文件:這裡是 ChatBotKit 的綜合指南。
  • 平台教學:此處提供 ChatBotKit 的分步教學。

貢獻

遇到錯誤或想貢獻?在我們的官方 GitHub 儲存庫上提出問題或提交拉取請求。

展開
附加信息