LangChain4j
How to use GATE/0 with LangChain4j
LangChain4j is an open-source Java library that simplifies the integration of LLMs into Java applications.
GATE/0 integrates seamlessly with LangChain4j by leveraging its support for custom API endpoints and authentication keys. This allows you to route all LLM calls through GATE/0 with minimal setup — unlocking detailed usage analytics, labeling, and cost tracking — without altering how your chains are structured.
Using GATE/0 with LangChain4j
Set up your OpenAI provider in GATE/0
Install LangChain4J
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.4.0</version>
</dependency>implementation 'dev.langchain4j:langchain4j-open-ai:1.4.0'
implementation 'dev.langchain4j:langchain4j:1.4.0'Configure the GATE/0 API key Set your GATE/0 API key as an environment variable:
export GATE0_API_KEY=your-gate0-api-keyStart using LangChain4J
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import java.util.Map;
public class LangChain4jDemo {
public static void main(String[] args) {
ChatModel chatModel = OpenAiChatModel.builder()
.baseUrl("https://gateway.gate.io/v1")
.apiKey(System.getenv("GATE0_API_KEY"))
.modelName("openai/gpt-4o")
.metadata(Map.ofEntries(
Map.entry("gate0_label_project", "alpha"),
Map.entry("gate0_label_client", "java"),
Map.entry("gate0_label_env", "prod")))
.build();
String joke = chatModel.chat("Tell me a joke about Java");
System.out.println(joke);
}
}Explanation of the implementation
An instance of ChatModel is build using OpenAiChatModel.builder(), with:
- Base URL set to
https://gateway.gate0.io/v1 - A model name prefixed with the provider slug (e.g.,
openai/gpt-4oinstead ofgpt-4o) - Your GATE/0 API key passed via headers or environment
- Custom labels passed as a
Maptometadata