聊聊langchain4j的HTTP Client

本文主要研究一下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

HttpClientBuilderFact

### 关于 Langchain4j 的介绍 Langchain4j 是一个用于构建基于 Java 的应用程序框架,旨在简化自然语言处理 (NLP) 和机器学习模型集成到企业级应用中的过程。然而,在当前可获取的信息中,并未找到名为 “Langchain4j”的官方项目或库存在[^1]。 对于希望在 Java 中实现类似于 Python `langchain` 功能的应用开发者来说,可能需要考虑其他替代方案: - **Java 自然语言工具包(Stanford NLP for Java)**: 提供了一系列强大的文本分析功能。 - **Apache OpenNLP**: Apache 开源项目之一,专注于提供各种 NLP 工具集。 - **Deeplearning4j**: 专为 JVM 设计的分布式深度学习库,支持创建复杂的神经网络架构来进行高级的数据处理任务。 如果确实有特定需求要模仿 `langchain` 库的行为,则可以尝试通过调用 RESTful API 或者利用现有的微服务架构来间接达到目的。例如,可以在后台部署 Python 微服务作为接口层,前端则继续使用 Java 进行业务逻辑开发。 ```java // 假设这是如何在一个 Java 程序里发起 HTTP 请求给外部 Python microservice 来执行类似 langchain 的操作 import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class LangChainServiceClient { private static final String BASE_URL = "http://localhost:8080/api/v1/langchain"; public String summarizeText(String textToSummarize){ OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(BASE_URL + "/summarize") .post(RequestBody.create(MediaType.parse("text/plain"), textToSummarize)) .build(); try(Response response = client.newCall(request).execute()){ return response.body().string(); } catch (IOException e){ throw new RuntimeException(e); } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值