总结之Spring AI(一)——使用Spring AI

慈云数据 2024-06-15 技术支持 35 0

前言

当前各种AI项目层出不穷,但绝大多数都是用python写的,现在Spring开源了Spring AI项目,让Java开发者也可以轻松给自己的springboot项目集成AI能力。目前spring AI正式版本为0.8.1,支持接入openAI、Ollama、Azure openAI、Huggingface等,可实现聊天、embedding、图片生成、语音转文字、向量数据库、function calling、prompt模板、outputparser、RAG等功能,就像Java版本的langchain。

Spring AI有以下特点:

1、在AI的聊天、文生图、嵌入模型等方面提供API级别的支持

2、与模型之间支持同步式和流式交互(还记得ChatGPT返回内容时,是逐字生成的吗,这就是流式交互的效果)

3、多种模型支持

使用Spring AI

版本

maven:3.8.1

jdk:17

springboot:3.2.4

spring ai:0.8.1 (稳定版)

OpenAI

使用OpenAI需要对应账号及api key才行,这里可以使用自己账号也可以借助网络免费使用chatGPT的方法

https://gitcode.com/chatanywhere/GPT_API_free/overview

在这里插入图片描述

运行AI

创建一个maven工程

pom


    4.0.0
    
		org.springframework.boot
		spring-boot-starter-parent
		3.2.4
		 
	
    com.imaniy
    hello-springai
    1.0-SNAPSHOT
	Simple AI Application demos
	
		17
        17
        17
	
    
		
			
				org.springframework.ai
				spring-ai-bom
				0.8.1
				pom
				import
			
		
	
	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
		
			spring-snapshots
			Spring Snapshots
			https://repo.spring.io/snapshot
			
				false
			
		
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.ai
			spring-ai-openai-spring-boot-starter
		
		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		
		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		
		
			org.projectlombok
			lombok
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

这里最重要的是spring-ai-openai-spring-boot-starter这个包,如果出现这个包拉不下来,可以更改一下setting.xml文件,使用更快的镜像,再拉不下来就自己手动下载贴在对应的

在这里插入图片描述

setting.xml




  
  
  
  
  
    
  
  
  
    
  
  
  
    
    
  
  
  
   
        alimaven
        aliyun maven
        http://maven.aliyun.com/nexus/content/groups/public/
        central        
    
    
  
  
  
    
    
  
  

配置

# 调用OpenAI接口时表明身份的API Key,前面的章节有提到如何生成一个免费的
spring.ai.openai.api-key=sk-xxxxxx
# 调用OpenAI接口时的基础地址,如果用的是chatanywhere的API Key,这里就要用chatanywhere提供的地址,
# 如果用的是OpenAI的原生API Key,就不用配置这个参数
spring.ai.openai.base-url=https://api.chatanywhere.tech
# 用到的模型
spring.ai.openai.chat.options.model=gpt-3.5-turbo
# temperature越小,回答的内容越严谨,temperature越大,回答的内容越有创造性
spring.ai.openai.chat.options.temperature=0.7

调用

我们这里使用的是springboot工程,需要一个启动

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

调用AI的Controller

import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class SimpleAiController {
	// 负责处理OpenAI的bean,所需参数来自properties文件
	private final ChatClient chatClient;
	public SimpleAiController(@Qualifier("openAiChatClient") ChatClient chatClient) {
		this.chatClient = chatClient;
	}
	@PostMapping("/ai/simple")
	public Map completion(@RequestBody Map map) {
		return Map.of("generation", chatClient.call(map.get("message")));
	}
}

使用API访问gpt

在这里插入图片描述

上面就是简单的使用Sping AI,也是对Spring AI的初步认识。

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon