/ examples / knowledge / scope_identifiers_example.py
scope_identifiers_example.py
  1  #!/usr/bin/env python3
  2  """
  3  Example: Using Scope Identifiers (user_id, agent_id, run_id)
  4  
  5  This example demonstrates:
  6  1. Multi-user knowledge isolation with user_id
  7  2. Multi-agent knowledge sharing with agent_id
  8  3. Run-specific context with run_id
  9  4. Error handling for missing scope identifiers
 10  
 11  Requirements:
 12  - pip install praisonaiagents[knowledge]
 13  - OPENAI_API_KEY environment variable
 14  
 15  Usage:
 16      python scope_identifiers_example.py
 17  """
 18  
 19  import os
 20  import tempfile
 21  import shutil
 22  
 23  from praisonaiagents import Agent
 24  
 25  
 26  def main():
 27      temp_dir = tempfile.mkdtemp(prefix='praison_scope_')
 28      
 29      try:
 30          # Create test document
 31          doc = os.path.join(temp_dir, 'shared_knowledge.txt')
 32          with open(doc, 'w') as f:
 33              f.write("""
 34              Company Policy Document
 35              
 36              1. All employees must complete security training annually.
 37              2. Remote work is allowed with manager approval.
 38              3. Expense reports must be submitted within 30 days.
 39              """)
 40          
 41          print("=" * 60)
 42          print("Example: Scope Identifiers for Knowledge Isolation")
 43          print("=" * 60)
 44          
 45          # Example 1: User-scoped knowledge
 46          print("\n1. User-scoped knowledge (user_id):")
 47          print("-" * 40)
 48          
 49          user1_agent = Agent(
 50              name="HR_Assistant_User1",
 51              instructions="You are an HR assistant.",
 52              knowledge=[temp_dir],
 53              user_id="user_alice",  # Knowledge scoped to Alice
 54          )
 55          
 56          response = user1_agent.chat("What is the policy on remote work?")
 57          print("User Alice asks: What is the policy on remote work?")
 58          print(f"Response: {response[:200]}...")
 59          
 60          # Example 2: Agent-scoped knowledge
 61          print("\n2. Agent-scoped knowledge (agent_id):")
 62          print("-" * 40)
 63          
 64          shared_agent = Agent(
 65              name="Policy_Expert",
 66              instructions="You are a policy expert.",
 67              knowledge=[temp_dir],
 68              agent_id="policy_agent_v1",  # Knowledge scoped to this agent
 69          )
 70          
 71          response = shared_agent.chat("What are the expense report rules?")
 72          print("Agent policy_agent_v1 query: What are the expense report rules?")
 73          print(f"Response: {response[:200]}...")
 74          
 75          # Example 3: Combined scoping
 76          print("\n3. Combined scoping (user_id + agent_id):")
 77          print("-" * 40)
 78          
 79          combined_agent = Agent(
 80              name="Personal_HR_Bot",
 81              instructions="You are a personal HR assistant.",
 82              knowledge=[temp_dir],
 83              user_id="user_bob",
 84              agent_id="hr_bot_v2",
 85          )
 86          
 87          response = combined_agent.chat("What training is required?")
 88          print("User Bob with hr_bot_v2: What training is required?")
 89          print(f"Response: {response[:200]}...")
 90          
 91          print("\n" + "=" * 60)
 92          print("Scope Identifier Summary:")
 93          print("=" * 60)
 94          print("""
 95  - user_id: Isolates knowledge per user (multi-tenant)
 96  - agent_id: Isolates knowledge per agent type/version
 97  - run_id: Isolates knowledge per execution run (ephemeral)
 98  
 99  Best Practices:
100  1. Always provide at least one scope identifier
101  2. Use user_id for user-specific data
102  3. Use agent_id for shared agent knowledge
103  4. Use run_id for temporary/session-specific context
104          """)
105          
106      finally:
107          shutil.rmtree(temp_dir, ignore_errors=True)
108  
109  
110  if __name__ == "__main__":
111      main()