简介
Spring推出了ai框架,来对接各个ai大模型,即Spring AI
官网:https://docs.spring.io/spring-ai/reference/api
个人项目:qf-springAi
整合openAi
文档:https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html
pom
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
config
spring.application.name=qf-springAi
spring.ai.openai.api-key=sk-RGVCxBvJRHsWnfEq1a61F66171504f64874d682cxxxx
# openai chat
spring.ai.openai.chat.api-key=yiios-mOzW5k2WHI6jgCBbF0884a5a92B14d64931dExxxx
spring.ai.openai.chat.base-url=https://api.xxx.com
spring.ai.openai.chat.options.model=gpt-3.5-turbo
# openai image
spring.ai.openai.image.api-key=sk-XWfbhjxq4tAo6yi4Bb492eFc92A045Db901b165f449xxxx
spring.ai.openai.image.base-url=https://api.xxx.app
spring.ai.openai.image.options.model=dall-e-3
controller
@RestController
@RequestMapping("/openai")
public class OpenAiController {
@Resource
private OpenAiChatModel chatModel;
@Resource
private OpenAiImageModel imageModel;
@GetMapping("/ai/generate")
public Map<String,Object> generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", chatModel.call(message));
}
@GetMapping("/ai/generateStream")
public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return chatModel.stream(prompt);
}
/**
* Stream like file download
* @param message the message
* @return {@link Flux}<{@link String}>
*/
@GetMapping(value = "/ai/streamLikeFileDownload", produces = "application/stream;charset=utf-8")
public Flux<String> streamLikeFileDownload(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return chatModel.stream(message);
}
/**
* 打字机流式效果
* @param message the message
* @return {@link Flux}<{@link String}>
*/
@GetMapping(value = "/ai/streamLikeLineAlone", produces = "text/event-stream;charset=utf-8")
public Flux<String> streamLikeLineAlone(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return chatModel.stream(message);
}
/**
* 文生图
* @param message 提示词
* @return {@link ResponseEntity}<{@link String}>
*/
@GetMapping(value = "/ai/generateImageByMessage", produces = "text/event-stream;charset=utf-8")
public ResponseEntity<String> generateImageByMessage(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
ImageResponse imageResponse = imageModel.call(new ImagePrompt(message));
Image output = imageResponse.getResult().getOutput();
return ResponseEntity.ok(output.getUrl());
}
}
整合ollama大模型
文档:https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat.html
ollama:https://ollama.ai/
下载ollama到本地后安装,命令行启动ollama run llama3
pom
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
config
# ollama
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=llama3
controller
@RestController
@RequestMapping("/ollama")
public class OllamaController {
@Resource
private OllamaChatModel chatModel;
@GetMapping("/ai/generate")
public Map<String,Object> generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", chatModel.call(message));
}
@GetMapping("/ai/generateStream")
public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return chatModel.stream(prompt);
}
/**
* Stream like file download
* @param message the message
* @return {@link Flux}<{@link String}>
*/
@GetMapping(value = "/ai/streamLikeFileDownload", produces = "application/stream;charset=utf-8")
public Flux<String> streamLikeFileDownload(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return chatModel.stream(message);
}
/**
* 打字机流式效果
* @param message the message
* @return {@link Flux}<{@link String}>
*/
@GetMapping(value = "/ai/streamLikeLineAlone", produces = "text/event-stream;charset=utf-8")
public Flux<String> streamLikeLineAlone(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return chatModel.stream(message);
}
1111