{"repo": "charliefei/codehive-community", "files": {"codehive-ai/src/test/java/com/feirui/ai/AiChatTest.java": {"8c0cafd67addb1726da27c30b802b74d747e263f": {"t": 1785746127.5568616, "n": 4977, "h": "f08d91a3c9a31846", "c": "package com.feirui.ai;\n\nimport cn.hutool.core.util.StrUtil;\nimport cn.hutool.http.HtmlUtil;\nimport cn.hutool.http.HttpResponse;\nimport cn.hutool.http.HttpUtil;\nimport cn.hutool.http.Method;\nimport cn.hutool.json.JSONException;\nimport cn.hutool.json.JSONObject;\nimport cn.hutool.json.JSONUtil;\nimport com.feirui.ai.domain.ChatRequest;\nimport com.feirui.ai.domain.ChatResponse;\nimport lombok.extern.slf4j.Slf4j;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.http.HttpHeaders;\nimport org.springframework.http.MediaType;\nimport org.springframework.web.reactive.function.BodyInserters;\nimport org.springframework.web.reactive.function.client.WebClient;\nimport reactor.core.publisher.Flux;\n\nimport java.io.IOException;\nimport java.util.Collections;\nimport java.util.Optional;\n\n@Slf4j\npublic class AiChatTest {\n\n @Test\n public void testAIChat() {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n try {\n // 构建 deepseek 请求对象\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-chat\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .build();\n\n // 执行 HTTP 请求\n HttpResponse execute = HttpUtil.createRequest(Method.POST, \"https://api.deepseek.com/chat/completions\")\n .body(JSONUtil.toJsonStr(request))\n .header(\"Content-Type\", \"application/json\")\n .header(\"Authorization\", \"Bearer sk-xxx\")\n .header(\"Accept\", \"application/json\")\n .execute();\n\n // 获取并日志记录响应内容\n String resp = execute.body();\n\n // 解析响应并提取内容\n ChatResponse chatResponse = JSONUtil.toBean(resp, ChatResponse.class);\n System.out.println(JSONUtil.toJsonStr(chatResponse));\n String str = Optional.ofNullable(chatResponse)\n .map(ChatResponse::getChoices)\n .filter(choices -> !choices.isEmpty())\n .map(choices -> choices.get(0))\n .map(choice -> choice.getMessage().getContent())\n .orElseThrow(() -> new RuntimeException(\"Empty response\"));\n System.out.println(\"------------------------\");\n System.out.println(str);\n } catch (Exception e) {\n log.error(\"Error generating response for query: {}\", query, e);\n throw new RuntimeException(\"Failed to generate response\", e);\n }\n }\n\n @Test\n public void testAIChatStream() throws IOException {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-reasoner\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .stream(true)\n .build();\n\n Flux authorization = WebClient.builder()\n .baseUrl(\"https://api.deepseek.com/chat/completions\")\n .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)\n .defaultHeader(\"Authorization\", \"Bearer sk-xxx\")\n .build()\n .post()\n .body(BodyInserters.fromObject(request))\n .retrieve()\n .bodyToFlux(String.class)\n .flatMap(result -> {\n if (\"[DONE]\".equals(result)) {\n return Flux.empty();\n } else {\n try {\n return Flux.just(result);\n } catch (Exception e) {\n log.error(\"解析失败: {}\", result);\n }\n }\n return Flux.empty();\n });\n authorization.subscribe(res -> {\n try {\n ChatResponse response = JSONUtil.toBean(res, ChatResponse.class);\n String content = response.getChoices().get(0).getDelta().getContent();\n String reasoningContent = response.getChoices().get(0).getDelta().getReasoning_content();\n if (StrUtil.isBlank(reasoningContent)) {\n JSONObject jsonObject = new JSONObject();\n jsonObject.set(\"content\", content);\n jsonObject.set(\"thinking\", false);\n System.out.println(jsonObject);\n } else {\n JSONObject jsonObject = new JSONObject();\n jsonObject.set(\"content\", reasoningContent);\n jsonObject.set(\"thinking\", true);\n System.out.println(jsonObject);\n }\n } catch (JSONException e) {\n log.error(\"解析失败: {}\", res);\n }\n });\n\n System.in.read();\n }\n\n @Test\n public void contextLoads() {\n System.out.println(HtmlUtil.cleanHtmlTag(\"

段落


测试文本

段落

\"));\n }\n\n}\n"}, "74bb22a4b6660749dca9b6c55448662eb0097130": {"t": 1785760044.070967, "n": 4977, "h": "f08d91a3c9a31846", "c": "package com.feirui.ai;\n\nimport cn.hutool.core.util.StrUtil;\nimport cn.hutool.http.HtmlUtil;\nimport cn.hutool.http.HttpResponse;\nimport cn.hutool.http.HttpUtil;\nimport cn.hutool.http.Method;\nimport cn.hutool.json.JSONException;\nimport cn.hutool.json.JSONObject;\nimport cn.hutool.json.JSONUtil;\nimport com.feirui.ai.domain.ChatRequest;\nimport com.feirui.ai.domain.ChatResponse;\nimport lombok.extern.slf4j.Slf4j;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.http.HttpHeaders;\nimport org.springframework.http.MediaType;\nimport org.springframework.web.reactive.function.BodyInserters;\nimport org.springframework.web.reactive.function.client.WebClient;\nimport reactor.core.publisher.Flux;\n\nimport java.io.IOException;\nimport java.util.Collections;\nimport java.util.Optional;\n\n@Slf4j\npublic class AiChatTest {\n\n @Test\n public void testAIChat() {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n try {\n // 构建 deepseek 请求对象\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-chat\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .build();\n\n // 执行 HTTP 请求\n HttpResponse execute = HttpUtil.createRequest(Method.POST, \"https://api.deepseek.com/chat/completions\")\n .body(JSONUtil.toJsonStr(request))\n .header(\"Content-Type\", \"application/json\")\n .header(\"Authorization\", \"Bearer sk-xxx\")\n .header(\"Accept\", \"application/json\")\n .execute();\n\n // 获取并日志记录响应内容\n String resp = execute.body();\n\n // 解析响应并提取内容\n ChatResponse chatResponse = JSONUtil.toBean(resp, ChatResponse.class);\n System.out.println(JSONUtil.toJsonStr(chatResponse));\n String str = Optional.ofNullable(chatResponse)\n .map(ChatResponse::getChoices)\n .filter(choices -> !choices.isEmpty())\n .map(choices -> choices.get(0))\n .map(choice -> choice.getMessage().getContent())\n .orElseThrow(() -> new RuntimeException(\"Empty response\"));\n System.out.println(\"------------------------\");\n System.out.println(str);\n } catch (Exception e) {\n log.error(\"Error generating response for query: {}\", query, e);\n throw new RuntimeException(\"Failed to generate response\", e);\n }\n }\n\n @Test\n public void testAIChatStream() throws IOException {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-reasoner\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .stream(true)\n .build();\n\n Flux authorization = WebClient.builder()\n .baseUrl(\"https://api.deepseek.com/chat/completions\")\n .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)\n .defaultHeader(\"Authorization\", \"Bearer sk-xxx\")\n .build()\n .post()\n .body(BodyInserters.fromObject(request))\n .retrieve()\n .bodyToFlux(String.class)\n .flatMap(result -> {\n if (\"[DONE]\".equals(result)) {\n return Flux.empty();\n } else {\n try {\n return Flux.just(result);\n } catch (Exception e) {\n log.error(\"解析失败: {}\", result);\n }\n }\n return Flux.empty();\n });\n authorization.subscribe(res -> {\n try {\n ChatResponse response = JSONUtil.toBean(res, ChatResponse.class);\n String content = response.getChoices().get(0).getDelta().getContent();\n String reasoningContent = response.getChoices().get(0).getDelta().getReasoning_content();\n if (StrUtil.isBlank(reasoningContent)) {\n JSONObject jsonObject = new JSONObject();\n jsonObject.set(\"content\", content);\n jsonObject.set(\"thinking\", false);\n System.out.println(jsonObject);\n } else {\n JSONObject jsonObject = new JSONObject();\n jsonObject.set(\"content\", reasoningContent);\n jsonObject.set(\"thinking\", true);\n System.out.println(jsonObject);\n }\n } catch (JSONException e) {\n log.error(\"解析失败: {}\", res);\n }\n });\n\n System.in.read();\n }\n\n @Test\n public void contextLoads() {\n System.out.println(HtmlUtil.cleanHtmlTag(\"

段落


测试文本

段落

\"));\n }\n\n}\n"}, "e6820ed6dcf24fbbb4e4a4b45412490fcc45f577": {"t": 1785760045.5169961, "n": 3930, "h": "70fb3d32f609a6d8", "c": "package com.feirui.ai;\n\nimport cn.hutool.http.HtmlUtil;\nimport cn.hutool.http.HttpResponse;\nimport cn.hutool.http.HttpUtil;\nimport cn.hutool.http.Method;\nimport cn.hutool.json.JSONUtil;\nimport com.feirui.ai.domain.ChatRequest;\nimport com.feirui.ai.domain.ChatResponse;\nimport lombok.extern.slf4j.Slf4j;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.http.HttpHeaders;\nimport org.springframework.http.MediaType;\nimport org.springframework.web.reactive.function.BodyInserters;\nimport org.springframework.web.reactive.function.client.WebClient;\nimport reactor.core.publisher.Flux;\n\nimport java.io.IOException;\nimport java.util.Collections;\nimport java.util.Optional;\n\n@Slf4j\npublic class AiChatTest {\n\n @Test\n public void testAIChat() {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n try {\n // 构建 deepseek 请求对象\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-chat\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .build();\n\n // 执行 HTTP 请求\n HttpResponse execute = HttpUtil.createRequest(Method.POST, \"https://api.deepseek.com/chat/completions\")\n .body(JSONUtil.toJsonStr(request))\n .header(\"Content-Type\", \"application/json\")\n .header(\"Authorization\", \"Bearer sk-xxx\")\n .header(\"Accept\", \"application/json\")\n .execute();\n\n // 获取并日志记录响应内容\n String resp = execute.body();\n\n // 解析响应并提取内容\n ChatResponse chatResponse = JSONUtil.toBean(resp, ChatResponse.class);\n System.out.println(JSONUtil.toJsonStr(chatResponse));\n String str = Optional.ofNullable(chatResponse)\n .map(ChatResponse::getChoices)\n .filter(choices -> !choices.isEmpty())\n .map(choices -> choices.get(0))\n .map(choice -> choice.getMessage().getContent())\n .orElseThrow(() -> new RuntimeException(\"Empty response\"));\n System.out.println(\"------------------------\");\n System.out.println(str);\n } catch (Exception e) {\n log.error(\"Error generating response for query: {}\", query, e);\n throw new RuntimeException(\"Failed to generate response\", e);\n }\n }\n\n @Test\n public void testAIChatStream() throws IOException {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-chat\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .stream(true)\n .build();\n\n Flux authorization = WebClient.builder()\n .baseUrl(\"https://api.deepseek.com/chat/completions\")\n .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)\n .defaultHeader(\"Authorization\", \"Bearer sk-xxx\")\n .build()\n .post()\n .body(BodyInserters.fromObject(request))\n .retrieve()\n .bodyToFlux(String.class)\n .flatMap(result -> {\n if (\"[DONE]\".equals(result)) {\n return Flux.empty();\n } else {\n try {\n return Flux.just(result);\n } catch (Exception e) {\n log.error(\"解析失败: {}\", result);\n }\n }\n return Flux.empty();\n });\n authorization.subscribe(res -> log.info(\"最终响应:{}\", res));\n\n System.in.read();\n }\n\n @Test\n public void contextLoads() {\n System.out.println(HtmlUtil.cleanHtmlTag(\"

段落


测试文本

段落

\"));\n }\n\n}\n"}, "eb83640b5259c1595ecc59107be7acbc86cf2ecc": {"t": 1785760046.852695, "n": 3742, "h": "b31d1179de0427d9", "c": "package com.feirui.ai;\n\nimport cn.hutool.http.HttpResponse;\nimport cn.hutool.http.HttpUtil;\nimport cn.hutool.http.Method;\nimport cn.hutool.json.JSONUtil;\nimport com.feirui.ai.domain.ChatRequest;\nimport com.feirui.ai.domain.ChatResponse;\nimport lombok.extern.slf4j.Slf4j;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.http.HttpHeaders;\nimport org.springframework.http.MediaType;\nimport org.springframework.web.reactive.function.BodyInserters;\nimport org.springframework.web.reactive.function.client.WebClient;\nimport reactor.core.publisher.Flux;\n\nimport java.io.IOException;\nimport java.util.Collections;\nimport java.util.Optional;\n\n@Slf4j\npublic class AiChatTest {\n\n @Test\n public void testAIChat() {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n try {\n // 构建 deepseek 请求对象\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-chat\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .build();\n\n // 执行 HTTP 请求\n HttpResponse execute = HttpUtil.createRequest(Method.POST, \"https://api.deepseek.com/chat/completions\")\n .body(JSONUtil.toJsonStr(request))\n .header(\"Content-Type\", \"application/json\")\n .header(\"Authorization\", \"Bearer sk-xxx\")\n .header(\"Accept\", \"application/json\")\n .execute();\n\n // 获取并日志记录响应内容\n String resp = execute.body();\n\n // 解析响应并提取内容\n ChatResponse chatResponse = JSONUtil.toBean(resp, ChatResponse.class);\n System.out.println(JSONUtil.toJsonStr(chatResponse));\n String str = Optional.ofNullable(chatResponse)\n .map(ChatResponse::getChoices)\n .filter(choices -> !choices.isEmpty())\n .map(choices -> choices.get(0))\n .map(choice -> choice.getMessage().getContent())\n .orElseThrow(() -> new RuntimeException(\"Empty response\"));\n System.out.println(\"------------------------\");\n System.out.println(str);\n } catch (Exception e) {\n log.error(\"Error generating response for query: {}\", query, e);\n throw new RuntimeException(\"Failed to generate response\", e);\n }\n }\n\n @Test\n public void testAIChatStream() throws IOException {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-chat\")\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .stream(true)\n .build();\n\n Flux authorization = WebClient.builder()\n .baseUrl(\"https://api.deepseek.com/chat/completions\")\n .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)\n .defaultHeader(\"Authorization\", \"Bearer sk-xxx\")\n .build()\n .post()\n .body(BodyInserters.fromObject(request))\n .retrieve()\n .bodyToFlux(String.class)\n .flatMap(result -> {\n if (\"[DONE]\".equals(result)) {\n return Flux.empty();\n } else {\n try {\n return Flux.just(result);\n } catch (Exception e) {\n log.error(\"解析失败: {}\", result);\n }\n }\n return Flux.empty();\n });\n authorization.subscribe(res -> log.info(\"最终响应:{}\", res));\n\n System.in.read();\n }\n\n}\n"}, "ed3d6aca57bbe2e0ebeb101a1fc31b887d299e45": {"t": 1785760048.19981, "n": 2078, "h": "fd9078ee0ca3d630", "c": "package com.feirui.ai;\n\nimport cn.hutool.http.HttpResponse;\nimport cn.hutool.http.HttpUtil;\nimport cn.hutool.http.Method;\nimport cn.hutool.json.JSONUtil;\nimport lombok.extern.slf4j.Slf4j;\nimport org.junit.jupiter.api.Test;\n\nimport java.util.Collections;\nimport java.util.Optional;\n\n@Slf4j\npublic class AiChatTest {\n\n @Test\n public void testAIChat() {\n String query = \"帮我用java解决力扣上的接雨水问题\";\n try {\n // 构建 deepseek 请求对象\n ChatRequest request = ChatRequest.builder()\n .model(\"deepseek-chat\")\n .temperature(0.7)\n .max_tokens(1000)\n .messages(Collections.singletonList(new ChatRequest.Message(\"user\", query)))\n .build();\n\n // 执行 HTTP 请求\n HttpResponse execute = HttpUtil.createRequest(Method.POST, \"https://api.deepseek.com/chat/completions\")\n .body(JSONUtil.toJsonStr(request))\n .header(\"Content-Type\", \"application/json\")\n .header(\"Authorization\", \"Bearer sk-xxx\")\n .header(\"Accept\", \"application/json\")\n .execute();\n\n // 获取并日志记录响应内容\n String resp = execute.body();\n\n // 解析响应并提取内容\n ChatResponse chatResponse = JSONUtil.toBean(resp, ChatResponse.class);\n System.out.println(JSONUtil.toJsonStr(chatResponse));\n String str = Optional.ofNullable(chatResponse)\n .map(ChatResponse::getChoices)\n .filter(choices -> !choices.isEmpty())\n .map(choices -> choices.get(0))\n .map(choice -> choice.getMessage().getContent())\n .orElseThrow(() -> new RuntimeException(\"Empty response\"));\n System.out.println(\"------------------------\");\n System.out.println(str);\n } catch (Exception e) {\n log.error(\"Error generating response for query: {}\", query, e);\n throw new RuntimeException(\"Failed to generate response\", e);\n }\n }\n\n}\n"}}}}