1.什么是Spring AI?
Spring AI API 涵蓋了廣泛的功能。每個主要功能都在其專門的部分中進(jìn)行了詳細(xì)介紹。為了提供概述,可以使用以下關(guān)鍵功能:
跨 AI 提供商的可移植 API,用于聊天、文本到圖像和嵌入模型。支持同步和流 API 選項(xiàng)。還支持下拉訪問模型特定功能。我們支持 OpenAI、Microsoft、Amazon、Google、Huggingface 等公司的 AI 模型。
跨 Vector Store 提供商的可移植 API,包括同樣可移植的新穎的類似 SQL 的元數(shù)據(jù)過濾器 API。支持 8 個矢量數(shù)據(jù)庫。
函數(shù)調(diào)用。Spring AI 使 AI 模型可以輕松調(diào)用 POJO java.util.Function 對象。
AI 模型和向量存儲的 Spring Boot 自動配置和啟動器。
數(shù)據(jù)工程的 ETL 框架。這為將數(shù)據(jù)加載到矢量數(shù)據(jù)庫提供了基礎(chǔ),有助于實(shí)現(xiàn)檢索增強(qiáng)生成模式,使您能夠?qū)?shù)據(jù)引入 AI 模型以納入其響應(yīng)中。
Chat Completion API
聊天 API 使開發(fā)人員能夠?qū)⑷斯ぶ悄苤С值牧奶旃δ芗傻剿麄兊膽?yīng)用程序中。它利用預(yù)先訓(xùn)練的語言模型,例如 GPT(生成式預(yù)訓(xùn)練變壓器),以自然語言對用戶輸入生成類似人類的響應(yīng)。
API 通常通過向 AI 模型發(fā)送提示或部分對話來工作,然后 AI 模型根據(jù)其訓(xùn)練數(shù)據(jù)和對自然語言模式的理解生成對話的完成或延續(xù)。然后,完成的響應(yīng)將返回到應(yīng)用程序,應(yīng)用程序可以將其呈現(xiàn)給用戶或?qū)⑵溆糜谶M(jìn)一步處理。
Spring AI Chat Completion API 被設(shè)計(jì)為一個簡單且可移植的接口,用于與各種 AI 模型交互,允許開發(fā)人員以最少的代碼更改在不同模型之間切換。這種設(shè)計(jì)符合 Spring 的模塊化和可互換性理念。
此外,在輸入封裝 Prompt 和輸出處理 ChatResponse 等配套類的幫助下,聊天完成 API 統(tǒng)一了與 AI 模型的通信。它管理請求準(zhǔn)備和響應(yīng)解析的復(fù)雜性,提供直接且簡化的 API 交互。
2.openapi相關(guān)環(huán)境準(zhǔn)備
參考鏈接:https://www.rebelmouse.com/openai-account-set-up
免費(fèi)提供api-key
加博主微信,免費(fèi)送apikey嘗試
3.代碼工程
實(shí)驗(yàn)?zāi)康模簩?shí)現(xiàn)聊天功能api
pom.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
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);
}
}
以上只是一些關(guān)鍵代碼,所有代碼請參見下面代碼倉庫
代碼倉庫
https://github.com/Harries/springboot-demo
4.測試
啟動Spring Boot應(yīng)用
訪問http://127.0.0.1:8088/ai/chat,返回響應(yīng)消息
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文章來源:http://www.zghlxwxcb.cn/news/detail-860940.html
https://springboot.io/t/topic/5166文章來源地址http://www.zghlxwxcb.cn/news/detail-860940.html
到了這里,關(guān)于Spring Boot集成Spring AI實(shí)現(xiàn)快速接入openAI的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!