当我们下载好了离线的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();
}
}
}