Spring AI介绍
Spring
AI是AI工程师的一个应用框架,它提供了一个友好的API和开发AI应用的抽象,旨在简化AI应用的开发工序,例如开发一款基于ChatGPT的对话应用程序。
- 项目地址:https://github.com/spring-projects-experimental/spring-ai
- 文档地址:https://docs.spring.io/spring-ai/reference/
目前该项目已经集成了OpenAI、Azure OpenAI、Hugging
Face、Ollama等API。不过,对于集成了OpenAI接口的项目,只要再搭配One-API项目,就可以调用目前主流的大语言模型了。
使用介绍
在介绍如何使用Spring AI开发一个对话接口之前,我先介绍下ChatGPT应用的开发原理。
首先,ChatGPT是OpenAI推出的一款生成式人工智能大语言模型,OpenAI为了ChatGPT能够得到广泛应用,向开发者提供了ChatGPT的使用接口,开发者只需使用OpenAI为开发者提供的Key,向OpenAI提供的接口地址发起各种形式的请求就可以使用ChatGPT。因此,开发一款ChatGPT应用并不是让你使用人工智能那套技术进行训练和开发,而是作为搬运工,通过向OpenAI提供的ChatGPT接口发起请求来获取ChatGPT响应,基于这一流程来开发的。
第一种方式采用openai
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>ai</artifactId><version>0.0.1-SNAPSHOT</version><name>ai</name><description>ai</description><properties><java.version>17</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>0.8.1-SNAPSHOT</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId></dependency></dependencies><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></repository></repositories><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml
spring:ai:openai:api-key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
IndexService
package com.example.service;public interface IndexService {String send(String msg);
}
IndexServiceImpl
package com.example.service.impl;import com.example.service.IndexService;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class IndexServiceImpl implements IndexService {@Autowiredprivate OpenAiChatClient openAiChatClient;@Overridepublic String send(String msg) {// Prompt prompt = new Prompt(new UserMessage(msg));
// return openAiChatClient.stream(prompt);return openAiChatClient.call(msg);}
}
第二种方式采用Ollama
Spring AI 不仅提供了与 OpenAI 进行API交互,同样支持与 Ollama 进行API交互。Ollama
是一个发布在GitHub上的项目,专为运行、创建和分享大型语言模型而设计,可以轻松地在本地启动和运行大型语言模型。
windows可以下载ollama,你可以用linux都行,我这里图方便
https://ollama.com/download
下载好了打开命令台 你可以自己选model
https://ollama.com/library
ollama run llama2-chinese
然后yml配置
spring:ai:ollama:base-url: http://127.0.0.1:11434chat:# 要跟你刚刚ollama run llama2-chinese 后面这个模块一模一样才行model: llama2-chinese
IndexServiceImpl
package com.example.service.impl;import com.example.service.IndexService;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class IndexServiceImpl implements IndexService {@Autowiredprivate OllamaChatClient ollamaChatClient;@Overridepublic String send(String msg) {// Prompt prompt = new Prompt(new UserMessage(msg));
// return ollamaChatClient.stream(prompt);return ollamaChatClient.call(msg);}
}