-
Notifications
You must be signed in to change notification settings - Fork 469
feat: Add YugabyteDB embedding store examples #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krishna-yb
wants to merge
1
commit into
langchain4j:main
Choose a base branch
from
krishna-yb:feat/yugabytedb-embedding-store-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+563
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>dev.langchain4j</groupId> | ||
| <artifactId>yugabytedb-example</artifactId> | ||
| <version>1.8.0-beta15-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
|
|
||
| <dependency> | ||
| <groupId>dev.langchain4j</groupId> | ||
| <artifactId>langchain4j-community-yugabytedb</artifactId> | ||
| <version>1.8.0-beta15-SNAPSHOT</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.testcontainers</groupId> | ||
| <artifactId>testcontainers</artifactId> | ||
| <version>1.19.7</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>dev.langchain4j</groupId> | ||
| <artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId> | ||
| <version>1.7.1-beta14</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-simple</artifactId> | ||
| <version>2.0.12</version> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
| </project> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Run YugabyteDB examples as standalone applications | ||
| # This avoids exec:java classloader issues with Testcontainers | ||
|
|
||
| set -e | ||
|
|
||
| EXAMPLE_CLASS=${1:-YugabyteDBEmbeddingStoreExample} | ||
|
|
||
| echo "🚀 Building project..." | ||
| ../mvnw clean compile -q | ||
|
|
||
| echo "📦 Running example: $EXAMPLE_CLASS" | ||
| echo "" | ||
|
|
||
| # Run with proper classpath | ||
| ../mvnw exec:exec -Dexec.executable="java" \ | ||
| -Dexec.args="-cp %classpath $EXAMPLE_CLASS" \ | ||
| -q | ||
|
|
||
| echo "" | ||
| echo "✅ Example execution completed!" | ||
|
|
104 changes: 104 additions & 0 deletions
104
yugabytedb-example/src/main/java/YugabyteDBEmbeddingStoreExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import dev.langchain4j.community.store.embedding.yugabytedb.YugabyteDBEmbeddingStore; | ||
| import dev.langchain4j.community.store.embedding.yugabytedb.YugabyteDBEngine; | ||
| import dev.langchain4j.data.embedding.Embedding; | ||
| import dev.langchain4j.data.segment.TextSegment; | ||
| import dev.langchain4j.model.embedding.EmbeddingModel; | ||
| import dev.langchain4j.model.embedding.onnx.allminilml6v2.AllMiniLmL6V2EmbeddingModel; | ||
| import dev.langchain4j.store.embedding.EmbeddingMatch; | ||
| import dev.langchain4j.store.embedding.EmbeddingSearchRequest; | ||
| import dev.langchain4j.store.embedding.EmbeddingStore; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
|
|
||
| public class YugabyteDBEmbeddingStoreExample { | ||
|
|
||
| public static void main(String[] args) { | ||
| GenericContainer<?> yugabyteContainer = null; | ||
| YugabyteDBEngine engine = null; | ||
|
|
||
| try { | ||
| DockerImageName dockerImageName = DockerImageName.parse("yugabytedb/yugabyte:2025.1.0.1-b3"); | ||
| yugabyteContainer = new GenericContainer<>(dockerImageName) | ||
| .withExposedPorts(5433, 7000, 9000, 15433, 9042) | ||
| .withCommand("bin/yugabyted", "start", "--background=false") | ||
| .waitingFor(Wait.forListeningPorts(5433).withStartupTimeout(Duration.ofMinutes(5))); | ||
|
|
||
| yugabyteContainer.start(); | ||
|
|
||
| EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel(); | ||
|
|
||
| // Create YugabyteDB engine with PostgreSQL driver | ||
| engine = YugabyteDBEngine.builder() | ||
| .host(yugabyteContainer.getHost()) | ||
| .port(yugabyteContainer.getMappedPort(5433)) | ||
| .database("yugabyte") | ||
| .username("yugabyte") | ||
| .password("yugabyte") | ||
| .usePostgreSQLDriver(true) // Use PostgreSQL JDBC driver | ||
| .build(); | ||
|
|
||
| EmbeddingStore<TextSegment> embeddingStore = YugabyteDBEmbeddingStore.builder() | ||
| .engine(engine) | ||
| .tableName("test_embeddings") | ||
| .dimension(embeddingModel.dimension()) | ||
| .createTableIfNotExists(true) | ||
| .build(); | ||
|
|
||
| TextSegment segment1 = TextSegment.from("I like football."); | ||
| Embedding embedding1 = embeddingModel.embed(segment1).content(); | ||
| embeddingStore.add(embedding1, segment1); | ||
|
|
||
| TextSegment segment2 = TextSegment.from("The weather is good today."); | ||
| Embedding embedding2 = embeddingModel.embed(segment2).content(); | ||
| embeddingStore.add(embedding2, segment2); | ||
|
|
||
| Embedding queryEmbedding = embeddingModel.embed("What is your favourite sport?").content(); | ||
|
|
||
| EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder() | ||
| .queryEmbedding(queryEmbedding) | ||
| .maxResults(1) | ||
| .build(); | ||
|
|
||
| List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(embeddingSearchRequest).matches(); | ||
|
|
||
| EmbeddingMatch<TextSegment> embeddingMatch = relevant.get(0); | ||
|
|
||
| System.out.println(embeddingMatch.score()); // ~0.8144 | ||
| System.out.println(embeddingMatch.embedded().text()); // I like football. | ||
|
|
||
| System.out.println("\n✅ Example completed successfully!"); | ||
|
|
||
| } catch (Exception e) { | ||
| System.err.println("❌ Error running example: " + e.getMessage()); | ||
| e.printStackTrace(); | ||
| } finally { | ||
| // Give Testcontainers time to cleanup gracefully | ||
| try { | ||
| Thread.sleep(2000); | ||
| } catch (InterruptedException ignored) { | ||
| } | ||
|
|
||
| // Cleanup resources | ||
| System.out.println("🧹 Cleaning up resources..."); | ||
| if (engine != null) { | ||
| try { | ||
| engine.close(); | ||
| } catch (Exception e) { | ||
| System.err.println("Error closing engine: " + e.getMessage()); | ||
| } | ||
| } | ||
| if (yugabyteContainer != null) { | ||
| try { | ||
| yugabyteContainer.stop(); | ||
| } catch (Exception e) { | ||
| System.err.println("Error stopping container: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
131 changes: 131 additions & 0 deletions
131
yugabytedb-example/src/main/java/YugabyteDBEmbeddingStoreWithMetadataExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import dev.langchain4j.community.store.embedding.yugabytedb.DefaultMetadataStorageConfig; | ||
| import dev.langchain4j.community.store.embedding.yugabytedb.MetadataStorageConfig; | ||
| import dev.langchain4j.community.store.embedding.yugabytedb.MetadataStorageMode; | ||
| import dev.langchain4j.community.store.embedding.yugabytedb.YugabyteDBEmbeddingStore; | ||
| import dev.langchain4j.community.store.embedding.yugabytedb.YugabyteDBEngine; | ||
| import dev.langchain4j.data.document.Metadata; | ||
| import dev.langchain4j.data.embedding.Embedding; | ||
| import dev.langchain4j.data.segment.TextSegment; | ||
| import dev.langchain4j.model.embedding.EmbeddingModel; | ||
| import dev.langchain4j.model.embedding.onnx.allminilml6v2.AllMiniLmL6V2EmbeddingModel; | ||
| import dev.langchain4j.store.embedding.EmbeddingMatch; | ||
| import dev.langchain4j.store.embedding.EmbeddingSearchRequest; | ||
| import dev.langchain4j.store.embedding.EmbeddingStore; | ||
| import dev.langchain4j.store.embedding.filter.Filter; | ||
| import dev.langchain4j.store.embedding.filter.comparison.IsEqualTo; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
|
|
||
| public class YugabyteDBEmbeddingStoreWithMetadataExample { | ||
|
|
||
| public static void main(String[] args) { | ||
| GenericContainer<?> yugabyteContainer = null; | ||
| YugabyteDBEngine engine = null; | ||
|
|
||
| try { | ||
| DockerImageName dockerImageName = DockerImageName.parse("yugabytedb/yugabyte:2025.1.0.1-b3"); | ||
| yugabyteContainer = new GenericContainer<>(dockerImageName) | ||
| .withExposedPorts(5433, 7000, 9000, 15433, 9042) | ||
| .withCommand("bin/yugabyted", "start", "--background=false") | ||
| .waitingFor(Wait.forListeningPorts(5433).withStartupTimeout(Duration.ofMinutes(5))); | ||
|
|
||
| yugabyteContainer.start(); | ||
|
|
||
| EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel(); | ||
|
|
||
| // Create YugabyteDB engine with PostgreSQL driver | ||
| engine = YugabyteDBEngine.builder() | ||
| .host(yugabyteContainer.getHost()) | ||
| .port(yugabyteContainer.getMappedPort(5433)) | ||
| .database("yugabyte") | ||
| .username("yugabyte") | ||
| .password("yugabyte") | ||
| .usePostgreSQLDriver(true) | ||
| .build(); | ||
|
|
||
| // Configure metadata storage (JSONB format) | ||
| MetadataStorageConfig metadataConfig = DefaultMetadataStorageConfig.builder() | ||
| .storageMode(MetadataStorageMode.COMBINED_JSONB) | ||
| .build(); | ||
|
|
||
| EmbeddingStore<TextSegment> embeddingStore = YugabyteDBEmbeddingStore.builder() | ||
| .engine(engine) | ||
| .tableName("test_embeddings_with_metadata") | ||
| .dimension(embeddingModel.dimension()) | ||
| .metadataStorageConfig(metadataConfig) | ||
| .createTableIfNotExists(true) | ||
| .build(); | ||
|
|
||
| // Add embeddings with metadata | ||
| TextSegment segment1 = TextSegment.from("I like football.", | ||
| Metadata.from("category", "sports").put("user", "john")); | ||
| Embedding embedding1 = embeddingModel.embed(segment1).content(); | ||
| embeddingStore.add(embedding1, segment1); | ||
|
|
||
| TextSegment segment2 = TextSegment.from("The weather is good today.", | ||
| Metadata.from("category", "weather").put("user", "alice")); | ||
| Embedding embedding2 = embeddingModel.embed(segment2).content(); | ||
| embeddingStore.add(embedding2, segment2); | ||
|
|
||
| TextSegment segment3 = TextSegment.from("I love basketball.", | ||
| Metadata.from("category", "sports").put("user", "bob")); | ||
| Embedding embedding3 = embeddingModel.embed(segment3).content(); | ||
| embeddingStore.add(embedding3, segment3); | ||
|
|
||
| // Search with metadata filter | ||
| Embedding queryEmbedding = embeddingModel.embed("What sport do you like?").content(); | ||
|
|
||
| Filter categoryFilter = new IsEqualTo("category", "sports"); | ||
|
|
||
| EmbeddingSearchRequest searchRequest = EmbeddingSearchRequest.builder() | ||
| .queryEmbedding(queryEmbedding) | ||
| .maxResults(5) | ||
| .filter(categoryFilter) | ||
| .build(); | ||
|
|
||
| List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(searchRequest).matches(); | ||
|
|
||
| System.out.println("Found " + relevant.size() + " sports-related results:"); | ||
| for (EmbeddingMatch<TextSegment> match : relevant) { | ||
| System.out.println("Score: " + match.score()); | ||
| System.out.println("Text: " + match.embedded().text()); | ||
| System.out.println("Metadata: " + match.embedded().metadata()); | ||
| System.out.println("---"); | ||
| } | ||
|
|
||
| System.out.println("\n✅ Example completed successfully!"); | ||
|
|
||
| } catch (Exception e) { | ||
| System.err.println("❌ Error running example: " + e.getMessage()); | ||
| e.printStackTrace(); | ||
| } finally { | ||
| // Give Testcontainers time to cleanup gracefully | ||
| try { | ||
| Thread.sleep(2000); | ||
| } catch (InterruptedException ignored) { | ||
| } | ||
|
|
||
| // Cleanup resources | ||
| System.out.println("🧹 Cleaning up resources..."); | ||
| if (engine != null) { | ||
| try { | ||
| engine.close(); | ||
| } catch (Exception e) { | ||
| System.err.println("Error closing engine: " + e.getMessage()); | ||
| } | ||
| } | ||
| if (yugabyteContainer != null) { | ||
| try { | ||
| yugabyteContainer.stop(); | ||
| } catch (Exception e) { | ||
| System.err.println("Error stopping container: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update the version to the non-SNAPSHOT one. If it is not released yet, then this PR will be merged after the release
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I've created yugabyteDB as embedding store in community repo.
Link: yugabyte/langchain4j-community@90c2f0f
Here I've used this version: 1.8.0-beta15-SNAPSHOT
What should I do to progress from this point?
Thanks