概览

AI 助手 SDK 能够让开发者快速在自己的小程序中集成 AI 对话功能,这能够使开发者专注实现自己的业务逻辑,而不用在技术细节上浪费时间。

使用智能生活 App(6.0.0 及以上版本)扫描下方二维码,可以体验 AI 对话功能:

ray-ai-agent-chat

快速开始

版本说明

⚠️ 重要提示:@ray-js/t-agent-plugin-assistant 已被废弃,请使用 @ray-js/t-agent-plugin-aistream 替代。

安装依赖

在项目的 package.json 中添加以下依赖:

{
  "dependencies": {
    "@ray-js/t-agent": "^0.2.0",
    "@ray-js/t-agent-plugin-aistream": "^0.2.0",
    "@ray-js/t-agent-ui-ray": "^0.2.0"
  }
}

确保 @ray-js/t-agent、@ray-js/t-agent-plugin-aistream、@ray-js/t-agent-ui-ray 版本一致

执行 yarn install 安装依赖。

小程序 Kit 要求

{
  "dependencies": {
    "BaseKit": "3.12.0",
    "BizKit": "4.10.0",
    "DeviceKit": "4.6.1",
    "HomeKit": "3.4.0",
    "MiniKit": "3.12.1",
    "AIStreamKit": "1.0.0"
  },
  "baseversion": "2.21.10"
}

基础示例

以下是在小程序中集成 AI 对话界面的最简代码示例:

import React from 'react';
import { View } from '@ray-js/components';
import { createChatAgent, withDebug, withUI } from '@ray-js/t-agent';
import { ChatContainer, MessageInput, MessageList, MessageActionBar } from '@ray-js/t-agent-ui-ray';
import { withAIStream, withBuildIn } from '@ray-js/t-agent-plugin-aistream';

const createAgent = () => {
  const agent = createChatAgent(
    withUI(), // 第一个插件 withUI 插件提供了一些默认的 UI 行为,必选
    withAIStream({
      // withAIStream 插件对接小程序 AI 智能体平台,在小程序中必选
      earlyStart: true, // 是否在 onAgentStart 阶段就建立连接
      agentId: '[your_agent_id]', // 输入您的智能体 ID
    }),
    withDebug(), // withDebug 会在 console 里打印日志
    withBuildIn() // withBuildIn 插件提供了一些内置的功能
  );
  
  return agent;
};

export default function Home() {
  return (
    <View style={{ height: '100vh' }}>
      <ChatContainer createAgent={createAgent}>
        <MessageList />
        <MessageInput />
        <MessageActionBar />
      </ChatContainer>
    </View>
  );
}

请替换 [your_agent_id] 为您的 Agent ID,Agent ID 可以在涂鸦 AI 涂鸦开发者平台中获取。

高级配置

自定义历史消息大小

withAIStream({
  agentId: '[your_agent_id]',
  historySize: 500, // 历史消息大小,默认 1000
})

更多详细说明和高级用法,请参考 T-Agent SDK 开发指南。