聊聊langchain4j的HTTP Client

news/2025/3/29 18:43:29/
http://www.w3.org/2000/svg" style="display: none;">

本文主要研究一下langchain4j的HTTP Client

httpclient_3">langchain4j-http-client

langchain4j提供了langchain4j-http-client模块,它实现了一个HttpClient SPI(服务提供者接口),其他模块通过该接口调用LLM提供商的REST API。这意味着底层HTTP客户端可以被自定义,通过实现HttpClient SPI,还可以集成任何其他HTTP客户端。目前,有两个现成的实现:

  • langchain4j-http-client-jdk模块中的JdkHttpClient。在例如langchain4j-open-ai等有引用的模块中默认使用
  • langchain4j-http-client-spring-restclient模块中的SpringRestClient。在例如langchain4j-open-ai-spring-boot-starter等有引用的spring boot starter中默认使用

HttpClient

langchain4j-http-client/src/main/java/dev/langchain4j/http/client/HttpClient.java

@Experimental
public interface HttpClient {/*** Executes a given HTTP request synchronously and returns the response.* This method blocks until the entire response is received.** @param request the HTTP request to be executed.* @return a {@link SuccessfulHttpResponse} containing the response data for successful HTTP requests (2XX status codes)* @throws HttpException    if the server returns a client (4XX) or server (5XX) error response* @throws RuntimeException if an unexpected error occurs during request execution (e.g., network issues, timeouts)*/SuccessfulHttpResponse execute(HttpRequest request) throws HttpException, RuntimeException;/*** Executes a given HTTP request asynchronously with server-sent events (SSE) handling.* This method returns immediately while processing continues on a separate thread.* Events are processed through the provided {@link ServerSentEventListener}.* <p>* The execution flow is as follows:* <ol>*   <li>The request is initiated asynchronously</li>*   <li>Received SSE data is parsed using the {@link DefaultServerSentEventParser}</li>*   <li>Parsed events are delivered to the listener's appropriate methods</li>*   <li>If an error occurs, {@link ServerSentEventListener#onError(Throwable)} is called</li>* </ol>* <p>* If any exception is thrown from the listener's methods, the stream processing* will be terminated and no further events will be processed.** @param request  the HTTP request to be executed.* @param listener the listener to receive parsed events and error notifications.*/default void execute(HttpRequest request, ServerSentEventListener listener) {execute(request, new DefaultServerSentEventParser(), listener);}/*** Executes a given HTTP request asynchronously with server-sent events (SSE) handling.* This method returns immediately while processing continues on a separate thread.* Events are processed through the provided {@link ServerSentEventListener}.* <p>* The execution flow is as follows:* <ol>*   <li>The request is initiated asynchronously</li>*   <li>Received SSE data is parsed using the provided parser</li>*   <li>Parsed events are delivered to the listener's appropriate methods</li>*   <li>If an error occurs, {@link ServerSentEventListener#onError(Throwable)} is called</li>* </ol>* <p>* If any exception is thrown from the listener's methods, the stream processing* will be terminated and no further events will be processed.** @param request  the HTTP request to be executed.* @param parser   the parser to process incoming server-sent events.* @param listener the listener to receive parsed events and error notifications.*/void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener);
}

HttpClient定义了execute方法,其中有一个支持ServerSentEventListener

HttpClientBuilderFactory

langchain4j-http-client/src/main/java/dev/langchain4j/http/client/HttpClientBuilderFactory.java

@Experimental
public interface HttpClientBuilderFactory {HttpClientBuilder create();
}

HttpClientBuilderFactory定义了create方法返回HttpClientBuilder

HttpClientBuilder

langchain4j-http-client/src/main/java/dev/langchain4j/http/client/HttpClientBuilder.java

@Experimental
public interface HttpClientBuilder {Duration connectTimeout();HttpClientBuilder connectTimeout(Duration timeout);Duration readTimeout();HttpClientBuilder readTimeout(Duration timeout);HttpClient build();
}

HttpClientBuilder定义了connectTimeout、readTimeout、build方法

HttpClientBuilderLoader

langchain4j-http-client/src/main/java/dev/langchain4j/http/client/HttpClientBuilderLoader.java

@Experimental
public class HttpClientBuilderLoader {public static HttpClientBuilder loadHttpClientBuilder() {Collection<HttpClientBuilderFactory> factories = loadFactories(HttpClientBuilderFactory.class);if (factories.size() > 1) {List<String> factoryNames = factories.stream().map(factory -> factory.getClass().getName()).toList();throw new IllegalStateException(String.format("Conflict: multiple HTTP clients have been found " +"in the classpath: %s. Please explicitly specify the one you wish to use.", factoryNames));}for (HttpClientBuilderFactory factory : factories) {return factory.create();}throw new IllegalStateException("No HTTP client has been found in the classpath");}
}

