langchain4j笔记-04

流式调用

模型创建
java 复制代码
public static StreamingChatModel STREAMING_BASE_MODEL = OpenAiStreamingChatModel.builder()
            .baseUrl("https://api.deepseek.com")
            .apiKey(System.getenv("DS_API_KEY"))
            .modelName("deepseek-v4-flash")
            .logRequests(true)
            .logResponses(true)
            .build();
对话
java 复制代码
    /**
     * Response Streaming
     */
    @Test
    void test01() throws InterruptedException {
        String userMessage = "给我讲一个笑话";

        STREAMING_BASE_MODEL.chat(userMessage, new StreamingChatResponseHandler() {

            // 部分响应
            @Override
            public void onPartialResponse(String partialResponse) {
                System.out.println("onPartialResponse: " + partialResponse);
            }

            // 部分思考
            @Override
            public void onPartialThinking(PartialThinking partialThinking) {
                System.out.println("onPartialThinking: " + partialThinking);
            }

            // 部分工具调用
            @Override
            public void onPartialToolCall(PartialToolCall partialToolCall) {
                System.out.println("onPartialToolCall: " + partialToolCall);
            }

            // 工具调用完成
            @Override
            public void onCompleteToolCall(CompleteToolCall completeToolCall) {
                System.out.println("onCompleteToolCall: " + completeToolCall);
            }

            // 完成调用
            @Override
            public void onCompleteResponse(ChatResponse completeResponse) {
                System.out.println("onCompleteResponse: " + completeResponse);
            }

            // 异常
            @Override
            public void onError(Throwable error) {
                error.printStackTrace();
            }
        });

        Thread.sleep(1000000000000000L);
    }
输出结果如下:
json 复制代码
{
  "aiMessage": {
    "text": "当然!这是一个经典的笑话:有一天,一只海鸥飞到一家咖啡馆,点了一杯咖啡和一块蛋糕。服务员惊讶地说:"您居然会说话?"海鸥淡定地回答:"这里的蛋糕,难道不会说话吗?"",
    "thinking": null,
    "toolExecutionRequests": [],
    "attributes": {}
  },
  "metadata": {
    "id": "8cf726c0-6b4e-4d56-8d78-9bd78228cdea",
    "modelName": "deepseek-v4-flash",
    "tokenUsage": {
      "inputTokenCount": 8,
      "inputTokensDetails": {
        "cachedTokens": 0
      },
      "outputTokenCount": 90,
      "outputTokensDetails": {
        "reasoningTokens": 44
      },
      "totalTokenCount": 98
    },
    "finishReason": "STOP",
    "created": 1778565664,
    "serviceTier": "null",
    "systemFingerprint": "fp_8b330d02d0_prod0820_fp8_kvcache_20260402",
    "rawHttpResponse": "dev.langchain4j.http.client.SuccessfulHttpResponse@662e8336",
    "rawServerSentEvents": [{
      "event": null,
      "data": "{\"id\":\"8cf726c0-6b4e-4d56-8d78-9bd78228cdea\",\"object\":\"chat.completion.chunk\",\"created\":1778565664,\"model\":\"deepseek-v4-flash\",\"system_fingerprint\":\"fp_8b330d02d0_prod0820_fp8_kvcache_20260402\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null,\"reasoning_content\":\"\"},\"logprobs\":null,\"finish_reason\":null}],\"usage\":null}"
    }],
    "logProbs": null
  }
}
简化如下:
java 复制代码
@Test
void test01() throws InterruptedException {
    String userMessage = "给我讲一个笑话";
    //StreamingChatResponseHandler handler = LambdaStreamingResponseHandler.onPartialResponse(System.out::println);
    STREAMING_BASE_MODEL.chat(userMessage, LambdaStreamingResponseHandler.onPartialResponse(System.out::println));
    STREAMING_BASE_MODEL.chat("Tell me a joke", LambdaStreamingResponseHandler.onPartialResponseAndError(System.out::print, Throwable::printStackTrace));
    Thread.sleep(1000000000000000L);
}
流的取消
java 复制代码
@Test
void test02() throws InterruptedException {
    String userMessage = "给我讲一个笑话";
    boolean shouldCancel = true;
    STREAMING_BASE_MODEL.chat(userMessage, new StreamingChatResponseHandler() {

        @Override
        public void onPartialResponse(PartialResponse partialResponse, PartialResponseContext context) {
            System.out.println("onPartialResponse: " + partialResponse);
            if (shouldCancel) {
                // 取消
                context.streamingHandle().cancel();
            }
        }

        @Override
        public void onCompleteResponse(ChatResponse completeResponse) {
            System.out.println("onCompleteResponse: " + completeResponse);
        }

        @Override
        public void onError(Throwable error) {
            error.printStackTrace();
        }
    });
}
相关推荐
LinXunFeng2 天前
Obsidian - 使用 Share Note 分享笔记并自部署
前端·笔记·github
闪闪发亮的小星星6 天前
高斯光以及高斯光公式解释
笔记
cqbzcsq6 天前
CellFlow虚拟细胞论文阅读
论文阅读·人工智能·笔记·学习·生物信息
阿米亚波6 天前
【Windows】QEMU 启动 openEuler aarch64/arm64 架构系统 + 离线软件源
linux·windows·经验分享·笔记·架构·arm
自传.6 天前
尚硅谷 Vibe Coding|第三章(1) Claude Code深度使用与进阶技巧 学习笔记
笔记·学习·尚硅谷·vibecoding
.千余6 天前
【C++】模板进阶全解:非类型参数|全特化|偏特化|分离编译完全指南
开发语言·c++·笔记·学习·其他
自传.6 天前
尚硅谷 Vibe Coding|第二章 AI编程工具生态 学习笔记
笔记·学习·ai编程·尚硅谷·vibe coding
秋波。未央6 天前
Java Agent 开发 · Day 1 学习笔记(含作业完整标准答案)
java·笔记·学习
中屹指纹浏览器6 天前
2026指纹浏览器字体指纹、字体渲染偏差检测与全维度虚拟字体池搭建方案
经验分享·笔记