本文详细介绍了如何在多租户rag应用中,利用pinecone向量数据库的元数据过滤功能,高效且安全地隔离不同用户的向量数据。通过在向量嵌入时附加用户id作为元数据,并在检索时应用精确过滤,可以避免创建昂贵的独立索引,实现资源共享和数据隔离的平衡,从而优化系统性能和成本。
在构建多用户或多租户的检索增强生成(RAG)系统时,一个常见且关键的需求是如何在共享的向量数据库中,高效且安全地隔离不同用户的数据。例如,在一个PDF阅读器应用中,每个用户上传的文档都应仅供其本人查询。直接为每个用户创建独立的Pinecone索引虽然能实现数据隔离,但随着用户数量的增长,这将带来高昂的成本和管理复杂性。本文将深入探讨如何利用Pinecone的元数据过滤功能,以一种更经济、更优雅的方式解决这一挑战。
Pinecone允许在存储向量时,为每个向量附加一组键值对形式的元数据。这些元数据可以在查询时作为过滤条件,精确地筛选出符合特定条件的向量。这是实现多租户数据隔离的理想方案,因为它允许所有用户的数据存储在同一个索引中,但通过元数据确保查询结果仅限于当前用户的数据。
在将文档内容转换为向量并上传到Pinecone时,需要将用户的唯一标识符(例如user_id)作为元数据一并存储。
示例:上传向量时附加元数据
from pinecone import Pinecone, Index from langchain_openai import OpenAIEmbeddings from langchain_community.vectorstores import Pinecone as LangchainPinecone import os # 初始化Pinecone客户端和嵌入模型 api_key = os.getenv("PINECONE_API_KEY") environment = os.getenv("PINECONE_ENVIRONMENT") index_name = os.getenv("PINECONE_INDEX") pinecone_client = Pinecone(api_key=api_key, environment=environment) embeddings = OpenAIEmbeddings(openai_api_key=os.getenv("OPENAI_API_KEY")) # 假设这是您要嵌入的文档和对应的用户ID documents_for_user1 = [ ("This is a document for user 1.", {"source": "user_document", "user_id": 1}), ("Another piece of text from user 1.", {"source": "user_document", "user_id": 1}) ] documents_for_user2 = [ ("User 2's specific information.", {"source": "user_document", "user_id": 2}), ("A different document for user 2.", {"source": "user_document", "user_id": 2}) ] # 获取或创建Pinecone索引 if index_name not in pinecone_client.list_indexes(): pinecone_client.create_index( name=index_name, dimension=embeddings.client.dimensions, # 确保维度匹配您的嵌入模型 metric='cosine' ) pinecone_index = pinecone_client.Index(index_name) # 批量嵌入并上传向量,包含user_id元数据 def upsert_vectors_with_metadata(index: Index, texts_and_metadatas: list, embeddings_model, batch_size=32): for i in range(0, len(texts_and_metadatas), batch_size): batch = texts_and_metadatas[i:i+batch_size] texts = [item[0] for item in batch] metadatas = [item[1] for item in batch] # 生成嵌入 embeds = embeddings_model.embed_documents(texts) # 准备upsert数据 # Pinecone的upsert方法需要 (id, vector, metadata) 格式 # 这里我们简化处理,假设id是递增的 vectors_to_upsert = [] for j, (text, metadata) in enumerate(batch): # 实际应用中,id应该是一个唯一且持久的标识符 vector_id = f"doc_{metadata['user_id']}_{i+j}" vectors_to_upsert.append((vector_id, embeds[j], metadata)) index.upsert(vectors=vectors_to_upsert) print(f"Upserted {len(texts_and_metadatas)} vectors to index '{index_name}'.") # 示例调用 # upsert_vectors_with_metadata(pinecone_index, documents_for_user1, embeddings) # upsert_vectors_with_metadata(pinecone_index, documents_for_user2, embeddings)
注意: 上述代码片段展示了如何手动进行upsert。在实际使用Langchain的Pinecone向量存储时,当您使用from_documents或add_documents方法时,可以将元数据作为参数传递,Langchain会自动处理与Pinecone的交互。
当用户发起查询时,您需要从当前用户的会话或请求中获取其user_id,并将其作为过滤条件传递给Pinecone检索器。
示例:在Langchain的ConversationalRetrievalChain中应用用户ID过滤
from flask import Flask, request, jsonify, session import os from langchain_openai import ChatOpenAI from langchain.memory import ConversationBufferWindowMemory from langchain.chains import ConversationalRetrievalChain from langchain_core.prompts import PromptTemplate from langchain_community.vectorstores import Pinecone as LangchainPinecone from pinecone import Pinecone, Index app = Flask(__name__) app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey") # 用于会话管理 # 初始化Pinecone客户端和嵌入模型 pinecone_api_key = os.getenv("PINECONE_API_KEY") pinecone_environment = os.getenv("PINECONE_ENVIRONMENT") openai_api_key = os.getenv("OPENAI_API_KEY") index_name = os.getenv("PINECONE_INDEX") text_field = "text" # 假设您的文本内容存储在元数据的'text'字段中 pinecone_client = Pinecone(api_api_key=pinecone_api_key, environment=pinecone_environment) embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) # 获取Pinecone索引实例 # 确保索引已经存在并包含数据 pinecone_index_instance = pinecone_client.Index(index_name) # 使用Langchain的Pinecone集成创建vectorstore vectorstore = LangchainPinecone( index=pinecone_index_instance, embedding=embeddings, text_key=text_field # 指定存储原始文本的元数据字段 ) # 假设这些函数用于获取用户特定的配置 def get_bot_temperature(user_id): # 根据user_id返回不同的温度,或默认值 return 0.7 def get_custom_prompt(user_id): # 根据user_id返回不同的自定义提示,或默认值 return "You are a helpful AI assistant. Answer the question based on the provided context." @app.route('/<int:user_id>/chat', methods=['POST']) def chat(user_id): user_message = request.form.get('message') if not user_message: return jsonify({"error": "Message is required"}), 400 # 从会话中加载对话历史 # 注意:为了每个用户隔离,会话键应包含user_id conversation_history_key = f'conversation_history_{user_id}' conversation_history = session.get(conversation_history_key, []) bot_temperature = get_bot_temperature(user_id) custom_prompt = get_custom_prompt(user_id) llm = ChatOpenAI( openai_api_key=openai_api_key, model_name='gpt-3.5-turbo', temperature=bot_temperature ) prompt_template = f""" {custom_prompt} CONTEXT: {{context}} QUESTION: {{question}}""" TEST_PROMPT = PromptTemplate(input_variables=["context", "question"], template=prompt_template) memory = ConversationBufferWindowMemory(memory_key="chat_history", return_messages=True, k=8) # 关键部分:在as_retriever中应用filter # Pinecone的过滤语法是字典形式,这里使用'$eq'操作符表示“等于” retriever = vectorstore.as_retriever( search_kwargs={ 'filter': {'user_id': user_id} # 精确匹配当前用户的user_id } ) conversation_chain = ConversationalRetrievalChain.from_llm( llm=llm, retriever=retriever, # 使用带有过滤器的retriever memory=memory, combine_docs_chain_kwargs={"prompt": TEST_PROMPT}, ) response = conversation_chain.run({'question': user_message}) # 保存用户消息和机器人响应到会话 conversation_history.append({'input': user_message, 'output': response}) session[conversation_history_key] = conversation_history return jsonify(response=response) # if __name__ == '__main__': # # 仅用于开发测试,生产环境应使用WSGI服务器 # app.run(debug=True)
代码解析:
通过在Pinecone中利用元数据过滤,我们能够构建一个高效、可扩展且成本效益高的多租户RAG系统。这种方法避免了为每个用户创建独立索引的复杂性和高成本,同时确保了数据隔离和查询的准确性。在设计多用户应用时,将用户ID等关键标识符作为元数据存储并应用于检索过滤,是实现数据隔离和资源共享的强大策略。
以上就是利用元数据在Pinecone中实现用户ID过滤的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号