我正在尝试将 LangChain4j 集成到我的 Spring Boot 应用程序中,顺便说一句,该应用程序正在使用 Spring Data mongo。我意识到(显然)的一件事是我不想使用内存存储 bur,而是使用 RAG 的 mongo 存储。这就是我面临错误的地方,我不知道如何解决它。详情如下:
<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>
当我执行应用程序时,由于以下错误而无法启动:
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...
这是一个依赖版本兼容性问题。 langchain4j-mongodb-atlas 方法在当前 MongoDB 驱动版本中不可用。
让我们通过检查您的 pom.xml 来解决此问题:
<properties>
部分显式指定MongoDB驱动程序版本:<properties>
<mongodb-driver-sync.version>4.11.1</mongodb-driver-sync.version>
</properties>
<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>
<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>
主要变化:
如果问题仍然存在,请尝试:
mvn clean
rm -rf ~/.m2/repository/org/mongodb
mvn dependency:purge-local-repository
mvn clean install
如果仍然遇到问题,请考虑将 langchain4j-mongodb-atlas 降级到早期版本(如 0.35.0)进行测试。
错误信息显示当前MongoDB驱动版本中不存在
listCollectionNames()
方法。此方法在较新版本的驱动程序中可用,这就是我们需要更新依赖项的原因。
您的 Bean 配置本身看起来是正确的。该问题仅与依赖项版本兼容性有关。