SpringAI-Ollama

官网地址:https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat.html

1、描述

可以拉取LLM在本地运行,SpringAI通过OllamaChatModel支持聊天功能
可以使用 ollama pull model-name,拉取模型,模型仓库地址:https://ollama.com/search

2、自动配置

2.1、依赖:

<dependency>
   <groupId>org.springframework.ai</groupId>
   <artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>

2.2、属性

2.2.1、基础属性

属性 默认值 描述
spring.ai.ollama.base-url http://localhost:11434 Ollama API 服务器运行的基本 URL
spring.ai.ollama.init.pull-model-strategy never 是否在启动时拉取模型以及如何拉取
spring.ai.ollama.init.timeout 5m 等待模型被拉取的时间
spring.ai.ollama.init.max-retries 0 模型拉取操作的最大重试次数
spring.ai.ollama.init.chat.include true 在初始化任务中包含此类型的模型
spring.ai.ollama.init.chat.additional-models [] 除了通过默认属性配置的模型外,还需要初始化的其他模型

2.2.2、聊天属性

属性 默认值 描述
spring.ai.model.chat ollama 启用 Ollama 聊天模型
spring.ai.ollama.chat.options.model mistral 要使用的支持的模型名称
spring.ai.ollama.chat.options.format - 返回响应的格式。目前,唯一接受的值是 json
spring.ai.ollama.chat.options.keep_alive 5m 控制模型在请求后在内存中加载的时间

3、自动拉取模型

拉取模型有三种策略:

  • always(定义在 PullModelStrategy.ALWAYS 中):始终拉取模型,即使它已经可用。这有助于确保您使用的是最新版本的模型。
  • when_missing(定义在 PullModelStrategy.WHEN_MISSING 中):仅当模型尚不可用时才拉取。这可能导致使用旧版本的模型。
  • never(定义在 PullModelStrategy.NEVER 中):从不自动拉取模型。

由于下载模型可能存在延迟,因此不建议在生产环境中使用自动拉取。相反,请考虑提前评估并预下载必要的模型。

所有通过配置属性和默认选项定义的模型都可以在启动时自动拉取。您可以使用配置属性来配置拉取策略、超时和最大重试次数

spring:
  ai:
    ollama:
      init:
        pull-model-strategy: always
        timeout: 60s
        max-retries: 1

可以在启动时初始化其他模型,这对于在运行时动态使用的模型很有用。

spring:
  ai:
    ollama:
      init:
        pull-model-strategy: always
        chat:
          additional-models:
            - llama3.2
            - qwen2.5

如果只想将拉取策略应用于特定类型的模型,可以将聊天模型从初始化任务中排除

spring:
  ai:
    ollama:
      init:
        pull-model-strategy: always
        chat:
          include: false

4、函数调用

这个是将LLM功能与外部工具和API连接的强大技术,Ollama可以调用Java函数,,使用方法:

@Component
public class DateTimeTools {

    @Tool(description = "获取当前时间")
    String getCurrentTime() {
        return DateTime.now().toMsStr();
    }
}

@Configuration
public class OllamaConfig {

    @Autowired
    private DateTimeTools dateTimeTools;
    @Autowired
    private JdbcChatMemoryRepository jdbcChatMemoryRepository;
    @Autowired
    private MilvusVectorStore milvusVectorStore;

    @Bean
    public ChatMemory chatMemory() {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(jdbcChatMemoryRepository).maxMessages(10).build();
    }

    @Bean
    public ChatClient chatClient(OllamaChatModel ollamaChatModel) {
        return ChatClient.builder(ollamaChatModel)
                .defaultTools(dateTimeTools) // 注册工具
                .build();
    }

}

调用LLM结果:
image

5、思考模式(推理)

Ollama 支持思考模式,用于可以发出其内部推理过程,然后提供最终答案的推理模型。此功能适用于 Qwen3、DeepSeek-v3.1、DeepSeek R1 和 GPT-OSS 等模型。

posted @ 2025-12-17 17:42  0xCAFEBABE_001  阅读(2)  评论(0)    收藏  举报