Springboot 如何调用本地部署Ollama中的多个模型

下面我将介绍如何在Spring Boot应用中调用本地部署的Ollama中的多个模型,包括设置依赖、配置和调用代码。

1、添加依赖

首先,在 pom.xml 中添加必要的依赖:

<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- HTTP Client -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
    
    <!-- JSON Processing -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    
    <!-- Lombok (可选) -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

2. 配置Ollama连接

application.propertiesapplication.yml中添加Ollama配置:

# application.properties
ollama.base-url=http://localhost:11434
ollama.models=llama2,gemma,mistral
ollama.timeout=30000

或者YAML格式:

# application.yml
ollama:
  base-url: http://localhost:11434
  models: llama2,gemma,mistral
  timeout: 30000

3. 创建配置类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

@Configuration
public class OllamaConfig {
    
    @Value("${ollama.base-url}")
    private String baseUrl;
    
    @Value("${ollama.models}")
    private String[] models;
    
    @Value("${ollama.timeout}")
    private int timeout;
    
    @Bean
    public CloseableHttpClient httpClient() {
        return HttpClients.createDefault();
    }
    
    // Getter methods
    public String getBaseUrl() {
        return baseUrl;
    }
    
    public String[] getModels() {
        return models;
    }
    
    public int getTimeout() {
        return timeout;
    }
}

4. 创建请求和响应DTO

import lombok.Data;
@Data
public class OllamaRequest {
    private String model;
    private String prompt;
    private boolean stream = false;
    // 可以根据需要添加其他参数
}

@Data
public class OllamaResponse {
    private String model;
    private String response;
    // 可以根据实际响应结构添加更多字段
}

5. 创建服务类调用Ollama API

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class OllamaService {
    
    private final CloseableHttpClient httpClient;
    private final OllamaConfig ollamaConfig;
    private final ObjectMapper objectMapper;
    
    @Autowired
    public OllamaService(CloseableHttpClient httpClient, OllamaConfig ollamaConfig, ObjectMapper objectMapper) {
        this.httpClient = httpClient;
        this.ollamaConfig = ollamaConfig;
        this.objectMapper = objectMapper;
    }
    
    public String generateText(String model, String prompt) throws IOException {
        String url = ollamaConfig.getBaseUrl() + "/api/generate";
        
        OllamaRequest request = new OllamaRequest();
        request.setModel(model);
        request.setPrompt(prompt);
        
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(request)));
        
        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            String responseBody = EntityUtils.toString(response.getEntity());
            OllamaResponse ollamaResponse = objectMapper.readValue(responseBody, OllamaResponse.class);
            return ollamaResponse.getResponse();
        }
    }
    
    public Map<String, String> generateWithAllModels(String prompt) {
        Map<String, String> responses = new HashMap<>();
        
        for (String model : ollamaConfig.getModels()) {
            try {
                String response = generateText(model, prompt);
                responses.put(model, response);
            } catch (IOException e) {
                responses.put(model, "Error calling model: " + e.getMessage());
            }
        }
        
        return responses;
    }
}

6. 创建控制器

import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api/ollama")
public class OllamaController {
    
    private final OllamaService ollamaService;
    
    public OllamaController(OllamaService ollamaService) {
        this.ollamaService = ollamaService;
    }
    
    @PostMapping("/generate")
    public String generateText(@RequestParam String model, @RequestParam String prompt) {
        try {
            return ollamaService.generateText(model, prompt);
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
    
    @PostMapping("/generate-all")
    public Map<String, String> generateWithAllModels(@RequestParam String prompt) {
        return ollamaService.generateWithAllModels(prompt);
    }
}

7. 使用示例

启动Spring Boot应用后,你可以通过以下方式调用:

(1)调用单个模型:

POST /api/ollama/generate?model=llama2&prompt=Tell me about AI

(2)调用所有配置的模型:

POST /api/ollama/generate-all?prompt=Explain quantum computing

8. 高级功能扩展

如果需要更复杂的功能,可以考虑:

(1)添加模型切换策略

(2)实现流式响应

(3)添加模型健康检查

(4)实现模型性能监控

例如,添加模型健康检查:

public boolean isModelAvailable(String model) throws IOException {
    String url = ollamaConfig.getBaseUrl() + "/api/tags";
    HttpGet httpGet = new HttpGet(url);
    
    try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
        String responseBody = EntityUtils.toString(response.getEntity());
        // 解析响应检查模型是否存在
        return responseBody.contains(model);
    }
}

注意事项

1、确保本地Ollama服务已启动并运行在默认端口(11434)

2、根据实际Ollama API版本调整请求和响应结构

3、在生产环境中添加适当的错误处理和日志记录

4、考虑添加请求超时和重试机制

