Maven dependency problem between spring data mongo and langchain4j mongo atlas

I am trying to integrate LangChain4j to my spring boot app that, by the way, is using Spring Data mongo. One of the things I realize (obviously) is that I dont want to use an in memory store bur rather a mongo store for RAG. And this is where I am facing an error I dont know how to solve it. Here are the details:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.6</version>
</parent>

<dependencies>
    <dependency>
        <groupId>com.vaadin</groupId>
        <!-- Replace artifactId with vaadin-core to use only free components -->
        <artifactId>vaadin</artifactId>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.parttio</groupId>
        <artifactId>line-awesome</artifactId>
        <version>2.1.0</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-testbench-junit5</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j</artifactId>
        <version>0.36.2</version>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-spring-boot-starter</artifactId>
        <version>0.32.0</version>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-easy-rag</artifactId>
        <version>0.36.2</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.vaadin.addons.flowingcode</groupId>
        <artifactId>chat-assistant-addon</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-ollama</artifactId>
        <version>0.36.2</version>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-mongodb-atlas</artifactId>
        <version>0.36.2</version>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
        <version>0.36.2</version>
    </dependency>
</dependencies>

When I execute the application, it fails to start because of this error:

APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    dev.langchain4j.store.embedding.mongodb.MongoDbEmbeddingStore.isCollectionExist(MongoDbEmbeddingStore.java:337)

The following method did not exist:

    'com.mongodb.client.MongoIterable com.mongodb.client.MongoDatabase.listCollectionNames()'

The calling method's class, dev.langchain4j.store.embedding.mongodb.MongoDbEmbeddingStore, was loaded from the following location:

    jar:file:/home/cholordo/.m2/repository/dev/langchain4j/langchain4j-mongodb-atlas/0.36.2/langchain4j-mongodb-atlas-0.36.2.jar!/dev/langchain4j/store/embedding/mongodb/MongoDbEmbeddingStore.class

The called method's class, com.mongodb.client.MongoDatabase, is available from the following locations:

    jar:file:/home/cholordo/.m2/repository/org/mongodb/mongodb-driver-sync/5.0.1/mongodb-driver-sync-5.0.1.jar!/com/mongodb/client/MongoDatabase.class

The called method's class hierarchy was loaded from the following locations:

    com.mongodb.client.MongoDatabase: file:/home/cholordo/.m2/repository/org/mongodb/mongodb-driver-sync/5.0.1/mongodb-driver-sync-5.0.1.jar


Action:

Correct the classpath of your application so that it contains compatible versions of the classes dev.langchain4j.store.embedding.mongodb.MongoDbEmbeddingStore and com.mongodb.client.MongoDatabase ```

This is triggers while creating the Embedded Mongo store in this part:





    @Bean
    public EmbeddingStore embeddingStore(){
        String uri = "mongodb://root:[email protected]:27017/admin?authSource=admin";
        MongoClient mongoClient = MongoClients.create(uri);
        return new MongoDbEmbeddingStore.Builder().indexName("wildcatProIndex")
                .fromClient(mongoClient)
                .collectionName("ingested_documents")
                .databaseName("admin")
                .build();
    }

More precisely in this part:

        if (!this.isCollectionExist(database, collectionName)) {
            this.createCollection(database, collectionName, (CreateCollectionOptions)Utils.getOrDefault(createCollectionOptions, new CreateCollectionOptions()));
        }

Can any give me guidance on this ?.I have problems structuring the post so I apology... 

Upvotes: 0

Views: 86

Answers (1)

Elve Xu
Elve Xu

Reputation: 1

This is a dependency version compatibility issue. The langchain4j-mongodb-atlas method is not available in the current MongoDB driver version.

Let's fix this issue by examining your pom.xml:

  1. First, explicitly specify the MongoDB driver version in the <properties> section:
<properties>
    <mongodb-driver-sync.version>4.11.1</mongodb-driver-sync.version>
</properties>
  1. Ensure dependency version consistency. Modify the dependencies:
<dependencies>
    <!-- Remove or comment out the existing spring-boot-starter-data-mongodb -->
    <!--
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    -->
    
    <!-- Add these dependencies -->
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>${mongodb-driver-sync.version}</version>
    </dependency>
    
    <!-- Ensure all langchain4j related dependencies use the same version -->
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-spring-boot-starter</artifactId>
        <version>0.36.2</version>  <!-- Update to match other langchain4j dependencies -->
    </dependency>
</dependencies>
  1. If you still need Spring Data MongoDB, configure it like this:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Key changes:

  1. Explicitly specify MongoDB driver version
  2. Unify all langchain4j dependencies to version 0.36.2
  3. Avoid MongoDB driver version conflicts

If issues persist, try:

  1. Clean Maven cache:
mvn clean
rm -rf ~/.m2/repository/org/mongodb
mvn dependency:purge-local-repository
  1. Rebuild the project:
mvn clean install

If you still encounter problems, consider downgrading langchain4j-mongodb-atlas to an earlier version (like 0.35.0) for testing.

The error message shows that the listCollectionNames() method doesn't exist in the current MongoDB driver version. This method is available in newer versions of the driver, which is why we need to update the dependencies.

Your Bean configuration itself looks correct. The issue is solely with dependency version compatibility.

Upvotes: 0

Related Questions