赞
踩
硬件条件:最好具有N卡,ollama可以在轻量运行大模型提供支持。内存8G以上,cpu好一点。
软件环境:windows系统安装ollama
ollama:配置开发测试模型:qwen:7b
(运行 7B 型号至少需要 8 GB 可用 RAM (内存),运行 13B 型号至少需要16 GB可用 RAM (内存) ,运行 33B 型号至少需要32 GB 可用 RAM (内存))
# 下载千问 7b模型
ollama run qwen:7b
- 注意:修改pom文件,重新下载spring ai依赖需要科学上网,请确保网络连接没有问题
<properties> <java.version>21</java.version> <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version> </properties> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </pluginRepository> </pluginRepositories>
spring:
application:
name: ChatOllama
ai:
openai:
base-url: http://localhost:11434
server:
port: 8085
import jakarta.annotation.Resource; import org.springframework.ai.chat.ChatResponse; import org.springframework.ai.chat.messages.Media; import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.ollama.OllamaChatClient; import org.springframework.ai.ollama.api.OllamaOptions; import org.springframework.core.io.ClassPathResource; import org.springframework.util.MimeTypeUtils; 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 reactor.core.publisher.Flux; import java.io.IOException; import java.util.List; @RestController public class OllamaController { @Resource private OllamaChatClient ollamaChatClient; @RequestMapping("/ai/ollama") public Object chat(@RequestParam(value = "msg") String msg) { ChatResponse response = ollamaChatClient.call( new Prompt(msg, OllamaOptions.create() .withModel("qwen:7b") .withTemperature(0.8f) ) ); System.out.print(response.getResult().getOutput()); return response.getResult().getOutput(); } @GetMapping("/ai/generateStream") public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message), OllamaOptions.create().withModel("qwen:7b")); return ollamaChatClient.stream(prompt); } /** * 本次使用llava:7b 硬件有限,怕跑的很慢 所以不返回结果,直接打印 */ @GetMapping("/ai/test") public void test() throws IOException { byte[] imageData = new ClassPathResource("/multimodal.test.png").getContentAsByteArray(); var userMessage = new UserMessage("描述一下图片内容?", List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData))); ChatResponse response = ollamaChatClient.call( new Prompt(List.of(userMessage), OllamaOptions.create().withModel("llava:7b").withMainGPU(1) ) ); System.out.println(response.getResult().getOutput().getContent()); } //更多的图片相关的接口开发可以参考MultiModel项目的内容 }
#获取ollama已经下载的模型
http://localhost:11434/api/tags
http://localhost:8085/ai/ollama?msg=hello
http://localhost:8085/ai/generateStream=hello
http://localhost:8085/ai/test
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。