How to create a knowledge base based on user isolation

I am creating knowledge base by below code:

    def create_knowledge_base(self, user_id: str, chat_session_id: str, urls: list):

        knowledge_base = PDFUrlKnowledgeBase(
            urls=urls,
            vector_db=PgVector(
                table_name="kb_pdf_url",
                db_url=self.db_url,
                # text-embedding-3-small
                # embedder=OpenAIEmbedder(id="text-embedding-ada-002"),
            ),
            metadata={"user_id": user_id, "chat_session_id": chat_session_id},
        )

        knowledge_base.load(
            upsert=True,
            filters={"user_id": user_id, "chat_session_id": chat_session_id},
        )

        return knowledge_base

I want to use 1 table to save different users and different sessions pdf. When use agent to answer my question. different user’s agent will load and search different kb. based on userid and sessionid

But I do not know how to make agent to search with filters? I know agent.knowledge.search can pass filters. But this func is triggerred by me. When agent auto search kb, how to do that?

This is my agent code:

    def get_agent(
        self,
        user_id: str,
        session_id: str,
        model_name: AIModelEnum,
        knowledge_base: AgentKnowledge = None,
    ) -> Agent:
        memory = AgentMemory(
            db=PgMemoryDb(
                table_name="agent_memory", db_url=self.db_url
            ),  # Persist memory in Postgres
            create_user_memories=True,  # Store user preferences
            create_session_summary=True,  # Store conversation summaries
        )
        agent_model = get_ai_model_instance(model_name)

        agent = Agent(
            user_id=user_id,
            session_id=session_id,
            model=agent_model,
            markdown=True,
            instructions=self.instructions,
            storage=PostgresAgentStorage(
                table_name="agentic_rag_agent_sessions",
                db_url=self.db_url,
            ),
            # memory=memory,
            read_chat_history=True,
            add_history_to_messages=True,
            debug_mode=True,
            # reasoning=True,
            # reasoning_model=DeepSeek(
            #     id="deepseek-reasoner", api_key=getenv("DEEPSEEK_API_KEY")
            # ),
        )

        if knowledge_base:
            agent.knowledge = knowledge_base
            agent.search_knowledge = True
            agent.add_references = True

        return agent

Currently, all users are using the same kb table, not search by userId & sessionId

Thanks

1 Like

Hi @miles
Thanks for reaching out and for using Agno! I’ve looped in the right engineers to help with your question. We usually respond within 24 hours, but if this is urgent, just let us know, and we’ll do our best to prioritize it.
Appreciate your patience—we’ll get back to you soon! :smile:

Hey @miles! From what I understand, you’re looking to trigger the filter function via the agent using user_id. One approach I’d suggest is passing the user ID directly in the prompt you send to the agent. For example:

agent.run("get purchase order for user id 1234")

Hope this helps! Let me know if this answers your question.