| | |
| | | import dev.langchain4j.store.embedding.EmbeddingStore; |
| | | import dev.langchain4j.store.embedding.pinecone.PineconeEmbeddingStore; |
| | | import dev.langchain4j.store.embedding.pinecone.PineconeServerlessIndexConfig; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import io.pinecone.clients.Index; |
| | | import io.pinecone.clients.Pinecone; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | /** |
| | | * @author :yys |
| | | * @date : 2025/5/2 21:07 |
| | | * 向量存储配置 |
| | | */ |
| | | @Configuration |
| | | public class EmbeddingStoreConfig { |
| | | |
| | | @Autowired |
| | | private EmbeddingModel embeddingModel; |
| | | @Value("${pinecone.api-key:pcsk_4SJLnh_tNB3wSLJU8tc4E5P28PcXX8eCLdURqZpVhg1FMV8CRYxjneWdzqRdB5Ftqooi9}") |
| | | private String pineconeApiKey; |
| | | |
| | | @Value("${pinecone.index:xiaozhi-index}") |
| | | private String indexName; |
| | | |
| | | @Value("${pinecone.namespace:knowledge-base}") |
| | | private String namespace; |
| | | |
| | | @Bean |
| | | public EmbeddingStore<TextSegment> embeddingStore() { |
| | | //创建向量存储 |
| | | public Pinecone pinecone() { |
| | | return new Pinecone.Builder(pineconeApiKey).build(); |
| | | } |
| | | |
| | | @Bean |
| | | public Index pineconeIndex(Pinecone pinecone) { |
| | | return pinecone.getIndexConnection(indexName); |
| | | } |
| | | |
| | | @Bean |
| | | public EmbeddingStore<TextSegment> embeddingStore(EmbeddingModel embeddingModel) { |
| | | return PineconeEmbeddingStore.builder() |
| | | .apiKey("pcsk_4SJLnh_tNB3wSLJU8tc4E5P28PcXX8eCLdURqZpVhg1FMV8CRYxjneWdzqRdB5Ftqooi9") |
| | | .index("xiaozhi-index")//如果指定的索引不存在,将创建一个新的索引 |
| | | .nameSpace("xiaozhi-namespace") //如果指定的名称空间不存在,将创建一个新的名称 空间 |
| | | .apiKey(pineconeApiKey) |
| | | .index(indexName) |
| | | .nameSpace(namespace) |
| | | .createIndex(PineconeServerlessIndexConfig.builder() |
| | | .cloud("AWS") //指定索引部署在 AWS 云服务上。 |
| | | .region("us-east-1") //指定索引所在的 AWS 区域为 us-east-1。 |
| | | .dimension(embeddingModel.dimension()) //指定索引的向量维度,该维度与 embeddedModel 生成的向量维度相同。 |
| | | .cloud("AWS") |
| | | .region("us-east-1") |
| | | .dimension(embeddingModel.dimension()) |
| | | .build()) |
| | | .build(); |
| | | } |