Spring Boot集成Spring AI实现快速接入openAI

2024-06-04 7235阅读

1.什么是Spring AI?

Spring AI API 涵盖了广泛的功能。每个主要功能都在其专门的部分中进行了详细介绍。为了提供概述,可以使用以下关键功能:

  • 跨 AI 提供商的可移植 API,用于聊天、文本到图像和嵌入模型。支持同步和流 API 选项。还支持下拉访问模型特定功能。我们支持 OpenAI、Microsoft、Amazon、Google、Huggingface 等公司的 AI 模型。

  • 跨 Vector Store 提供商的可移植 API,包括同样可移植的新颖的类似 SQL 的元数据过滤器 API。支持 8 个矢量数据库。

  • 函数调用。Spring AI 使 AI 模型可以轻松调用 POJO java.util.Function 对象。

  • AI 模型和向量存储的 Spring Boot 自动配置和启动器。

  • 数据工程的 ETL 框架。这为将数据加载到矢量数据库提供了基础,有助于实现检索增强生成模式,使您能够将数据引入 AI 模型以纳入其响应中。

    Chat Completion API

    • 聊天 API 使开发人员能够将人工智能支持的聊天功能集成到他们的应用程序中。它利用预先训练的语言模型,例如 GPT(生成式预训练变压器),以自然语言对用户输入生成类似人类的响应。

    • API 通常通过向 AI 模型发送提示或部分对话来工作,然后 AI 模型根据其训练数据和对自然语言模式的理解生成对话的完成或延续。然后,完成的响应将返回到应用程序,应用程序可以将其呈现给用户或将其用于进一步处理。

    • Spring AI Chat Completion API 被设计为一个简单且可移植的接口,用于与各种 AI 模型交互,允许开发人员以最少的代码更改在不同模型之间切换。这种设计符合 Spring 的模块化和可互换性理念。

    • 此外,在输入封装 Prompt 和输出处理 ChatResponse 等配套类的帮助下,聊天完成 API 统一了与 AI 模型的通信。它管理请求准备和响应解析的复杂性,提供直接且简化的 API 交互。

      2.openapi相关环境准备

      参考链接:https://www.rebelmouse.com/openai-account-set-up

      免费提供api-key

      加博主微信,免费送apikey尝试

      Spring Boot集成Spring AI实现快速接入openAI 第1张

      3.代码工程

      实验目的:实现聊天功能api

      pom.xml

      
          
              org.springframework.boot
              spring-boot-starter-parent
              3.2.1
          
          4.0.0
          ai
          
              17
              17
          
          
      
              
                  org.springframework.boot
                  spring-boot-starter-web
              
              
                  org.springframework.boot
                  spring-boot-autoconfigure
              
              
                  org.springframework.boot
                  spring-boot-starter-test
                  test
              
              
                  org.springframework.ai
                  spring-ai-openai-spring-boot-starter
                  0.8.0-SNAPSHOT
              
      
          
          
              
                  spring-snapshots
                  Spring Snapshots
                  https://repo.spring.io/snapshot
                  
                      false
                  
              
          
      

      application.yaml

      server:
        port: 8088
      spring:
        ai:
          openai:
            base-url: https://api.openai.com/
            api-key: sk-xxx
            embedding:
              options:
                model: text-davinci-003
            chat:
              #指定某一个API配置(覆盖全局配置)
              api-key: sk-xxx
              base-url: https://api.openai.com/
              options:
                model: gpt-3.5-turbo # 模型配置

      controller

      package com.et.ai.controller;
      import jakarta.annotation.Resource;
      import org.springframework.ai.chat.ChatClient;
      import org.springframework.ai.chat.prompt.Prompt;
      import org.springframework.ai.embedding.EmbeddingClient;
      import org.springframework.ai.embedding.EmbeddingResponse;
      import org.springframework.ai.openai.OpenAiChatClient;
      import org.springframework.ai.openai.api.OpenAiApi;
      import org.springframework.beans.factory.annotation.Autowired;
      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 java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      @RestController
      public class HelloWorldController {
          @Autowired
          EmbeddingClient embeddingClient;
          @Autowired
          ChatClient chatClient;
          @GetMapping("/ai/embedding")
          public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
              EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
              return Map.of("embedding", embeddingResponse);
          }
          @GetMapping("/ai/chat")
          public String chat(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
              Prompt prompt = new Prompt(message);
              return chatClient.call(prompt).getResult().getOutput().getContent();
          }
      }

      DemoApplication.java

      package com.et.ai;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      @SpringBootApplication
      public class DemoApplication {
         public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
         }
      }

      以上只是一些关键代码,所有代码请参见下面代码仓库

      代码仓库

      • https://github.com/Harries/springboot-demo

        4.测试

        • 启动Spring Boot应用

        • 访问http://127.0.0.1:8088/ai/chat,返回响应消息

          Why couldn't the bicycle stand up by itself? Because it was two tired!

          5.参考引用

          • https://docs.spring.io/spring-ai/reference/api/embeddings/openai-embeddings.htm

          • http://www.liuhaihua.cn/archives/710471.html

          • https://springboot.io/t/topic/5166


    免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

    目录[+]