JAVA调用ollama部署的离线deepseek

当我们下载好了离线的ollama和deepseek-R1模型的时候,如何对接到java代码中?如何实现关联上下文的会话?废话不多说,直接上代码:

注意:这里我的ollama部署到另一台机子的,ollama默认安装后只允许本机访问,所以我单独运行了个端口11433来转发ollama默认的11434,如果你们是本机部署的ollama就直接用11434端口+本机ip即可

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import okhttp3.*;

import java.io.IOException;

/**
 * @author xw
 * @Date 2025/2/18 10:17
 * @Version 1.0
 */
public class Test2 {

    private static final String BASE_URL = "http://192.168.1.10:11433/v1/";
    private static final String API_KEY = "ollama";
    
    
    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();
        //创建请求体
        JSONArray messages = new JSONArray();
        messages.put(new JSONObject().put("role", "user").put("content", "请记住我叫小明"));
        messages.put(new JSONObject().put("role", "assistant").put("content", "好的,小明。请问有什么我可以帮您?"));
        messages.put(new JSONObject().put("role", "user").put("content", "我叫什么名字?"));

        JSONObject requestBody = new JSONObject();
        requestBody.put("messages", messages);
        requestBody.put("model", "deepseek-r1:32b");
        requestBody.put("stream", true);
        requestBody.put("temperature", 0.7);

        RequestBody body = RequestBody.create(MediaType.parse("application/json"), requestBody.toString());

        //创建请求体
        Request request = new Request.Builder()
                .url(BASE_URL + "chat/completions")
                .post(body)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                // 处理流式响应
                ResponseBody responseBody = response.body();
                try (okio.BufferedSource source = responseBody.source()) {
                    while (!source.exhausted()) {
                        String line = source.readUtf8Line();
                        if (line != null && !line.isEmpty()) {
                            // 移除 SSE 前缀 "data: "
                            if (line.startsWith("data: ")) {
                                line = line.substring(6);
                            }
                            // 处理 JSON 数据
                            if (!line.equals("[DONE]")) {
                                JSONObject json = new JSONObject(line);
                                // 提取响应内容
                                JSONArray choices = json.getJSONArray("choices");
                                if (choices != null && !choices.isEmpty()) {
                                    JSONObject choice = choices.getJSONObject(0);
                                    JSONObject delta = choice.getJSONObject("delta");
                                    if (delta != null) {
                                        String content = delta.getStr("content");
                                        if (content != null) {
                                            System.out.print(content);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                System.err.println("请求失败: " + response.code() + " " + response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值