I’m using Qdrant as the vector DB in my SQL Agent and have two questions:
-
How do I add metadata for each point when indexing documents in Qdrant (e.g. adding a user_id)?
-
How do I apply a filter on my knowledge base for multi-tenancy (e.g. user_id)? I want to restrict search results so that each tenant only accesses its own documents based on the metadata.
Below is a simplified version of my SQL Agent code where I initialize the knowledge base:
class SQLAgent:
def __init__(self, config: SQLAgentConfig) -> None:
self.config = config
self.sql_agent: Optional[Agent] = None
def initialize_knowledge_base(self) -> None:
try:
vector_db = Qdrant(
collection=self.config.qdrant_collection,
host=self.config.qdrant_host,
port=str(self.config.qdrant_port),
embedder=OpenAIEmbedder(
id=self.config.embedding_model,
api_key=self.config.openai_api_key,
dimensions=1536,
),
)
# some filter {user_id: user_id}
self.knowledge_base = AgentKnowledge(vector_db=vector_db, num_documents=3)
logger.info("Support Agent knowledge base initialized successfully.")
except Exception:
logger.exception("Failed to initialize Support Agent knowledge base.")
raise
def initialize_agent(self) -> Agent:
try:
self.sql_agent = Agent(
name=self.config.agent_name,
model=OpenAIChat(
id=self.config.model,
api_key=self.config.openai_api_key,
),
user_id=self.config.user_id,
session_id=self.config.session_id,
markdown=self.config.markdown,
show_tool_calls=False,
description=self.config.description,
instructions=self.config.instructions,
tools=[
SQLTools(
db_engine=self.config.engine,
tables=self.config.allowed_tables,
dialect="mysql+pymysql",
describe_table=True,
list_tables=True,
)
],
retries=self.config.retries,
debug_mode=self.config.debug_mode,
num_history_responses=self.config.num_history_responses,
add_history_to_messages=True,
storage=MySQLAgentStorage(
table_name=self.config.conversation_table_name,
db_url=self.config.db_url,
),
extra_data=self.config.extra_data,
read_chat_history=self.config.read_chat_history,
read_tool_call_history=self.config.read_tool_call_history,
stream=self.config.stream,
add_datetime_to_instructions=self.config.add_datetime_to_instructions,
telemetry=self.config.telemetry,
)
logger.info("SQL Agent initialized successfully.")
return self.sql_agent
except Exception as exc:
logger.exception("Failed to initialize SQL Agent.")
raise exc
Thanks