langchain4j/langchain4j
Sobre
LangChain4j é uma biblioteca Java idiomática e de código aberto para criar aplicações baseadas em LLM na JVM. Ela oferece uma API unificada para provedores de LLM e bancos de dados vetoriais populares, além de facilitar a implementação de chamadas de ferramentas (incluindo suporte a MCP), agentes e RAG. Integra-se perfeitamente a frameworks Java corporativos, como Quarkus e Spring Boot.
Desenvolvimento web e de aplicativosO que você pode fazer
- AI Services: declare a Java interface and LangChain4j implements it. Methods can return text, your own classes and records, or a stream of tokens.
- Tools and MCP: let the model call your Java methods and the tools of Model Context Protocol servers.
- RAG (retrieval-augmented generation): load, split, embed and retrieve your documents, from a few lines of "Easy RAG" to a fully customized pipeline.
- Agents: workflows (sequence, loop, parallel, conditional), supervisor agents, more agentic patterns, human-in-the-loop steps, A2A, and runs that can be suspended, persisted and resumed.
Crescimento medido de estrelas
- em 24 h
- +5
- em 7 dias
- +28
- em 15 dias
- +57
- em 1 mês
- +126
Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.
README.md
OriginalLangChain4j
LangChain4j is an open-source Java library for building applications and agents powered by large language models (LLMs).
- One API for many providers: 20+ LLM providers and 40+ embedding (vector) stores. Switch between them without rewriting your code.
- Building blocks that feel like plain Java: AI Services, tools, MCP, RAG, agents, guardrails, structured outputs, chat memory, observability and testing support.
- Works where your code runs: plain Java, Quarkus, Spring Boot, Helidon, Micronaut and Jakarta EE.
Quick start
Add the dependencies (Java 17 or newer):
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>1.24.1</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.24.1</version>
</dependency>
Describe what you need as a Java interface, give the model a tool, and let LangChain4j do the rest:
interface Assistant {
@SystemMessage("You are a friendly support agent of an online shop.")
String chat(String userMessage);
}
class OrderTools {
@Tool("Returns the delivery status of an order")
String getOrderStatus(String orderId) {
return "Order " + orderId + ": shipped, arrives tomorrow";
}
}
ChatModel model = OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-6-luna")
.reasoningEffort("none") // tools with this model need it, see Get Started
.build();
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(model)
.tools(new OrderTools())
.build();
String answer = assistant.chat("Where is my order 12345?");
// Your order 12345 has shipped and is expected to arrive tomorrow.
LangChain4j implements the Assistant interface for you. It sends the message to the model, runs getOrderStatus
when the model asks for it, and returns the model's final answer. To use another provider, such as a local model with
Ollama, you add its dependency and change how model is created. The model and its integration need to support the
features you use, such as tool calling: see the comparison table.
Continue with the Get Started guide, which also shows the complete program, and the AI Services tutorial. Using Quarkus, Spring Boot, Helidon or Micronaut? See Frameworks.
Features
- AI Services: declare a Java interface and LangChain4j implements it. Methods can return text, your own classes and records, or a stream of tokens.
- Tools and MCP: let the model call your Java methods and the tools of Model Context Protocol servers.
- RAG (retrieval-augmented generation): load, split, embed and retrieve your documents, from a few lines of "Easy RAG" to a fully customized pipeline.
- Agents: workflows (sequence, loop, parallel, conditional), supervisor agents, more agentic patterns, human-in-the-loop steps, A2A, and runs that can be suspended, persisted and resumed.
- Structured outputs: get answers as Java objects.
- Chat memory: keep the conversation, in memory or in a store.
- Guardrails: check inputs and outputs, and let the model retry; tool guardrails check the calls and the results of tools.
- Streaming and non-blocking calls.
- Multimodal inputs: text, images, audio, video and PDF, depending on the model and the integration (see the comparison table).
- Observability: listeners for model calls, tools, RAG and AI Service events, and Micrometer metrics.
- Testing: mock models for fast unit tests, and checks that use a model as a judge.
- Decision services: ask typed questions (yes/no, one of several options) through plain Java interfaces.
Integrations
- LLM providers: OpenAI, Anthropic, Google Gemini, Amazon Bedrock, Azure OpenAI, Mistral AI, Ollama and many more.
- Embedding (vector) stores: PostgreSQL (pgvector), Elasticsearch, OpenSearch, MongoDB Atlas, Redis, Milvus, Qdrant, Pinecone, Weaviate, Chroma and many more.
- Also: embedding models (including models that run inside your JVM), document loaders and parsers, re-ranking models, image models, web search engines and more.
Many integrations are maintained by the community in the langchain4j-community repository.
Frameworks
- Quarkus: the Quarkus LangChain4j extension (samples).
- Spring Boot: Spring Boot starters for Spring Boot 3.5+ and 4.
- Helidon: Helidon integration.
- Micronaut: Micronaut LangChain4j.
- Jakarta EE: LangChain4j CDI.
- Kotlin: coroutine support.
Built on LangChain4j
Projects maintained by a foundation or a well-known company that use LangChain4j in a released version:
- Apache Camel: LangChain4j components for integration routes.
- Camunda: the AI Agent connector for process orchestration.
- Kestra: the AI plugin for workflow orchestration.
- WildFly: the AI feature pack.
Versions and compatibility
- LangChain4j needs Java 17 or newer.
- A new minor version is released about every three weeks. Release notes.
- Use the
langchain4j-bomto keep the versions of the LangChain4j modules in line, including the Spring Boot starters. Community integrations have their ownlangchain4j-community-bom. The Get Started guide shows how. With Quarkus, use the LangChain4j version that the Quarkus platform brings. - Modules with a
-betaversion (for example1.24.1-beta34) and APIs marked@Experimentalcan still change in a future release.
Learn
- Documentation and Javadoc
- Ask the documentation chatbot (experimental)
- Examples: plain Java, Quarkus, Spring Boot, Helidon and more, and the Quarkus LangChain4j samples
- LangChain4j for Beginners, a free course by Microsoft
- Useful materials
- Using a coding agent? Point it to llms.txt, or add
.mdto the address of any documentation page.
Not a port of LangChain
Despite the name, LangChain4j is not a Java port of LangChain (Python). It is designed from the ground up for Java: type safety, POJOs, annotations, interfaces, dependency injection and fluent APIs. Its API, internals and release cycle are independent of the Python project.
Get help
Ask on Discord or in GitHub Discussions. To request a feature or report a bug, open an issue. To report a security vulnerability, follow SECURITY.md and do not open a public issue.
Contribute
See the contribution guidelines.
License
LangChain4j is released under the Apache License 2.0.
Explore projetos relacionados
Carregando…