使用Pinecone Serverless加载已有索引时出现AttributeError求助
问题排查:Pinecone.from_existing_index 报错 AttributeError
问题描述
运行基于Flask、Llama 2开发的医疗聊天机器人代码时,执行Pinecone.from_existing_index(index_name, embeddings)语句触发以下错误:
AttributeError: type object 'Pinecone' has no attribute 'from_existing_index'
原因分析
代码混淆了原生Pinecone SDK的Pinecone类和LangChain封装的PineconeVectorStore类:
from_existing_index是LangChain适配自身检索逻辑封装的方法,属于PineconeVectorStore,而非原生Pinecone SDK的Pinecone类。- 直接调用原生
Pinecone类的该方法,必然触发属性不存在的错误。
解决方案
1. 修正索引加载逻辑
替换错误的索引加载代码,改用PineconeVectorStore的from_existing_index方法:
- 先通过已初始化的Pinecone客户端实例获取目标索引
- 再将索引对象传入LangChain的封装方法中
修改后的代码片段:
# 保留已有的Pinecone客户端初始化 pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY")) index_name = "chatbot" # 获取Pinecone索引实例 index = pc.Index(index_name) # 用LangChain的PineconeVectorStore加载索引 docsearch = PineconeVectorStore.from_existing_index( index_name=index_name, embedding=embeddings, index=index )
2. 确保依赖版本兼容
执行以下命令升级相关依赖,避免版本不匹配引发的问题:
pip install --upgrade pinecone-client langchain-pinecone
完整修正后的关键代码段
embeddings = download_hugging_face_embeddings() # Initializing the Pinecone pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY")) index_name = "chatbot" # Loading the index index = pc.Index(index_name) docsearch = PineconeVectorStore.from_existing_index( index_name=index_name, embedding=embeddings, index=index )
内容的提问来源于stack exchange,提问作者Garima Srivastava
相关产品推荐
相关产品推荐

