安装ollama 参考上篇文章:springboot集成ollama并调用阿里大模型_springboot 集成 spring-ai-ollama-CSDN博客

步骤:

1、在ollama搜索deepseek模型 如果电脑空间够大可以选择671b的

deepseek-r1:671bDeepSeek's first-generation of reasoning models with comparable performance to OpenAI-o1, including six dense models distilled from DeepSeek-R1 based on Llama and Qwen.https://ollama.com/library/deepseek-r1:671bhttps://ollama.com/library/deepseek-r1:671bhttps://ollama.com/library/deepseek-r1:671b2、打开cmd命令,输入以下命令进行安装,安装完成即可输入问题进行对话

ollama run deepseek-r1:671b

图片安装命令是1.5b的只有1G大小,上面命令是671b的有400多G大小

安装完可以测试下:

3、集成

1、准备好一个springboot项目  我这里jdk版本使用的jdk17

 加入springboot-ai自动装配依赖jar

    <dependency>
            <groupId>io.springboot.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
            <version>1.0.3</version>
        </dependency>

yml配置文件

server:
  port: 8080
spring:
  application:
    name: spring-ai
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        options:
          model: deepseek-r1:1.5b
  autoconfigure:
    exclude:
      - org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration

controller

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaOptions;
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;

/**
 * @ClassName:OllamaChatClientController
 * @Author: heng
 * @Date: 2025/1/7 13:56
 * @Description: 使用Ollama聊天api
 */
@Slf4j
@RestController
public class OllamaChatClientController {

    @Autowired
    private  OllamaChatClient ollamaChatClient;

    @GetMapping("/ollama/chat/msg")
    public String sendollaMachat(@RequestParam String msg) {
        return ollamaChatClient.call(msg);
    }

    @GetMapping("/ollama/chat/prompt")
    public Object sendollaMachatV2(@RequestParam String msg) {
        Prompt prompt = new Prompt(msg);
        return ollamaChatClient.call(prompt);
    }

    @GetMapping("/ollama/chat/model")
    public Object sendollaMachatV3(@RequestParam String msg) {
        Prompt prompt = new Prompt(
                msg,
                OllamaOptions.create()
                        .withModel("deepseek-r1:1.5b")
                        .withTemperature(0.4F));
        ChatResponse chatResponse = ollamaChatClient.call(prompt);
        return chatResponse.getResult().getOutput().getContent();
    }
}

测试

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