Spring AI Alibaba 对接百炼平台大模型使用详解

一、前言

随着各大厂商都在大模型领域布局,各种编程语言也在积极降低对接大模型的成本,可以肯定的是,在大模型能力和生态渐臻完善的情况下,接下里就是在应用层的接入、商用和市场化进程,基于次以java生态spring框架为例,也推出了springAi,基于springai可以快速对接chatgpt模型,也可以对接主流厂商的大模型,本文以spring ai alibaba 为例进行详细的说明

二、springAI概述

2.1 spring ai 是什么

SpringAI是一个人工智能工程的应用框架,他的目标是将spring生态系统的设计原则(如可移植性和模块化设计)应用于人工智能领域,并推广使用POJO作为人工智能领域应用程序的构建块

官网地址:Spring AI

三、阿里百炼平台

登录地址:https://bailian.console.aliyun.com/#/home

获取api_key:(我的-模型-模型广场-api_key)对应模型的api_key

3.1验证模型能力:(http调用)

点击模型,对应有调用示例:https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=https%3A%2F%2Fhelp.aliyun.com%2Fdocument_detail%2F2712576.html

代码如下:

package com.example.springai.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/ai")
public class QianWenTest {
    /**
     * 调用模型的
     * @param args
     */
    public static void main(String[] args) {
        String requestUrl = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
        String dashScopeApiKey = "sk-875dd6ef14244431acdc7ccb974f5bfe";
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            // 请求头
            connection.setRequestProperty("Authorization", "Bearer " + dashScopeApiKey);
            connection.setRequestProperty("Content-Type", "application/json");
            // 设置允许输出
            connection.setDoOutput(true);
            // 构造请求参数
            String requestBody = "{\n" +
                    "    \"model\": \"qwen-plus\",\n" +
                    "    \"messages\": [\n" +
                    "        {\n" +
                    "            \"role\": \"system\",\n" +
                    "            \"content\": \"You are a helpful assistant.\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"role\": \"user\",\n" +
                    "            \"content\": \"写一段Java的Hello World的代码\"\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}";
            // 写入请求数据
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }
            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code : " + responseCode);
            // 读取响应数据
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                System.out.println("Response Body: " + response.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试输出:

3.2创建应用,并且验证应用的能力

百炼平台:应用管理页面,创建应用,并选择模型,点击发布,即可外部微服务调用

创建好的应用对应app_id:

点击调用,对应的调用案例:

代码如下:

package com.example.springai.controller;



import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/ai")
public class BailianAgentRagStreamController {

    private static final Logger logger = LoggerFactory.getLogger(BailianAgentRagStreamController.class);
    private DashScopeAgent agent;

    @Value("${spring.ai.dashscope.agent.app-id}")
    private String appId;

    public BailianAgentRagStreamController(DashScopeAgentApi dashscopeAgentApi) {
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode bizParams = objectMapper.createObjectNode();
        bizParams.put("name", "Alice");
        bizParams.put("age", 30);

        this.agent = new DashScopeAgent(dashscopeAgentApi,
                DashScopeAgentOptions.builder()
                        .withSessionId("current_session_id")
                        .withIncrementalOutput(true)
                        .withHasThoughts(true)
                        .withBizParams(bizParams)
                        .build());
    }

    @GetMapping("/bailian/agent/stream")
    public Flux<String> stream(@RequestParam(value = "message", defaultValue = "你好,请问你的知识库文档主要是关于什么内容的?") String message) {
        return agent.stream(new Prompt(message, DashScopeAgentOptions.builder().withAppId(appId).build())).map(response -> {
            if (response == null || response.getResult() == null) {
                logger.error("chat response is null");
                return "chat response is null";
            }

            AssistantMessage app_output = response.getResult().getOutput();
            String content = app_output.getText();

            DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput output = (DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput) app_output.getMetadata().get("output");
            List<DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput.DashScopeAgentResponseOutputDocReference> docReferences = output.docReferences();
            List<DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput.DashScopeAgentResponseOutputThoughts> thoughts = output.thoughts();

            logger.info("content:\n{}\n\n", content);

            return content;
        });
    }
}

pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.10</version>
        <relativePath/>
    </parent>


    <groupId>com.jzj</groupId>
    <artifactId>pharmacist-system</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>pharmacist-system</name>
    <description>pharmacist-system</description>


    <properties>

        <java.version>17</java.version>

        <spring-ai-alibaba.version>1.0.0-M6.1</spring-ai-alibaba.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>com.alibaba.cloud.ai</groupId>
            <artifactId>spring-ai-alibaba-starter</artifactId>
            <version>${spring-ai-alibaba.version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20171018</version>
        </dependency>
    </dependencies>


    <repositories>

        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml:

#server:
#  port: 8085

#spring:
#  ai:
#    dashscope:
#      agent:
#        api-id: sk-875dd6ef14244431acdc7ccb974f5bfe
#        api-key: 7a63af7dec3b4c3db381d3733e3f1736


spring:

  application:
    name: pharmacist-system


  ai:
    dashscope:


      agent:
        app-id: 7a63af7dec3b4c3db381d3733e3f1736


      api-key: sk-875dd6ef14244431acdc7ccb974f5bfe

测试:

http://localhost:8080/ai/bailian/agent/stream?message=最近房琪事件

参考:

【大模型】Spring AI Alibaba 对接百炼平台大模型使用详解-CSDN博客

Spring AI Alibaba本地集成百炼智能体应用-CSDN博客

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值