第五期 使用Pgvector 出错

如果出现类似的错误: No qualifying bean of type 'org.springframework.ai.vectorstore.VectorStore' available: expected single matching bean but found 3: LoveAppVectorStore,pgVectorVectorStore,vectorStore

关键:
要进行指定VectorStore @Bean("pgVectorVectorStore") @Primary

@Resource(name = "pgVectorVectorStore") private VectorStore pgVectorVectorStore;

main主程序

java
复制代码
@SpringBootApplication(exclude = PgVectorStoreAutoConfiguration.class) public class WAiAgentBackendApplication { public static void main(String[] args) { SpringApplication.run(WAiAgentBackendApplication.class, args); } }

```java

@Configuration public class PgVectorVectorStoreConfig {

text
复制代码
@Resource private LoveAppDocumentLoader loveAppDocumentLoader; @Bean("pgVectorVectorStore") @Primary public VectorStore pgVectorVectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel dashscopeEmbeddingModel) { VectorStore vectorStore = PgVectorStore.builder(jdbcTemplate, dashscopeEmbeddingModel) .dimensions(1536) // Optional: defaults to model dimensions or 1536 .distanceType(COSINE_DISTANCE) // Optional: defaults to COSINE_DISTANCE .indexType(HNSW) // Optional: defaults to HNSW .initializeSchema(true) // Optional: defaults to false .schemaName("public") // Optional: defaults to "public" .vectorTableName("vector_store") // Optional: defaults to "vector_store" .maxDocumentBatchSize(10000) // Optional: defaults to 10000 .build(); // 加载文档 List<Document> documents = loveAppDocumentLoader.loadMarkdowns(); vectorStore.add(documents); return vectorStore; }

}

text
复制代码
测试程序 ```java @SpringBootTest class PgVectorVectorStoreConfigTest { @Resource(name = "pgVectorVectorStore") private VectorStore pgVectorVectorStore; @Test void pgVectorVectorStore() { List<Document> documents = List.of( new Document("鱼皮的编程导航有什么用?学编程啊,做项目啊", Map.of("meta1", "meta1")), new Document("程序员鱼皮的原创项目教程 codefather.cn"), new Document("鱼皮这小伙子比较帅气", Map.of("meta2", "meta2"))); // 添加文档 pgVectorVectorStore.add(documents); // 相似度查询 List<Document> results = pgVectorVectorStore.similaritySearch(SearchRequest.builder().query("怎么学编程啊").topK(3).build()); Assertions.assertNotNull(results); } }
0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP