Spring Boot集成oauth2快速入门demo

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相关环境准备

参考链接:www.rebelmouse.com/openai-acco…

免费提供api-key

加入博主的知识星球,另外现在加入还可以带大家手把手做一个出海项目,作为项目共创者5c55d703-a577-4942-aa50-f222bf45bcdf

3.代码工程

**

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

**

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
xml复制代码<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>ai</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>


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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>


</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

</project>

application.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
kotlin复制代码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

1
2
3
4
5
6
7
8
9
10
11
12
typescript复制代码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);
}
}

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

代码仓库

4.测试

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

5.参考引用

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%