SpringBoot 接入阿里云百炼(通入千问) 并实现流式输出内容

一:引入Maven依赖

<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>broadscope-bailian-sdk-java</artifactId>
            <version>1.2.0</version>
</dependency>

二:编写阿里云百炼Client类

配置文件加入如下配置

相关参数请从阿里云百炼官网获取

yun:
  bailian:
    accessKeyId:  **************************
    accessKeySecret:  **************************
    agentKey:   **************************
    appId: **************************
@Component
public class BaiLianClient {
    @Resource
    private AlbaiLianConfig baiLianConfig;
    private AccessTokenClient accessTokenClient;
    @Resource
    private ChatCustomerInfoMapper chatCustomerInfoMapper;
    @PostConstruct
    public void init(){
        String accessKeyId = baiLianConfig.getAccessKeyId();
        String agentKey = baiLianConfig.getAgentKey();
        String accessKeySecret = baiLianConfig.getAccessKeySecret();
        accessTokenClient = new AccessTokenClient(accessKeyId,accessKeySecret,agentKey);
    }
    public Flux<CompletionsResponse> createStreamCompletion(ChatCustomerInfo chatCustomerInfo) {
        String content = chatCustomerInfo.getInfoContent();
        String token = accessTokenClient.getToken();
        BaiLianConfig config = new BaiLianConfig()
                .setApiKey(token);
        CompletionsRequest request = new CompletionsRequest()
                .setAppId(baiLianConfig.getAppId())
                .setPrompt(content)
                .setStream(true);
        ApplicationClient client = new ApplicationClient(config);
        chatCustomerInfoMapper.sendInfo(chatCustomerInfo.getUserId(),chatCustomerInfo.getInfoContent(),false);
        Flux<CompletionsResponse> flux = client.streamCompletions(request);
        return client.streamCompletions(request);
    }
}

三:编写Controller层

为了更快实现功能,本人只是简单调用回复消息接口,并将历史消息保存到数据库表中,具体复杂API请参考阿里云百炼官网手册,后端流式输入采用FLux实现,<

Spring Boot集成阿里云百炼AI主要是过其提供的SDK和服务来实现的。首先,你需要完成以下步骤: 1. **添加依赖**:在你的Spring Boot项目中,需要引阿里云的AI SDK,可以在pom.xml文件中添加相应的Maven依赖。例如,如果你要使用自然语言处理(NLP)API,可以添加`alibaba-cloud-sdk-java`。 ```xml <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-sdk-core</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-sdk-alice</artifactId> <version>版本号</version> </dependency> ``` 记得替换`版本号`为实际的SDK版本。 2. **配置密钥和访点**:在应用的配置文件(如application.properties或application.yml)中,设置阿里云的访Key、Secret Key以及服务端点(Endpoint)。 3. **创建服务客户端**:在业务层创建百炼AI的服务客户端实例,比如使用自然语言理解(NLU)服务: ```java import com.aliyun.ailib.client.NLUServiceClient; NLUServiceClient nluService = NLUServiceClient.createInstance( "你的AccessKeyId", "你的AccessKeySecret", "你的区域名_endpoint" ); ``` 4. **调用API处理响应**:使用客户端方法执行特定的AI操作,如文本分析、语音识别等,将结果返回给前端展示。例如,处理完NLP请求后,你可以构造一个包含结果的数据结构传递给前端。 5. **前端显示**:在前端(如Vue、React或Angular),过AJAX或者其他方式从后端获取处理后的数据,然后渲染到对应的UI组件上。可以是简单的文本展示,也可以是图表或其他交互式的可视化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值