文本转Embedding上传Pinecone报错:'Pinecone'对象无from_texts属性
问题排查:Pinecone 'from_texts' 属性错误
问题场景
学习生成式AI时,运行文本转向量嵌入并上传至Pinecone的脚本,讲师环境可正常执行,但本地Jupyter Notebook因版本更新触发属性错误。
报错信息
AttributeError Traceback (most recent call last) Cell In[44], line 8 5 index_name="medical-bot" 7 #Creating Embeddings for Each of The Text Chunks & storing ----> 8 docsearch=pc.from_texts([t.page_content for t in text_chunks], embeddings, index_name=index_name) AttributeError: 'Pinecone' object has no attribute 'from_texts'
完整代码
def load_data(data): loader = DirectoryLoader(data, glob="\*.pdf", loader_cls=PyPDFLoader) documents = loader.load() return documents extracted_data = load_data("E:\\data") def text_split(extracted_data): text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap = 20) text_chunks = text_splitter.split_documents(extracted_data) return text_chunks text_chunks = text_split(extracted_data) def download_hugging_face_embeddings(): embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") return embeddings embeddings = download_hugging_face_embeddings() query_result = embeddings.embed_query("Hello World") print("Length", len(query_result)) from dotenv import load_dotenv import os load_dotenv() pinecone_api_key = os.getenv("PINECONE_API_KEY") pinecone_environment = os.getenv("PINECONE_API_ENV") pc = Pinecone(api_key=pinecone_api_key, environment=pinecone_environment) index_name="medical-bot" #My Index Name created in Pinecone DB #Creating Embeddings for Each of The Text Chunks & storing docsearch=Pinecone.from_texts([t.page_content for t in text_chunks], embeddings, index_name=index_name)
错误原因及修复方案
核心原因
代码混用了Pinecone原生SDK和LangChain集成的Pinecone类:
- 讲师使用的是LangChain封装的Pinecone类(
from langchain.vectorstores import Pinecone),该类提供from_texts批量导入方法; - 你当前导入的是Pinecone官方原生SDK(
from pinecone import Pinecone),原生SDK的Pinecone对象没有from_texts方法。
修复步骤
方案1:适配原生SDK + LangChain调用
保留已初始化的原生SDK对象,获取index后传入LangChain的Pinecone类:
- 导入LangChain的Pinecone类:
from langchain.vectorstores import Pinecone - 修改最后一段代码:
# 获取Pinecone索引对象 index = pc.Index(index_name) # 使用LangChain的from_texts方法 docsearch = Pinecone.from_texts( [t.page_content for t in text_chunks], embeddings, index=index )
方案2:使用新版LangChain-Pinecone集成
新版LangChain已将第三方向量库拆分到独立包,需先安装依赖:
pip install langchain-pinecone
然后替换导入和调用逻辑:
from langchain_pinecone import Pinecone # 直接用LangChain-Pinecone的from_texts方法 docsearch = Pinecone.from_texts( [t.page_content for t in text_chunks], embeddings, index_name=index_name, pinecone_api_key=pinecone_api_key, pinecone_environment=pinecone_environment )
版本注意事项
- 旧版LangChain(<=0.1.x)中,Pinecone集成在
langchain.vectorstores模块; - 新版LangChain(>=0.2.x)需安装
langchain-pinecone独立包,导入路径改为langchain_pinecone.Pinecone。
内容的提问来源于stack exchange,提问作者Girish Sawant
相关产品推荐
相关产品推荐

