docs/docs/integrations/language-models/openai-compatible.md
Many services and tools expose OpenAI-compatible APIs. The general approach to using them with LangChain4j is:
Identify the Base URL: Find the API endpoint for the service. This often ends in /v1.
Obtain an API Key: If the service requires authentication, get an API key. If the service is local and doesn't require a key, put a placeholder as the apiKey parameter.
Specify the Model Name: Determine the correct model name to use for the service. This is often required.
Configure OpenAiChatModel or OpenAiStreamingChatModel:
ChatModel model = OpenAiChatModel.builder()
.baseUrl("YOUR_API_BASE_URL") // e.g., "http://localhost:8000/v1"
.apiKey("YOUR_API_KEY_OR_PLACEHOLDER") // e.g., "sk-yourkey" or "none"
.modelName("MODEL_NAME_AS_PER_PROVIDER_DOCS") // e.g., "gpt-3.5-turbo" or custom name
// Add other configurations like temperature, timeout, etc. as needed
.logRequests(true)
.logResponses(true)
.build();
Some OpenAI-compatible APIs may have different behaviors in streaming responses, particularly for tool calling. LangChain4j provides configuration options to handle these differences:
accumulateToolCallId (for OpenAiStreamingChatModel)Controls how tool call IDs are handled in streaming responses. Default is true.
true): Tool call IDs are accumulated across streaming chunks (standard OpenAI behavior)
false): Each chunk's tool call ID replaces the previous one
StreamingChatModel model = OpenAiStreamingChatModel.builder()
.baseUrl("https://api.deepseek.com/v1") // or other provider
.apiKey("YOUR_API_KEY")
.modelName("deepseek-chat")
.accumulateToolCallId(false) // Set to false for DeepSeek, Qwen, etc.
.build();
```
Below we provide specific examples for popular OpenAI-compatible APIs, including Tuning Engines, Groq, Docker Model Runner, GPT4All, Ollama, and LM Studio.
### Contents:
- [Prerequisites for Using OpenAI-Compatible Language Models](#prerequisites-for-using-openai-compatible-language-models)
- [Tuning Engines](#tuning-engines)
- [Groq](#groq)
- [Docker Model Runner](#docker-model-runner)
- [GPT4All](#gpt4all)
- [Ollama](#ollama)
- [LM Studio](#lm-studio)
## Prerequisites for Using OpenAI-Compatible Language Models
LangChain4j's OpenAI module can be used with various OpenAI-compatible APIs, including local and cloud-based solutions. For each of the models below, we show how to create a `ChatModel` that you can then use to chat with the model, just like in the [standard OpenAI examples](https://github.com/langchain4j/langchain4j-examples/blob/main/open-ai-examples/src/main/java/OpenAiChatModelExamples.java).
First, make sure you have the OpenAI module in your `pom.xml` or Gradle build file:
### Plain Java
```xml
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.17.2</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
<version>1.17.2-beta27</version>
</dependency>
Deployment: SaaS (key required)
Description: Tuning Engines exposes an OpenAI-compatible endpoint that can sit in front of your model providers. LangChain4j keeps the application and agent logic, while the endpoint can centralize routing, policy controls, audit logs, traces, approvals, and cost visibility.
ChatModel model = OpenAiChatModel.builder()
.baseUrl("https://api.tuningengines.com/v1")
.apiKey(System.getenv("TUNING_ENGINES_API_KEY"))
.modelName("gpt-4o-mini")
.build();
Deployment: SaaS (Key Required)
Description: Groq offers very fast inference for LLMs.
Setup: To use Groq, you'll need an API key from GroqCloud.
Configure LangChain4j's OpenAiChatModel or OpenAiStreamingChatModel:
ChatModel model = OpenAiChatModel.builder()
.baseUrl("https://api.groq.com/openai/v1")
.apiKey(System.getenv("GROQ_API_KEY")) // Or your actual key
.modelName("llama3-8b-8192") // Or any other model offered by Groq, e.g., mixtral-8x7b-32768, llama3-70b-8192
.temperature(0.0)
.build();
You can find available model names on the Groq models page.
Deployment: Local
Description: Docker Model Runner allows you to run LLMs locally using Docker desktop (uses llama.cpp under the hood and can use your CPU). This is useful for development, testing, or offline use. Works on Mac and Windows.
Setup:
docker model pull ai/qwen3 or any other model from this list.Example for ai/qwen3 (more info about the model here):
ChatModel model = OpenAiChatModel.builder()
.baseUrl("http://localhost:12434/engines/llama.cpp/v1")
.modelName("ai/qwen3")
.build();
Some models support tool calling, see details on the docker model page.
Deployment: Local
Description: GPT4All provides a desktop application to run open-source LLMs locally on your machine. It can also expose an OpenAI-compatible API.
Setup:
llama-3.2-1b-instruct.http://localhost:4891/v1).ChatModel model = OpenAiChatModel.builder()
.baseUrl("http://localhost:4891/v1")
.modelName("llama-3.2-1b-instruct") // The model name might be derived from the model loaded in GPT4All UI or configurable. Check GPT4All docs.
.build();
While LangChain4j has a dedicated langchain4j-ollama module (see Ollama docs), you can also use the OpenAI module to connect to Ollama's OpenAI-compatible endpoint as shown above.
Deployment: Local
Description: Ollama allows you to run open-source large language models, such as Llama 3, Mistral, and others, locally. It provides an OpenAI-compatible API endpoint.
Setup:
ollama pull <model_name> (e.g., ollama pull gemma3).http://localhost:11434/v1/.ChatModel model = OpenAiChatModel.builder()
.baseUrl("http://localhost:11434/v1/")
.modelName("gemma3")
.build();
Examples:
Deployment: Local
Description: LM Studio provides a UI to discover, download, and run local LLMs. It also features an OpenAI-compatible local server.
Setup:
smollm2-135m-instruct.>_ on the left) and toggle the server status on to 'running'http://127.0.0.1:1234). Alternatively, the cURL call will give you the full URL.<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-http-client-jdk</artifactId>
<version>1.17.2</version>
</dependency>
httpClientBuilderimport java.net.http.HttpClient;
import dev.langchain4j.http.client.jdk.JdkHttpClientBuilder;
import dev.langchain4j.http.client.jdk.JdkHttpClient;
...
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1) ;
JdkHttpClientBuilder jdkHttpClientBuilder = JdkHttpClient.builder()
.httpClientBuilder(httpClientBuilder);
ChatModel model = OpenAiChatModel.builder()
.baseUrl("http://127.0.0.1:1234/v1")
.modelName("smollm2-135m-instruct")
.httpClientBuilder(jdkHttpClientBuilder)
.build();