LangChain4j

website·Indexed 31 days ago

https://langchain4j.cn

A Java framework designed to simplify the integration of Large Language Models (LLMs) and vector databases into Java applications. It provides a unified API for various providers including OpenAI, Google AI Gemini, Amazon Bedrock, and Anthropic, and supports advanced AI patterns such as Retrieval-Augmented Generation (RAG), AI Agents, tool use (function calling), and structured JSON outputs. It offers deep integration with Spring Boot, Quarkus, Micronaut, and Helidon.

Tokens
44K
Snippets
189
Records
248
Agent score
53%

What's inside LangChain4j

  1. Overview of Retrieval-Augmented Generation (RAG) in LangChain4j

    RAG is a technique used to provide LLMs with domain-specific or proprietary knowledge by retrieving relevant information from a data source and injecting it into the prompt before sending it to the LLM. This reduces hallucinations and allows the model to answer questions based on data it wasn't trained on.

    LangChain4j supports three RAG styles:

    1. Easy RAG: The fastest way to start, abstracting away embeddings and splitting.
    2. Naive RAG: A basic implementation using vector search.
    3. Advanced RAG: A modular framework supporting query transformation, multi-source retrieval, and re-ranking.

    Common retrieval methods include Full-text (keyword) search, Vector (semantic) search, and Hybrid search (a combination of both). Currently, full-text and hybrid search are primarily supported via the AzureAiSearchContentRetriever.

  2. Overview of LangChain4j ChatMemory

    ChatMemory is an abstraction in LangChain4j that acts as a container for ChatMessage objects. It simplifies the manual management of conversation history by providing built-in support for eviction strategies, persistence, and special handling of SystemMessage and tool-related messages. It can be used as a standalone low-level component or integrated into high-level components like AI Services.
  3. Overview of Guardrails in LangChain4j

    Guardrails are a mechanism used to validate LLM inputs and outputs to ensure they meet specific constraints or business rules. Common use cases include:

    • Validating that user input is within allowed boundaries.
    • Defending against prompt injection attacks.
    • Ensuring output formats are correct (e.g., valid JSON).
    • Enforcing business rules (e.g., preventing a company chatbot from mentioning competitors).
    • Detecting hallucinations.
  4. Overview of LangChain4j capabilities

    LangChain4j is a Java framework that simplifies LLM integration by providing:

    1. Unified APIs: A consistent interface for 15+ LLM providers (e.g., OpenAI, Google Vertex AI) and 20+ vector databases (e.g., Pinecone, Milvus), allowing developers to switch providers without rewriting code.
    2. Comprehensive Toolkit: Out-of-the-box implementations for prompt templates, chat memory management, function calling, Agents, and Retrieval Augmented Generation (RAG).
    3. Framework Integration: Native support for Quarkus, Spring Boot, Helidon, and Micronaut.
  5. Overview of LangChain4j for Java LLM Integration

    LangChain4j provides a unified API to interact with mainstream commercial and open-source Large Language Models (LLMs) and Vector Stores. It is specifically designed for the Java ecosystem, allowing developers to build chatbots and intelligent assistants with a simplified integration process. Key capabilities include bidirectional integration, where Java can call LLMs and LLMs can invoke Java code.
  6. Overview of Customizable HTTP Clients in LangChain4j

    Certain LangChain4j modules (currently OpenAI and Ollama) allow customization of the HTTP client used to call LLM provider APIs. This is achieved via the HttpClient SPI provided by the langchain4j-http-client module. There are two primary out-of-the-box implementations: JdkHttpClient (default for standard modules like langchain4j-open-ai) and SpringRestClient (default for Spring Boot starters like langchain4j-open-ai-spring-boot-starter).
  7. Overview of Tool Use (Function Calling) in LangChain4j

    Tool use allows an LLM to express an intent to call a pre-defined function (like a web search or API call) instead of returning plain text. The LLM does not execute the tool itself; it returns a request for execution. The developer must execute the tool and provide the result back to the LLM to complete the loop. This mechanism extends the LLM's capabilities, such as improving mathematical accuracy or enabling external actions like sending emails.
  8. Integrate LangChain4j with Payara Micro

    LangChain4j can be integrated into Payara Micro applications by leveraging Jakarta EE and MicroProfile standards for dependency injection and configuration. This allows developers to create JAX-RS resources that instantiate and use AI models while managing settings via MicroProfile Config.
  9. Enable automatic content moderation in AI Services

    AI Services can automatically audit content using a moderationModel. If inappropriate content is detected, a ModerationException is thrown, containing a Moderation object with details about the flagged text.
    Assistant assistant = AiServices.builder(Assistant.class)
        .chatModel(model)
        .moderationModel(moderationModel)
        .build();
  10. Use ChatModel for low-level LLM interaction

    The ChatModel interface is the primary low-level API for interacting with Large Language Models in LangChain4j. It is preferred over the simpler LanguageModel (which only handles String-to-String) because it supports multiple message types and multi-modal inputs. ChatModel provides flexibility for advanced configurations like temperature, tool specifications, and JSON schemas.
    public interface ChatModel {
        // Simple convenience method
        String chat(String userMessage);
        
        // Standard methods for structured conversations
        ChatResponse chat(ChatMessage... messages);
        ChatResponse chat(List<ChatMessage> messages);
        
        // Advanced method for full request customization
        ChatResponse chat(ChatRequest chatRequest);
    }
  11. Use InMemoryEmbeddingStore for rapid prototyping

    The InMemoryEmbeddingStore is a simple implementation of the EmbeddingStore interface that stores Embedding and associated TextSegment objects in memory. It is ideal for rapid prototype development and simple use cases where a full vector database is not required. Search operations are performed directly in memory.
  12. Understand AgenticScope for shared state

    AgenticScope is a shared data collection used by multiple Agents within an agentic system. It allows Agents to collaborate by writing results to shared variables and reading results produced by other Agents. Additionally, AgenticScope automatically tracks the sequence of Agent calls and their respective responses. It is created automatically when the main Agent of an agentic system is invoked.