Customizable HTTP Client
Some LangChain4j modules (currently OpenAI and Ollama) support customizing the HTTP clients used to call the LLM provider API.
The langchain4j-http-client
module implements an HttpClient
SPI, which is used
by those modules to call the LLM provider's REST API.
This means the underlying HTTP client can be customized,
and any other HTTP client can be integrated by implementing the HttpClient
SPI.
Currently, there are 2 out-of-the-box implementations:
JdkHttpClient
in thelangchain4j-http-client-jdk
module. It is used by default when a supported module (e.g.,langchain4j-open-ai
) is used.SpringRestClient
in thelangchain4j-http-client-spring-restclient
. It is used by default when a supported module's Spring Boot starter (e.g.,langchain4j-open-ai-spring-boot-starter
) is used.
Customizing JDK's HttpClient
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder()
.sslContext(...);
JdkHttpClientBuilder jdkHttpClientBuilder = JdkHttpClient.builder()
.httpClientBuilder(httpClientBuilder);
OpenAiChatModel model = OpenAiChatModel.builder()
.httpClientBuilder(jdkHttpClientBuilder)
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4o-mini")
.build();
Customizing Spring's RestClient
RestClient.Builder restClientBuilder = RestClient.builder()
.requestFactory(new HttpComponentsClientHttpRequestFactory());
SpringRestClientBuilder springRestClientBuilder = SpringRestClient.builder()
.restClientBuilder(restClientBuilder)
.streamingRequestExecutor(new VirtualThreadTaskExecutor());
OpenAiChatModel model = OpenAiChatModel.builder()
.httpClientBuilder(springRestClientBuilder)
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4o-mini")
.build();