
Langchain4j Rag Implementation Patterns
Copy production-oriented LangChain4j RAG patterns—in-memory stores, ingestors, and retrievers—into a Java backend that answers questions over your documents.
Overview
langchain4j-rag-implementation-patterns is an agent skill for the Build phase that provides production-ready LangChain4j RAG code examples for in-memory retrieval and OpenAI-backed Q&A.
Install
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill langchain4j-rag-implementation-patternsWhat is this skill?
- Simple in-memory RAG walkthrough with EmbeddingStoreIngestor and EmbeddingStoreContentRetriever
- OpenAI embedding (text-embedding-3-small) and chat (gpt-4o-mini) model builders with env-based API keys
- DocumentAssistant interface pattern via LangChain4j AiServices for question answering
- Production-ready framing for Retrieval-Augmented Generation with LangChain4j APIs
- Runnable Java examples suitable for extending to persistent vector stores
- Simple in-memory RAG scenario documented as section 1
Adoption & trust: 1.2k installs on skills.sh; 271 GitHub stars; 2/3 security scanners passed (skills.sh audits).
What problem does it solve?
You need a Java RAG pipeline with LangChain4j but lack a verified ingest-and-retrieve pattern wired to chat and embedding models.
Who is it for?
Solo builders writing Java/Spring-style APIs or agent backends who want copy-paste LangChain4j RAG scaffolding with OpenAI models.
Skip if: Non-JVM stacks, teams wanting only Python LangChain guides, or production deploy without your own security review of API keys and data handling.
When should I use this skill?
When implementing or extending LangChain4j RAG in a Java backend using the documented practical examples.
What do I get? / Deliverables
You can stand up a minimal DocumentAssistant flow with ingested segments and embedding retrieval, ready to harden with persistent stores and ops concerns.
- Runnable-style RAG example structure (ingest + retriever + AiServices assistant)
- Reference imports and model builder configuration
Recommended Skills
Journey fit
How it compares
A code-pattern skill for LangChain4j RAG—not an MCP server or a hosted vector DB product.
Common Questions / FAQ
Who is langchain4j-rag-implementation-patterns for?
Indie developers and small teams building Java backends who need Retrieval-Augmented Generation examples with LangChain4j and OpenAI embeddings.
When should I use langchain4j-rag-implementation-patterns?
During Build while implementing backend RAG—after you have documents to index and before you ship persistent vector storage and evaluation.
Is langchain4j-rag-implementation-patterns safe to install?
Examples call external embedding and chat APIs via API keys; review the Security Audits panel on this page and never commit secrets into repos.
SKILL.md
READMESKILL.md - Langchain4j Rag Implementation Patterns
# LangChain4j RAG Implementation - Practical Examples Production-ready examples for implementing Retrieval-Augmented Generation (RAG) systems with LangChain4j. ## 1. Simple In-Memory RAG **Scenario**: Quick RAG setup with documents in memory for development/testing. ```java import dev.langchain4j.data.document.Document; import dev.langchain4j.data.segment.TextSegment; import dev.langchain4j.model.embedding.EmbeddingModel; import dev.langchain4j.model.openai.OpenAiEmbeddingModel; import dev.langchain4j.model.openai.OpenAiChatModel; import dev.langchain4j.service.AiServices; import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore; import dev.langchain4j.store.embedding.EmbeddingStoreIngestor; import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever; interface DocumentAssistant { String answer(String question); } public class SimpleRagExample { public static void main(String[] args) { // Setup var embeddingStore = new InMemoryEmbeddingStore<TextSegment>(); var embeddingModel = OpenAiEmbeddingModel.builder() .apiKey(System.getenv("OPENAI_API_KEY")) .modelName("text-embedding-3-small") .build(); var chatModel = OpenAiChatModel.builder() .apiKey(System.getenv("OPENAI_API_KEY")) .modelName("gpt-4o-mini") .build(); // Ingest documents var ingestor = EmbeddingStoreIngestor.builder() .embeddingModel(embeddingModel) .embeddingStore(embeddingStore) .build(); ingestor.ingest(Document.from("Spring Boot is a framework for building Java applications with minimal configuration.")); ingestor.ingest(Document.from("Spring Data JPA provides data access abstraction using repositories.")); ingestor.ingest(Document.from("Spring Cloud enables building distributed systems and microservices.")); // Create retriever and AI service var contentRetriever = EmbeddingStoreContentRetriever.builder() .embeddingStore(embeddingStore) .embeddingModel(embeddingModel) .maxResults(3) .minScore(0.7) .build(); var assistant = AiServices.builder(DocumentAssistant.class) .chatModel(chatModel) .contentRetriever(contentRetriever) .build(); // Query with RAG System.out.println(assistant.answer("What is Spring Boot?")); System.out.println(assistant.answer("What does Spring Data JPA do?")); } } ``` ## 2. Vector Database RAG (Pinecone) **Scenario**: Production RAG with persistent vector database. ```java import dev.langchain4j.store.embedding.pinecone.PineconeEmbeddingStore; import dev.langchain4j.data.segment.TextSegment; import dev.langchain4j.data.document.Document; import dev.langchain4j.data.document.Metadata; public class PineconeRagExample { public static void main(String[] args) { // Production vector store var embeddingStore = PineconeEmbeddingStore.builder() .apiKey(System.getenv("PINECONE_API_KEY")) .index("docs-index") .namespace("production") .build(); var embeddingModel = OpenAiEmbeddingModel.builder() .apiKey(System.getenv("OPENAI_API_KEY")) .build(); // Ingest with metadata var ingestor = EmbeddingStoreIngestor.builder() .documentTransformer(doc -> { doc.metadata().put("source", "documentation"); doc.metadata().put("date", LocalDate.now().toString()); return doc; }) .documentSplitter(DocumentSplitters.recursive(1000, 200)) .embeddingModel(embeddingModel) .embeddingStore(embeddingStore) .build(); ingestor.ingest(Document.from("Your large document...")); // Retrieve with filters var retriever = EmbeddingStoreContentR