Ollama 官网:https://ollama.com/,官方网站的介绍就一句话:Get up and running with large language models. (开始使用大语言模型。)
Ollama是一个开源的 LLM(大型语言模型)服务工具,用于简化在本地运行大语言模型、降低使用大语言模型的门槛,使得大模型的开发者、研究人员和爱好者能够在本地环境快速实验、管理和部署最新大语言模型,包括如Qwen2、Llama3、Phi3、Gemma2等开源的大型语言模型。
c
安装后是默认路径:C:\Users\Administrator\AppData\Local\Programs\Ollama
Ollama大模型仓库
c
https://ollama.org.cn/library
Ollama配置 服务端口
编辑环境变量
OLLAMA_HOST:配置端口
D:\ollama\models:配置大模型本地存储路径
OLLAMA_ORIGINS:访问端口允许跨域
配置环境变量后重启Ollama
启动Ollama后安装启动本地大模型
c
ollama run qwen2:0.5b
查看本地大模型
c
ollama list

java 调用大模型接口
1、JDK必须是17 版本
2、使用的springboot版本
c
<spring-boot.version>3.2.3</spring-boot.version>
3、引入ai依赖
c
<dependency>
<groupId>io.springboot.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
4、配置文件
c
server.port=8088
spring.application.name=NTopicBootX
#大模型端口默认是11434
spring.ai.ollama.base-url=http://localhost:8000
#使用的大模型
spring.ai.ollama.chat.options.model=qwen2:0.5b
5、测试代码
c
@Autowired
@Qualifier("ollamaChatClient")
private OllamaChatClient ollamaChatClient;
@Test
public void test32(){
System.out.println(ollamaChatClient.call("天空为什么是蓝色的"));
}
@Test
public void test31(){
Prompt prompt = new Prompt(
"天空为什么是蓝色的",
OllamaOptions.create()
.withModel("qwen2:0.5b")
.withTemperature(0.4F));
ChatResponse chatResponse = ollamaChatClient.call(prompt);
System.out.println(chatResponse.getResult().getOutput().getContent());
}
6、运行结果