HttpClientBuilderLoader提供了loadHttpClientBuilder方法,它通过dev.langchain4j.spi.ServiceHelper.loadFactories方法来加载HttpClientBuilderFactory的实现类

ServiceHelper

langchain4j-core/src/main/java/dev/langchain4j/spi/ServiceHelper.java

    /*** Load all the services of a given type.** @param clazz the type of service* @param <T>   the type of service* @return the list of services, empty if none*/public static <T> Collection<T> loadFactories(Class<T> clazz) {return loadFactories(clazz, null);}/*** Load all the services of a given type.** <p>Utility mechanism around {@code ServiceLoader.load()}</p>** <ul>*     <li>If classloader is {@code null}, will try {@code ServiceLoader.load(clazz)}</li>*     <li>If classloader is not {@code null}, will try {@code ServiceLoader.load(clazz, classloader)}</li>*     </ul>** <p>If the above return nothing, will fall back to {@code ServiceLoader.load(clazz, $this class loader$)}</p>** @param clazz       the type of service* @param classLoader the classloader to use, may be null* @param <T>         the type of service* @return the list of services, empty if none*/public static <T> Collection<T> loadFactories(Class<T> clazz, /* @Nullable */ ClassLoader classLoader) {List<T> result;if (classLoader != null) {result = loadAll(ServiceLoader.load(clazz, classLoader));} else {// this is equivalent to:// ServiceLoader.load(clazz, TCCL);result = loadAll(ServiceLoader.load(clazz));}if (result.isEmpty()) {// By default, ServiceLoader.load uses the TCCL, this may not be enough in environment dealing with// classloaders differently such as OSGi. So we should try to use the classloader having loaded this// class. In OSGi it would be the bundle exposing vert.x and so have access to all its classes.result = loadAll(ServiceLoader.load(clazz, ServiceHelper.class.getClassLoader()));}return result;}    

ServiceHelper是对JDK自带的ServiceLoader.load()进行了封装

httpclientjdk_181">langchain4j-http-client-jdk

JdkHttpClientBuilderFactory

dev/langchain4j/http/client/jdk/JdkHttpClientBuilderFactory.java

public class JdkHttpClientBuilderFactory implements HttpClientBuilderFactory {@Overridepublic JdkHttpClientBuilder create() {return JdkHttpClient.builder();}
}

JdkHttpClientBuilderFactory的create返回的是JdkHttpClientBuilder

JdkHttpClientBuilder

dev/langchain4j/http/client/jdk/JdkHttpClientBuilder.java

public class JdkHttpClientBuilder implements HttpClientBuilder {private java.net.http.HttpClient.Builder httpClientBuilder;private Duration connectTimeout;private Duration readTimeout;public java.net.http.HttpClient.Builder httpClientBuilder() {return httpClientBuilder;}public JdkHttpClientBuilder httpClientBuilder(java.net.http.HttpClient.Builder httpClientBuilder) {this.httpClientBuilder = httpClientBuilder;return this;}@Overridepublic Duration connectTimeout() {return connectTimeout;}@Overridepublic JdkHttpClientBuilder connectTimeout(Duration connectTimeout) {this.connectTimeout = connectTimeout;return this;}@Overridepublic Duration readTimeout() {return readTimeout;}@Overridepublic JdkHttpClientBuilder readTimeout(Duration readTimeout) {this.readTimeout = readTimeout;return this;}@Overridepublic JdkHttpClient build() {return new JdkHttpClient(this);}
}

JdkHttpClientBuilder的build构建的是JdkHttpClient

JdkHttpClient

dev/langchain4j/http/client/jdk/JdkHttpClient.java

public class JdkHttpClient implements HttpClient {private final java.net.http.HttpClient delegate;private final Duration readTimeout;public JdkHttpClient(JdkHttpClientBuilder builder) {java.net.http.HttpClient.Builder httpClientBuilder =getOrDefault(builder.httpClientBuilder(), java.net.http.HttpClient::newBuilder);if (builder.connectTimeout() != null) {httpClientBuilder.connectTimeout(builder.connectTimeout());}this.delegate = httpClientBuilder.build();this.readTimeout = builder.readTimeout();}public static JdkHttpClientBuilder builder() {return new JdkHttpClientBuilder();}@Overridepublic SuccessfulHttpResponse execute(HttpRequest request) throws HttpException {try {java.net.http.HttpRequest jdkRequest = toJdkRequest(request);java.net.http.HttpResponse<String> jdkResponse = delegate.send(jdkRequest, BodyHandlers.ofString());if (!isSuccessful(jdkResponse)) {throw new HttpException(jdkResponse.statusCode(), jdkResponse.body());}return fromJdkResponse(jdkResponse, jdkResponse.body());} catch (IOException | InterruptedException e) {throw new RuntimeException(e);}}@Overridepublic void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener) {java.net.http.HttpRequest jdkRequest = toJdkRequest(request);delegate.sendAsync(jdkRequest, BodyHandlers.ofInputStream()).thenAccept(jdkResponse -> {if (!isSuccessful(jdkResponse)) {listener.onError(new HttpException(jdkResponse.statusCode(), readBody(jdkResponse)));return;}SuccessfulHttpResponse response = fromJdkResponse(jdkResponse, null);listener.onOpen(response);try (InputStream inputStream = jdkResponse.body()) {parser.parse(inputStream, listener);listener.onClose();} catch (IOException e) {throw new RuntimeException(e);}}).exceptionally(throwable -> {if (throwable.getCause() instanceof HttpTimeoutException) {listener.onError(throwable);}return null;});}//......
}    

JdkHttpClient的构造器创建了java.net.http.HttpClient作为delegate

httpclientspringrestclient_317">langchain4j-http-client-spring-restclient

SpringRestClientBuilderFactory

dev/langchain4j/http/client/spring/restclient/SpringRestClientBuilderFactory.java

public class SpringRestClientBuilderFactory implements HttpClientBuilderFactory {@Overridepublic SpringRestClientBuilder create() {return SpringRestClient.builder();}
}

SpringRestClientBuilderFactory的create返回的是SpringRestClientBuilder

SpringRestClientBuilder

dev/langchain4j/http/client/spring/restclient/SpringRestClientBuilder.java

public class SpringRestClientBuilder implements HttpClientBuilder {private RestClient.Builder restClientBuilder;private AsyncTaskExecutor streamingRequestExecutor;private Boolean createDefaultStreamingRequestExecutor = true;private Duration connectTimeout;private Duration readTimeout;public RestClient.Builder restClientBuilder() {return restClientBuilder;}public SpringRestClientBuilder restClientBuilder(RestClient.Builder restClientBuilder) {this.restClientBuilder = restClientBuilder;return this;}public AsyncTaskExecutor streamingRequestExecutor() {return streamingRequestExecutor;}public SpringRestClientBuilder streamingRequestExecutor(AsyncTaskExecutor streamingRequestExecutor) {this.streamingRequestExecutor = streamingRequestExecutor;return this;}public Boolean createDefaultStreamingRequestExecutor() {return createDefaultStreamingRequestExecutor;}public SpringRestClientBuilder createDefaultStreamingRequestExecutor(Boolean createDefaultStreamingRequestExecutor) {this.createDefaultStreamingRequestExecutor = createDefaultStreamingRequestExecutor;return this;}@Overridepublic Duration connectTimeout() {return connectTimeout;}@Overridepublic SpringRestClientBuilder connectTimeout(Duration connectTimeout) {this.connectTimeout = connectTimeout;return this;}@Overridepublic Duration readTimeout() {return readTimeout;}@Overridepublic SpringRestClientBuilder readTimeout(Duration readTimeout) {this.readTimeout = readTimeout;return this;}@Overridepublic SpringRestClient build() {return new SpringRestClient(this);}
}

SpringRestClientBuilder的build创建的是SpringRestClient

SpringRestClient

dev/langchain4j/http/client/spring/restclient/SpringRestClient.java

public class SpringRestClient implements HttpClient {private final RestClient delegate;private final AsyncTaskExecutor streamingRequestExecutor;public SpringRestClient(SpringRestClientBuilder builder) {RestClient.Builder restClientBuilder = getOrDefault(builder.restClientBuilder(), RestClient::builder);ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.DEFAULTS;if (builder.connectTimeout() != null) {settings = settings.withConnectTimeout(builder.connectTimeout());}if (builder.readTimeout() != null) {settings = settings.withReadTimeout(builder.readTimeout());}ClientHttpRequestFactory clientHttpRequestFactory = ClientHttpRequestFactories.get(settings);this.delegate = restClientBuilder.requestFactory(clientHttpRequestFactory).build();this.streamingRequestExecutor = getOrDefault(builder.streamingRequestExecutor(), () -> {if (builder.createDefaultStreamingRequestExecutor()) {return createDefaultStreamingRequestExecutor();} else {return null;}});}//......@Overridepublic SuccessfulHttpResponse execute(HttpRequest request) throws HttpException {try {ResponseEntity<String> responseEntity = toSpringRestClientRequest(request).retrieve().toEntity(String.class);return SuccessfulHttpResponse.builder().statusCode(responseEntity.getStatusCode().value()).headers(responseEntity.getHeaders()).body(responseEntity.getBody()).build();} catch (RestClientResponseException e) {throw new HttpException(e.getStatusCode().value(), e.getMessage());}}@Overridepublic void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener) {streamingRequestExecutor.execute(() -> {try {toSpringRestClientRequest(request).exchange((springRequest, springResponse) -> {int statusCode = springResponse.getStatusCode().value();if (!springResponse.getStatusCode().is2xxSuccessful()) {String body = springResponse.bodyTo(String.class);listener.onError(new HttpException(statusCode, body));return null;}SuccessfulHttpResponse response = SuccessfulHttpResponse.builder().statusCode(statusCode).headers(springResponse.getHeaders()).build();listener.onOpen(response);try (InputStream inputStream = springResponse.getBody()) {parser.parse(inputStream, listener);listener.onClose();}return null;});} catch (Exception e) {if (e.getCause() instanceof SocketTimeoutException) {listener.onError(e);}}});}}    

SpringRestClient的构造器构建了org.springframework.web.client.RestClient为delegate,同时要构建了streamingRequestExecutor用于ServerSentEventListener

小结

langchain4j的langchain4j-http-client模块定义了HttpClient SPI(服务提供者接口),目前有两个现成的实现:

  • langchain4j-http-client-jdk模块中的JdkHttpClient,使用的是java.net.http.HttpClient
  • langchain4j-http-client-spring-restclient模块中的SpringRestClient,使用的是org.springframework.web.client.RestClient

doc

  • customizable-http-client

http://www.ppmy.cn/news/1582742.html

相关文章

Ubuntu AX200 iwlwifi-cc-46.3cfab8da.0.tgz无法下载的解决办法

文章目录 前言一、检查网卡是否被识别二、确认内核模块是否可用1.AX200 wifi 要求内核5.12.检查 iwlwifi.ko 是否存在&#xff1a;3.如果未找到&#xff0c;可能是内核模块未正确生成。尝试安装 linux-modules-extra&#xff1a;4.再次检查 iwlwifi.ko 是否存在&#xff1a;5.确…

Keil5调试技巧

一、引言 Keil5作为一款广泛应用于嵌入式系统开发的集成开发环境&#xff08;IDE&#xff09;&#xff0c;在微控制器编程领域占据着重要地位。它不仅提供了强大的代码编辑和编译功能&#xff0c;还具备丰富的调试工具&#xff0c;帮助开发者快速定位和解决代码中的问题。本文…

23种设计模式-创建型模式-抽象工厂

文章目录 简介场景问题1. 风格一致性失控2. 对象创建硬编码3. 产品族管理失效 解决总结 简介 抽象工厂是一种创建型设计模式&#xff0c;可以生成相关对象系列&#xff0c;而无需指定它们的具体类。 场景 假设你正在写一个家具店模拟器。 你的代码这些类组成&#xff1a; 相…

C#与西门子PLC的六大通信库

C#与西门子PLC的六大通信库&#xff1a; 一、S7.NET S7.NET是一款开源的S7协议通信库&#xff0c;支持西门子S7通信。 二、Sharp7 Sharp7与S7.NET一样&#xff0c;是一款.NET版本的S7通信库。 三、Snap7 Snap7是一个开源的C通信库&#xff0c;支持西门子S7通信。 四、Prodave P…

31天Python入门——第9天:再学函数

你好&#xff0c;我是安然无虞。 文章目录 再学函数1. 变量在函数中的作用域2. 函数的参数传递.补充学习: 不定长参数*args和**kwargs 3. 值传递和引用传递补充学习: 把函数作为参数传递 4. 匿名函数5. python中内置的常用函数zip()map()filter()all()any() 6. 函数练习 再学函…

DeepSeek面试——模型架构和主要创新点

本文将介绍DeepSeek的模型架构多头潜在注意力&#xff08;MLA&#xff09;技术&#xff0c;混合专家&#xff08;MoE&#xff09;架构&#xff0c; 无辅助损失负载均衡技术&#xff0c;多Token 预测&#xff08;MTP&#xff09;策略。 一、模型架构 DeepSeek-R1的基本架构沿用…

【CVPR2024-工业异常检测】PromptAD方法(CLIP和提示学习)

Preliminaries 3.1. CLIP and Prompt Learning(CLIP和提示学习) CLIP核心机制 输入&#xff1a;未知图像 多组文本提示&#xff08;如 “a photo of [class]”&#xff09; 操作&#xff1a; 图像编码&#xff1a;视觉编码器 f() 提取图像特征 f(i)文本编码&#xff1a;文…

IDEA批量替换项目下所有文件中的特定内容

文章目录 1. 问题引入2. 批量替换项目下所有文件中的特定内容2.1 右键项目的根目录&#xff0c;点击在文件中替换2.2 输入要替换的内容 3. 解决替换一整行文本后出现空行的问题4. 增加筛选条件提高匹配的精确度 更多 IDEA 的使用技巧可以查看 IDEA 专栏&#xff1a; IDEA 1. 问…