#

SUD

大模型游戏互动接入(Web)

在支持AI互动的游戏当中,用户可以通过语音或者文字与大模型AI玩家进行互动
互动的前提是游戏中至少得有一个真人,再通过相应接口先加入大模型AI玩家,后续通过ISudAiAgent相关接口和大模型AI玩家进行互动

ISudAiAgent接口

interface ISudAiAgent {

  /**
   * 传入的音频切片是从RTC获取的PCM数据
   * PCM数据格式必须是:采样率:16000, 采样位数:16, 声道数: MONO
   * PCM数据长度可以根据效果调整,长度大: 精确度好但延时长  长度小:延时短但牺牲精确度
   */
  pushAudio(data: Buffer, dataLength: number): void;

  /** 发送文本内容 */
  sendText(text: string): void;

  /**
   * 暂停语音识别,暂时不调用pushAudio了
   * 调用之后会对末define的识别直接变为define
   */
  pauseAudio(): void;

  /**
   * 关闭语音识别
   */
  stopAudio(): void;

  /** 设置回调 */
  setISudListenerAiAgent(listener: ISudListenerAiAgent): void;
}

一、获取ISudAiAgent

加载游戏后通过ISudFSTAPP接口方法getAiAgent获取ISudAiAgent
在demo中,可通过onGameStarted回调后iSudFSTAPP来获取ISudAiAgent 获取ISudAiAgent接口后,可通过该接口和大模型AI玩家进行语音、文字的互动,并可设置监听来接收大模型AI玩家的回复消息

使用样例

onGameStarted() {
  // 在onGameStarted触发之后去拿,可以保证返回对象不为空
  const ai = nsdk.iSudFSTAPP?.getAiAgent()
  console.log('[ ai ] >', ai)
  if (ai) {
    setAiAgent(ai!)
  }
}

二、添加大模型AI玩家

接口定义文档:通用状态 notifyStateChange

52. 设置游戏中的大模型AI玩家
app_common_game_add_big_scale_model_ai_players

在游戏位当中至少有一个真人时,客户端可通过该接口定义的协议格式向游戏发送消息来添加大模型AI玩家

const aiPlayer = {
  userId: 'userid', // 玩家id
  avatar: 'url', // 头像url
  name: `xxxname`, // 名字
  gender: 'male', // 性别 male:男,female:女
  aiId: '1' // 随机一个ai性格 目前支持1~370
}
const aiPlayers: IAiModel = {
  aiPlayers: [aiPlayer],
  isReady: 1 // 机器人加入后是否自动准备 1:自动准备,0:不自动准备 默认为1
}

SudSDK && SudSDK.sudFSTAPPDecorator.notifyAPPCommon('app_common_game_add_big_scale_model_ai_players', JSON.stringify(aiPlayers))

三、文本对话

APP可选择向游戏发送文本消息与大模型AI玩家对话
通过ISudAiAgent接口方法sendText来向游戏发送文本内容

public interface ISudAiAgent {
    ...
    /** 发送文本内容 */
    sendText(text: string): void;
    ...
}

const sendText = (text: string) => {
  if (aiAgent && text) {
    aiAgent.sendText(text)
  }
}

四、语音对话

APP可选择向游戏发送语音消息与大模型AI玩家对话
通过ISudAiAgent接口方法pushAudio来向游戏发送语音消息
pauseAudio用于暂停语音对话,stopAudio用于关闭语音对话

public interface ISudAiAgent {
    ...
    /**
     * 进行语音对话时,通过此接口发送语音数据,非必须实现
     * 传入的音频切片是从RTC或系统录音当中获取的PCM数据
     * PCM数据格式必须是:采样率:16000, 采样位数:16, 声道数: MONO
     * PCM数据长度可以根据效果调整,长度大: 精确度好但延时长  长度小:延时短但牺牲精确度
     */
     pushAudio(data: Buffer, dataLength: number): void

    /** 暂停语音识别,暂时不调用pushAudio了 */
    pauseAudio(): void

    /** 关闭语音识别 */
    stopAudio(): void
    ...
}

aiAgent?.pushAudio(pcm as any, length)

五、接收大模型AI玩家的消息

当APP通过语音或者文字向大模型AI玩家发送消息之后,大模型AI玩家会进行回复一条或多条消息给房间里的玩家
通过ISudAiAgent接口方法setISudListenerAiAgent设置监听器用于监听大模型AI玩家的回复消息

public interface ISudAiAgent {
    ...
    /** 设置回调 */
    setISudListenerAiAgent(listener: ISudListenerAiAgent);
    ...
}

public interface ISudListenerAiAgent {
    /** 大模型AI消息 */
    onRoomChatMessage(json: string): void;
}
// ISudListenerAiAgent.onRoomChatMessage返回的json格式
{
    "uid": "26", // 大模型AI玩家的uid
    "content": "我是双子座哦,你们呢?", // 大模型AI玩家说话的文本内容,和语音是对应的
    "audioData": "base64格式的音频数据" // 大模型AI玩家说话的语音内容,和文本是对应的
}
// 在Demo/QuickStart当中可这样调,用于监听大模型AI玩家发送过来的消息
onGameStarted() {
  // 在onGameStarted触发之后去拿,可以保证返回对象不为空
  const ai = nsdk.iSudFSTAPP?.getAiAgent()
  console.log('[ ai ] >', ai)
  if (ai) {
    setAiAgent(ai!)
    ai.setISudListenerAiAgent({
      // 接收大模型AI玩家的消息
      onRoomChatMessage(data) {
        if (data) {
          const parseData = JSON.parse(data)
          // parseData.content文本内容,和语音说话出来的内容是对应的,可根据自身产品需求决定是否展示
          console.log('[ parseData uid] >', parseData.uid, '[ parseData content] >', parseData.content)

          setUserAudioPlayState(parseData.uid, { state: 1, uid: parseData.uid }) 
          // 模拟播放ai大模型语音数据,调用方可按自身情况进行播放处理
          // base64格式的音频数据,将其通过Base64解成字节数组之后,就是mp3格式的二进制音频数据了
          player.pushSrc({ src: base64ToBlobUrl(`data:audio/aac;base64,${parseData.audioData}`), data: parseData })
          const list = aiUserContentList
          list.push(parseData)
          setAiUserContentList([...list])
        }
      }
    })
  }
},

当收到onRoomChatMessage回调时,大模型AI玩家的说话内容,根据自身产品需求来选择展示文本消息或者是播放语音消息

六、声浪控制

当APP收到下面的游戏消息时,表示APP可选择控制游戏展示对应玩家的声浪效果
接口定义文档:通用状态-游戏

69. 通知app可以开始推送麦克说话状态
mg_common_game_player_mic_state

收到指令之后可通过如下接口进行控制
接口定义文档:通用状态 notifyStateChange

51. app向游戏下发玩家mic状态
app_common_game_player_mic_state

用于控制游戏当中是否显示指定玩家的声浪,是一个开关性质的指令,开启或关闭的时候发送一次即可

onGameCustomerStateChange(handle, state, data) {
  switch (state) {
    case 'mg_common_game_player_mic_state': {
      // 收到可以控制推送麦克风状态开关
      console.log('[ 可以开始推送麦克说话状态 ] >', data)
      setIsGamePlayerMicStateOk(true)
      break
    }
  }
}

const value = {
  "uid": "user id", // 玩家id
  "state": 0   // 0:停止说话 1:说话中
}
// 控制玩家声浪变化
SudSDK && SudSDK.sudFSTAPPDecorator.notifyAPPCommon('app_common_game_player_mic_state', JSON.stringify(value))

results matching ""

    No results matching ""