小工具知识网

当前位置: 首页 > 行业知识 > Spring Boot集成Spring AI实现快速接入openAI

行业知识

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

2026-11-05 05:41:52 来源:互联网转载

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尝试

      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: 8088spring:  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;@RestControllerpublic 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;@SpringBootApplicationpublic 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

上一篇:歪歪论坛源码深度解析,优势特色全揭秘!

下一篇:普通服务器怎么做镜像?