$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-vector-stores-configurationProvides configuration patterns for LangChain4J vector stores in RAG applications. Use when building semantic search, integrating vector databases (PostgreSQL/pgvector, Pinecone, MongoDB, Milvus, Neo4j), implementing embedding storage/retrieval, setting up hybrid search, or optim
| 1 | # LangChain4J Vector Stores Configuration |
| 2 | |
| 3 | Configure vector stores for Retrieval-Augmented Generation applications with LangChain4J. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | LangChain4J provides a unified abstraction for vector stores (PostgreSQL/pgvector, Pinecone, MongoDB Atlas, Milvus, Neo4j) with builder-based configuration, metadata filtering, and hybrid search support. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | - Configuring vector stores for semantic search and RAG applications |
| 12 | - Setting up embedding storage with metadata filtering and hybrid search |
| 13 | - Optimizing vector database performance for production AI workloads |
| 14 | |
| 15 | ## Instructions |
| 16 | |
| 17 | ### Set Up Basic Vector Store |
| 18 | |
| 19 | Configure an embedding store for vector operations: |
| 20 | |
| 21 | ```java |
| 22 | @Bean |
| 23 | public EmbeddingStore<TextSegment> embeddingStore() { |
| 24 | return PgVectorEmbeddingStore.builder() |
| 25 | .host("localhost") |
| 26 | .port(5432) |
| 27 | .database("vectordb") |
| 28 | .user("username") |
| 29 | .password("password") |
| 30 | .table("embeddings") |
| 31 | .dimension(1536) // OpenAI embedding dimension |
| 32 | .createTable(true) |
| 33 | .useIndex(true) |
| 34 | .build(); |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | ### Validation Workflow |
| 39 | |
| 40 | Follow this workflow to ensure correct vector store setup: |
| 41 | |
| 42 | 1. **Configure**: Build the embedding store with required dimensions and connection parameters |
| 43 | 2. **Test connection**: Verify store connectivity with a health check before ingesting data |
| 44 | 3. **Validate dimensions**: Confirm embedding model dimensions match store configuration |
| 45 | 4. **Ingest test data**: Add a small batch of test documents to verify ingestion works |
| 46 | 5. **Run test query**: Execute a sample semantic search to confirm retrieval accuracy |
| 47 | 6. **Proceed to production**: Only after all steps pass, proceed with full data ingestion |
| 48 | |
| 49 | ### Configure Multiple Vector Stores |
| 50 | |
| 51 | Use different stores for different use cases: |
| 52 | |
| 53 | ```java |
| 54 | @Configuration |
| 55 | public class MultiVectorStoreConfiguration { |
| 56 | |
| 57 | @Bean |
| 58 | @Qualifier("documentsStore") |
| 59 | public EmbeddingStore<TextSegment> documentsEmbeddingStore() { |
| 60 | return PgVectorEmbeddingStore.builder() |
| 61 | .table("document_embeddings") |
| 62 | .dimension(1536) |
| 63 | .build(); |
| 64 | } |
| 65 | |
| 66 | @Bean |
| 67 | @Qualifier("chatHistoryStore") |
| 68 | public EmbeddingStore<TextSegment> chatHistoryEmbeddingStore() { |
| 69 | return MongoDbEmbeddingStore.builder() |
| 70 | .collectionName("chat_embeddings") |
| 71 | .build(); |
| 72 | } |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | ### Implement Document Ingestion |
| 77 | |
| 78 | Use EmbeddingStoreIngestor for automated document processing: |
| 79 | |
| 80 | ```java |
| 81 | @Bean |
| 82 | public EmbeddingStoreIngestor embeddingStoreIngestor( |
| 83 | EmbeddingStore<TextSegment> embeddingStore, |
| 84 | EmbeddingModel embeddingModel) { |
| 85 | |
| 86 | return EmbeddingStoreIngestor.builder() |
| 87 | .documentSplitter(DocumentSplitters.recursive( |
| 88 | 300, // maxSegmentSizeInTokens |
| 89 | 20, // maxOverlapSizeInTokens |
| 90 | new OpenAiTokenizer(GPT_3_5_TURBO) |
| 91 | )) |
| 92 | .embeddingModel(embeddingModel) |
| 93 | .embeddingStore(embeddingStore) |
| 94 | .build(); |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | ### Set Up Metadata Filtering |
| 99 | |
| 100 | Configure metadata-based filtering capabilities: |
| 101 | |
| 102 | ```java |
| 103 | // MongoDB with metadata field mapping |
| 104 | IndexMapping indexMapping = IndexMapping.builder() |
| 105 | .dimension(1536) |
| 106 | .metadataFieldNames(Set.of("category", "source", "created_date", "author")) |
| 107 | .build(); |
| 108 | |
| 109 | // Search with metadata filters |
| 110 | EmbeddingSearchRequest request = EmbeddingSearchRequest.builder() |
| 111 | .queryEmbedding(queryEmbedding) |
| 112 | .maxResults(10) |
| 113 | .filter(and( |
| 114 | metadataKey("category").isEqualTo("technical_docs"), |
| 115 | metadataKey("created_date").isGreaterThan(LocalDate.now().minusMonths(6)) |
| 116 | )) |
| 117 | .build(); |
| 118 | ``` |
| 119 | |
| 120 | ### Configure Production Settings |
| 121 | |
| 122 | Implement connection pooling and monitoring: |
| 123 | |
| 124 | ```java |
| 125 | @Bean |
| 126 | public EmbeddingStore<TextSegment> optimizedPgVectorStore() { |
| 127 | HikariConfig hikariConfig = new HikariConfig(); |
| 128 | hikariConfig.setJdbcUrl("jdbc:postgresql://localhost:5432/vectordb"); |
| 129 | hikariConfig.setUsername("username"); |
| 130 | hikariConfig.setPassword("password"); |
| 131 | hikariConfig.setMaximumPoolSize(20); |
| 132 | hikariConfig.setMinimumIdle(5); |
| 133 | hikariConfig.setConnectionTimeout(30000); |
| 134 | |
| 135 | DataSource dataSource = new HikariDataSource(hikariConfig); |
| 136 | |
| 137 | return PgVectorEmbeddingStore.builder() |
| 138 | .dataSource(dataSource) |
| 139 | .table("embeddings") |
| 140 | .dimension(1536) |
| 141 | .useIndex(true) |
| 142 | .build(); |
| 143 | } |
| 144 | ``` |
| 145 | |
| 146 | ### Implement Health Checks |
| 147 | |
| 148 | Monitor vector store connectivity: |
| 149 | |
| 150 | ```java |
| 151 | @Component |
| 152 | public class VectorStoreHealthIndicator implements HealthIndicator { |
| 153 | |
| 154 | private final EmbeddingStore<Tex |