Spring AI上架,打造专属业务大模型,AI开发再也不是难事!

在这里插入图片描述

Spring AI 来了

Spring AI 是 AI 工程师的一个应用框架,它提供了一个友好的 API 和开发 AI 应用的抽象,旨在简化 AI 应用的开发工序。

提供对常见模型的接入能力,目前已经上架 https://start.spring.io/,提供大家测试访问。(请注意虽然已经上架 start.spring.io,但目前还是在 Spring 私服,未发布至 Maven 中央仓库)

以 ChatGPT 模型为例

1. 添加依赖
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
  </dependency>

 <!-- 配置 Spring 仓库 -->
  <repositories>
      <repository>
          <id>spring-milestones</id>
          <name>Spring Milestones</name>
          <url>https://repo.spring.io/milestone</url>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
      </repository>
  </repositories>
2. 配置 OpenAI 相关参数

在yml 配置

spring:
  ai:
    openai:
      base-url: # 支持 openai-sb、openai-hk 等中转站点,如用官方则不填
      api-key: sk-xxxx

API 使用

1. 聊天 API
@Autowired
private OpenAiChatClient chatClient;

@Test
void testChatClient() {
    String message = """
            树上有 9 只鸟,猎人开枪打死一只,树上还剩下多少只鸟?
            """;
    System.out.println(chatClient.call(message));
}
2. 生成图像
@Autowired
private OpenAiImageClient openaiImageClient;
@Test
void testImageClient() {
    ImageResponse response = openaiImageClient
            .call(new ImagePrompt("A light cream colored mini golden doodle", OpenAiImageOptions.builder()
                    .withQuality("hd").withN(4).withHeight(1024).withWidth(1024).build())
    );
}
3. Prompts 提示词 API
@Test
void testPrompts() {
    String systemText = """
            You are a helpful AI assistant that helps people find information.
            Your name is {name}
            You should reply to the user's request with your name.
            """;
    SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);

    String userText = """
            Tell me about three famous pirates from the Golden Age of Piracy and why they did.
            Write at least a sentence for each pirate.
            """;

    Message userMessage = new UserMessage(userText);
    Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "lengleng"));

    Prompt prompt = new Prompt(List.of(userMessage, systemMessage));

    List<Generation> response = chatClient.call(prompt).getResults();

    for (Generation generation : response){
        System.out.println(generation.getOutput().getContent());
    }
}

进阶:开发业务大模型

目标:在自然语言交流中,可通过调用外部工具来回答问题;将自然语言转换为 API 调用参数或查询数据库的条件;提取文本中的结构化数据

在这里插入图片描述

1. 创建 Function Calling
public class MockMathScoreService implements Function<MockMathScoreService.Request, MockMathScoreService.Response> {

    /**
     * 请求此模型必须入参的数据维度有哪些 (姓名)
     * <p>
     * input: 张三同学的数学成绩怎么样?
     */
    @JsonInclude(Include.NON_NULL)
    @JsonClassDescription("数学行业")
    public record Request(@JsonProperty(required = true,
            value = "username") @JsonPropertyDescription("学生姓名") String username) {
    }


    /**
     * 返回大模型的数据维度有哪些 (姓名,成绩)
     */
    public record Response(String username, double score) {
    }

    /**
     * 实际的数据源提供逻辑,根据用户输入 username 查询本地数据库,返回给大模型
     *
     * @param request the function argument  username
     * @return Response
     */
    @Override
    public Response apply(Request request) {
        if (request.username.contains("张三")) {
            return new Response("张三", 100);
        } else if (request.username.contains("李四")) {
            return new Response("李四", 99.5);
        }
        return new Response("null", 0);
    }
}
2. 调用
@Test
void  testCallFunc(){
    String promptText= "张三同学的数学成绩怎么样?";
    UserMessage userMessage = new UserMessage(promptText);

    var promptOptions = OpenAiChatOptions.builder()
            .withFunctionCallbacks(List.of(new FunctionCallbackWrapper<>(
                    "MathScoreService",
                    "教育行业AI机器人",
                    new MockMathScoreService())))
            .build();

    ChatResponse response = chatClient.call(new Prompt(List.of(userMessage), promptOptions));
    System.out.println(response.getResult().getOutput().getContent());
}
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉清风_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值