# Ollama
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/04.%20%E7%89%B9%E5%AE%9A%E5%8A%9F%E8%83%BD/05.%20%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/23.%20AI/01.%20Spring%20AI%20Alibaba/%E6%A8%A1%E5%9E%8B/Ollama

Spring AI 接入 Ollama 系列模型


# Ollama

在本章节中，我们将学习如何使用 Spring AI Alibaba 接入 Ollama 系列模型。在开始学习之前，请确保您已经了解相关概念。

1. [Chat Client](../使用教程/Chat%20Client.md)；
2. [Chat Model](../使用教程/对话模型(Chat%20Model).md)；
3. [Spring AI Alibaba 快速开始](../01.%20快速开始.md)；
4. 本章节的代码您可以在 [Spring AI Alibaba Example](https://github.com/springaialibaba/spring-ai-alibaba-examples/tree/main/spring-ai-alibaba-chat-example) 仓库找到。

> 本示例主要演示如何以 ChatModel 形式接入。关于如何使用 ChatClient，请参考 Github 代码仓库示例。

## Ollama

Ollama 是一个开源的大型语言模型服务工具，旨在帮助用户快速在本地运行大模型。通过简单的安装指令，用户可以通过一条命令轻松启动和运行开源的大型语言模型。

是 LLM 领域的 Docker。

## Spring AI Alibaba 接入

在项目中引入在 Ollama 上部署的模型时，需要引入 `spring-ai-ollama-spring-boot-starter`。同时指定 Ollama 服务的 baseURL 以及运行的模型名称。

1. 引入 `spring-ai-ollama-spring-boot-starter`

    ```xml
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
        <version>1.0.0-M6</version>
    </dependency>
    ```

2. 编写 `application.yml`

    ```yaml
    spring:
      ai:
        ollama:
          base-url: http://localhost:11434
          chat:
            model: llama3
    ```

3. 遵循构造注入的方式注入 ChatModel

    ```java
    private final ChatModel ollamaChatModel;

    public OllamaChatModelController(ChatModel chatModel) {
        this.ollamaChatModel = chatModel;
    }
    ```

4. 编写控制器接口

    ```java
    @GetMapping("/simple/chat")
    public String simpleChat() {

        return ollamaChatModel.call(new Prompt(DEFAULT_PROMPT)).getResult().getOutput().getContent();
    }
    ```

至此，我们便完成 Ollama 系列模型的接入。