 以上代码提供了一个完整的Spring Boot集成Ollama多个模型的实现方案,大家可以根据实际需求进行调整和扩展。

<think>好的,我现在需要帮助用户解决在SpringBoot项目中集成本地Ollama部署的DeepSeek模型,并使用流式接口进行交互的问题。首先,我得理清楚用户的需求。用户希望有一个示例,说明如何在Spring Boot中实现这一点,可能需要代码示例和配置步骤。 首先,我应该考虑Ollama的API接口是什么样的。Ollama通常提供REST API,尤其是流式响应可能使用Server-SentEvents(SSE)或者类似的流式传输方式。用户提到的是流式接口,所以需要确保SpringBoot能够处理这种流式响应。接下来,Spring Boot项目中需要添加相关的依赖。例如,可能需要SpringWeb来创建REST控制器,以及一个HTTP客户端来调用Ollama的API。如果Ollama的流式接口使用SSE,可能需要相应的库来处理SSE,比如Spring的SseEmitter或者WebClient的流式支持。 然后,需要编写一个Controller,暴露一个端点给前端,让前端可以发送请求到Spring Boot应用,SpringBoot再转发请求到本地Ollama服务,并处理流式响应,将结果逐步返回给前端。另外,配置HTTP客户端的部分也很重要,比如使用WebClient来调用Ollama的API,设置正确的URL和请求头,特别是Content-Type为application/json,可能还需要处理授权信息,如果Ollama需要的话。用户可能需要示例代码,所以需要提供一个简单的Controller和Service层的代码,展示如何接收请求,调用Ollama的流式API,并将结果通过流式方式返回给客户端。同时,需要注意异常处理和资源管理,比如正确关闭响应流。还需要考虑Ollama部署情况,确保它运行在本地正确的端口上,通常是11434,所以代码中的URL应该是http://localhost:11434/api/generate之类的路径。 可能还需要解释如何测试这个接口,比如使用curl命令或者编写前端代码来消费这个流式端点。此外,用户可能会关心如何处理多个并发请求,或者如何调整参数,比如temperature、max_tokens等模型参数。另外,参考用户提供的引用内容,虽然主要是关于Kafka和Spark的,但可能需要注意依赖管理,比如如何在pom.xml中添加必要的依赖项,比如Spring Web和ReactiveWeb相关的依赖,以支持WebClient的使用。最后,生成相关问题,可能需要考虑性能优化、异常处理、集成到现有项目中的注意事项,或者如何扩展以支持其他模型。</think>### Spring Boot集成本地Ollama DeepSeek流式接口示例 #### 1. 添加依赖 在`pom.xml`中添加Spring Web和Reactive Web支持: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> ``` #### 2. 配置Ollama接口参数 在`application.properties`中配置: ```properties ollama.api.url=http://localhost:11434/api/generate ollama.model=deepseek ``` #### 3. 实现流式交互控制器 ```java @RestController public class OllamaController { @Value("${ollama.api.url}") private String apiUrl; @PostMapping("/stream-chat") public Flux<String> streamChat(@RequestBody Map<String, Object> request) { WebClient webClient = WebClient.create(); return webClient.post() .uri(apiUrl) .contentType(MediaType.APPLICATION_JSON) .bodyValue(buildRequest(request)) .retrieve() .bodyToFlux(String.class) .map(this::extractResponse); } private Map<String, Object> buildRequest(Map<String, Object> request) { return Map.of( "model", "deepseek", "prompt", request.get("prompt"), "stream", true, "options", Map.of( "temperature", 0.7, "max_tokens", 2048 ) ); } private String extractResponse(String json) { try { JsonNode node = new ObjectMapper().readTree(json); return node.get("response").asText(); } catch (Exception e) { return "[解析错误]"; } } } ``` #### 4. 测试接口 使用curl测试流式响应: ```bash curl -X POST -H "Content-Type: application/json" -d '{"prompt":"解释量子力学"}' http://localhost:8080/stream-chat ``` #### 5. 前端集成示例(JavaScript) ```javascript const eventSource = new EventSourcePolyfill('/stream-chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: "解释相对论" }) }); eventSource.onmessage = (e) => { console.log(e.data); }; ``` #### 关键实现点说明: 1. **响应式流处理**:使用`WebClient`处理流式响应,返回`Flux<String>`保持长连接 2. **JSON解析**:使用Jackson库解析Ollama的响应格式 3. **参数配置**:支持通过配置文件调整模型参数 4. **超时处理**:默认保持30秒超时设置(可通过`@CrossOrigin`调整)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT_LOSER

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值