如何传入文本作为大语言模型输入

更新时间:2025-04-03 01:57:33

本文将为您介绍如何通过调用接口传入文本作为大模型(LLM)输入内容。

功能概述

在用户与智能体的通话过程中,您可以通过调用接口的方式,手动将文本内容传入大型语言模型(LLM)。这一过程显著增强了智能体的功能和灵活性,使其能够根据最新的对话内容动态调整响应内容。

应用场景

在视觉理解通话场景中,如果您需要将手机拍摄的画面及其对应的文本内容同时传输至多模态大模型,为了避免在拍摄过程中手机缺乏声音输入,阿里云将自动生成一段文本内容,并与图像一同传递给大模型。在此过程中,您也可以自定义传入的文本内容。

功能实现

AI实时互动中,您可以选择在客户端或服务端调用接口,传入文本内容作为大型语言模型(LLM)的输入。

示例代码

客户端调用

您可以参考不同平台的示例代码,通过调用端侧接口将文本内容传递至LLM。

Android
iOS
Web
// 创建请求参数Req对象,传入询问智能体的文本消息
ARTCAICallSendTextToAgentRequest req = ARTCAICallSendTextToAgentRequest("xxx");
// 发送给Agent,注意需要在接通后调用,engine为ARTCAICallEngine的对象
engine.sendTextToAgent(req)
// 创建请求参数Req对象
let req = ARTCAICallSendTextToAgentRequest(text: "xxx")
// 发送给Agent,注意需要在接通后调用
_ = self.engine.sendTextToAgent(req: req)

// 发送给Agent,注意需要在接通后调用,engine为ARTCAICallEngine对象
engine.sendTextToAgent(new AICallSendTextToAgentRequest('xxx'));

服务端调用

在服务端,您可以通过调用SendAIAgentText - 传入消息作为LLM输入接口,将文本内容传入LLM。

// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.rtc;

import com.aliyun.tea.*;

public class Sample {

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     * @return Client
     * 
     * @throws Exception
     */
    public static com.aliyun.ice20201109.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("yourak")
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("yoursk");
        // Endpoint 请参考 https://api.aliyun.com/product/ICE
        config.endpoint = "ice.cn-shanghai.aliyuncs.com";
        return new com.aliyun.ice20201109.Client(config);
    }

    private static void sendAIAgentText() throws Exception {
        com.aliyun.ice20201109.Client client =  createClient();

        com.aliyun.ice20201109.models.SendAIAgentTextRequest request = new com.aliyun.ice20201109.models.SendAIAgentTextRequest()
            .setInstanceId("yourinstanceid")
            .setText("yourtext");
            try {
                // 复制代码运行请自行打印 API 的返回值
                com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
                
                client.sendAIAgentTextWithOptions(request, runtime);
            } catch (TeaException error) {
                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                com.aliyun.teautil.Common.assertAsString(error.message);
            }   
    }

    public static void main(String[] args_) throws Exception {
        sendAIAgentText();
    }
}

  • 本页导读 (1)
  • 功能概述
  • 应用场景
  • 功能实现
  • 示例代码