MongoDB: added javadoc, updated IT to use env vars

This commit is contained in:
LangChain4j 2024-02-08 10:38:59 +01:00
parent c694755cc3
commit 1e6f5bc316
3 changed files with 54 additions and 10 deletions

View File

@ -45,6 +45,9 @@ jobs:
HF_API_KEY: ${{ secrets.HF_API_KEY }}
MILVUS_API_KEY: ${{ secrets.MILVUS_API_KEY }}
MISTRAL_AI_API_KEY: ${{ secrets.MISTRAL_AI_API_KEY }}
MONGODB_ATLAS_USERNAME: ${{ secrets.MONGODB_ATLAS_USERNAME }}
MONGODB_ATLAS_PASSWORD: ${{ secrets.MONGODB_ATLAS_PASSWORD }}
MONGODB_ATLAS_HOST: ${{ secrets.MONGODB_ATLAS_HOST }}
NOMIC_API_KEY: ${{ secrets.NOMIC_API_KEY }}
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
WEAVIATE_API_KEY: ${{ secrets.WEAVIATE_API_KEY }}

View File

@ -45,9 +45,41 @@ import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
/**
* Represents a <a href="https://www.mongodb.com/">MongoDB</a> index as an embedding store.
* <p>
* More <a href="https://www.mongodb.com/docs/atlas/atlas-search/field-types/knn-vector/">info</a> to set up MongoDb as vectorDatabase
* More <a href="https://www.mongodb.com/docs/atlas/atlas-search/field-types/knn-vector/">info</a>
* to set up MongoDb as vectorDatabase.
* <p>
* <a href="https://www.mongodb.com/developer/products/atlas/semantic-search-mongodb-atlas-vector-search/">tutorial</a> how to use a knn-vector in MongoDB Atlas (great startingpoint)
* <a href="https://www.mongodb.com/developer/products/atlas/semantic-search-mongodb-atlas-vector-search/">tutorial</a>
* how to use a knn-vector in MongoDB Atlas (great starting point).
* <p>
* If you are using a free tier, {@code #createIndex = true} might not be supported,
* so you will need to create an index manually.
* In your Atlas web console go to: DEPLOYMENT -> Database -> {your cluster} -> Atlas Search -> Create Index Search
* -> "JSON Editor" under "Atlas Search" -> Next -> Select your database in the left pane
* -> Insert the following JSON into the right pane (set "dimensions" and "metadata"->"fields" to desired values)
* <pre>
* {
* "mappings": {
* "dynamic": false,
* "fields": {
* "embedding": {
* "dimensions": 384,
* "similarity": "cosine",
* "type": "knnVector"
* },
* "metadata": {
* "dynamic": false,
* "fields": {
* "test-key": {
* "type": "token"
* }
* },
* "type": "document"
* }
* }
* }
* }
* </pre>
* -> Next -> Create Search Index
*/
public class MongoDbEmbeddingStore implements EmbeddingStore<TextSegment> {

View File

@ -16,28 +16,37 @@ import org.bson.codecs.pojo.PojoCodecProvider;
import org.bson.conversions.Bson;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import static java.lang.String.format;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
@Disabled("Need Cloud Mongo Atlas Credential")
@EnabledIfEnvironmentVariable(named = "MONGODB_ATLAS_USERNAME", matches = ".+")
class MongoDbEmbeddingStoreCloudIT extends EmbeddingStoreIT {
private static final String DATABASE_NAME = "test_database";
private static final String COLLECTION_NAME = "test_collection";
private static final String INDEX_NAME = "default";
static MongoClient client;
MongoDbEmbeddingStore embeddingStore = MongoDbEmbeddingStore.builder()
.fromClient(client)
.databaseName("test_database")
.collectionName("test_collection")
.indexName("test_index")
.databaseName(DATABASE_NAME)
.collectionName(COLLECTION_NAME)
.indexName(INDEX_NAME)
.build();
EmbeddingModel embeddingModel = new AllMiniLmL6V2QuantizedEmbeddingModel();
@BeforeAll
static void beforeAll() {
client = MongoClients.create("mongodb+srv://<username>:<password>@<host>/?retryWrites=true&w=majority");
String username = System.getenv("MONGODB_ATLAS_USERNAME");
String password = System.getenv("MONGODB_ATLAS_PASSWORD");
String host = System.getenv("MONGODB_ATLAS_HOST");
String connectionString = format("mongodb+srv://%s:%s@%s/?retryWrites=true&w=majority", username, password, host);
client = MongoClients.create(connectionString);
}
@AfterAll
@ -62,8 +71,8 @@ class MongoDbEmbeddingStoreCloudIT extends EmbeddingStoreIT {
.build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoCollection<MongoDbDocument> collection = client.getDatabase("test_database")
.getCollection("test_collection", MongoDbDocument.class)
MongoCollection<MongoDbDocument> collection = client.getDatabase(DATABASE_NAME)
.getCollection(COLLECTION_NAME, MongoDbDocument.class)
.withCodecRegistry(codecRegistry);
Bson filter = Filters.exists("embedding");