序
本文主要研究一下langchain4j的HTTP Client
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

最低0.47元/天 解锁文章
1622

被折叠的 条评论
为什么被折叠?



