Ollama
简介
Ollama是一个开源的大型语言模型服务工具,它允许用户在本地机器上构建和运行语言模型,提供了一个简单易用的API来创建、运行和管理模型,同时还提供了丰富的预构建模型库,这些模型可以轻松地应用在多种应用场景中。Ollama支持多种操作系统,包括macOS、Windows、Linux,并提供Docker镜像,方便用户在不同环境中部署使用 。
Ollama的特点包括轻量级和可扩展性,它允许用户通过命令行界面(CLI)或REST API与语言模型进行交互。用户可以下载并运行预训练的模型,如Llama 2、Mistral、Dolphin Phi等,这些模型具有不同的参数量和大小,适用于不同的使用场景和需求 。
此外,Ollama还支持模型的自定义,用户可以根据自己的需求调整模型参数,或者导入自有的模型进行使用。例如,用户可以通过创建Modelfile来定制模型,Modelfile是一个配置文件,用于定义和管理Ollama平台上的模型,通过模型文件可以创建新模型或修改现有模型,以适应特定的应用场景 。
安装
官网:https://ollama.com/
Github:https://github.com/ollama/ollama
进入官网之后,点击download下载对应系统版本进行安装。
模型使用llama3
官网:https://ollama.com/library/llama3
ollama下载完成之后,打开命令行,运行命令ollama run llama3
,自动下载模型,在命令行可进行简单的聊天
llama3有8B和70B,上面的命令运行之后,默认选择的是8B
客户端
python客户端:https://github.com/ollama/ollama-python
import ollama
response = ollama.chat(model='llama3', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
])
print(response['message']['content'])
流式响应:
import ollama
stream = ollama.chat(
model='llama3',
messages=[{'role': 'user', 'content': '用中文讲一个笑话'}],
stream=True,
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
Spring AI
官网:https://docs.spring.io/spring-ai/reference/index.html
ollama文档:https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat.html
1、通过https://start.spring.io/
创建项目,并引入Ollama AI
pom.xml如下:
<?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.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pers.fengxu</groupId>
<artifactId>springaidemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springaidemo</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>22</java.version>
<spring-ai.version>1.0.0-M1</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
配置文件application.properties
spring.application.name=springaidemo
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=llama3
新建controller
package pers.fengxu.springaidemo.controller;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.util.Map;
@RestController
public class ChatController {
private final OllamaChatModel chatModel;
@Autowired
public ChatController(OllamaChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/ai/generate")
public Map 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);
}
}
新建启动类
package pers.fengxu.springaidemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringaidemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringaidemoApplication.class, args);
}
}
启动项目之后,访问:http://localhost:8080/ai/generate