|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from pydantic import BaseModel |
| 4 | + |
| 5 | +from agents import Agent, AgentBase, RunContextWrapper, Runner, trace |
| 6 | + |
| 7 | +""" |
| 8 | +This example demonstrates the agents-as-tools pattern with conditional tool enabling. |
| 9 | +Agent tools are dynamically enabled/disabled based on user access levels using the |
| 10 | +is_enabled parameter. |
| 11 | +""" |
| 12 | + |
| 13 | + |
| 14 | +class AppContext(BaseModel): |
| 15 | + language_preference: str = "spanish_only" # "spanish_only", "french_spanish", "european" |
| 16 | + |
| 17 | + |
| 18 | +def french_spanish_enabled(ctx: RunContextWrapper[AppContext], agent: AgentBase) -> bool: |
| 19 | + """Enable for French+Spanish and European preferences.""" |
| 20 | + return ctx.context.language_preference in ["french_spanish", "european"] |
| 21 | + |
| 22 | + |
| 23 | +def european_enabled(ctx: RunContextWrapper[AppContext], agent: AgentBase) -> bool: |
| 24 | + """Only enable for European preference.""" |
| 25 | + return ctx.context.language_preference == "european" |
| 26 | + |
| 27 | + |
| 28 | +# Create specialized agents |
| 29 | +spanish_agent = Agent( |
| 30 | + name="spanish_agent", |
| 31 | + instructions="You respond in Spanish. Always reply to the user's question in Spanish.", |
| 32 | +) |
| 33 | + |
| 34 | +french_agent = Agent( |
| 35 | + name="french_agent", |
| 36 | + instructions="You respond in French. Always reply to the user's question in French.", |
| 37 | +) |
| 38 | + |
| 39 | +italian_agent = Agent( |
| 40 | + name="italian_agent", |
| 41 | + instructions="You respond in Italian. Always reply to the user's question in Italian.", |
| 42 | +) |
| 43 | + |
| 44 | +# Create orchestrator with conditional tools |
| 45 | +orchestrator = Agent( |
| 46 | + name="orchestrator", |
| 47 | + instructions=( |
| 48 | + "You are a multilingual assistant. You use the tools given to you to respond to users. " |
| 49 | + "You must call ALL available tools to provide responses in different languages. " |
| 50 | + "You never respond in languages yourself, you always use the provided tools." |
| 51 | + ), |
| 52 | + tools=[ |
| 53 | + spanish_agent.as_tool( |
| 54 | + tool_name="respond_spanish", |
| 55 | + tool_description="Respond to the user's question in Spanish", |
| 56 | + is_enabled=True, # Always enabled |
| 57 | + ), |
| 58 | + french_agent.as_tool( |
| 59 | + tool_name="respond_french", |
| 60 | + tool_description="Respond to the user's question in French", |
| 61 | + is_enabled=french_spanish_enabled, |
| 62 | + ), |
| 63 | + italian_agent.as_tool( |
| 64 | + tool_name="respond_italian", |
| 65 | + tool_description="Respond to the user's question in Italian", |
| 66 | + is_enabled=european_enabled, |
| 67 | + ), |
| 68 | + ], |
| 69 | +) |
| 70 | + |
| 71 | + |
| 72 | +async def main(): |
| 73 | + """Interactive demo with LLM interaction.""" |
| 74 | + print("Agents-as-Tools with Conditional Enabling\n") |
| 75 | + print( |
| 76 | + "This demonstrates how language response tools are dynamically enabled based on user preferences.\n" |
| 77 | + ) |
| 78 | + |
| 79 | + print("Choose language preference:") |
| 80 | + print("1. Spanish only (1 tool)") |
| 81 | + print("2. French and Spanish (2 tools)") |
| 82 | + print("3. European languages (3 tools)") |
| 83 | + |
| 84 | + choice = input("\nSelect option (1-3): ").strip() |
| 85 | + preference_map = {"1": "spanish_only", "2": "french_spanish", "3": "european"} |
| 86 | + language_preference = preference_map.get(choice, "spanish_only") |
| 87 | + |
| 88 | + # Create context and show available tools |
| 89 | + context = RunContextWrapper(AppContext(language_preference=language_preference)) |
| 90 | + available_tools = await orchestrator.get_all_tools(context) |
| 91 | + tool_names = [tool.name for tool in available_tools] |
| 92 | + |
| 93 | + print(f"\nLanguage preference: {language_preference}") |
| 94 | + print(f"Available tools: {', '.join(tool_names)}") |
| 95 | + print(f"The LLM will only see and can use these {len(available_tools)} tools\n") |
| 96 | + |
| 97 | + # Get user request |
| 98 | + user_request = input("Ask a question and see responses in available languages:\n") |
| 99 | + |
| 100 | + # Run with LLM interaction |
| 101 | + print("\nProcessing request...") |
| 102 | + with trace("Conditional tool access"): |
| 103 | + result = await Runner.run( |
| 104 | + starting_agent=orchestrator, |
| 105 | + input=user_request, |
| 106 | + context=context.context, |
| 107 | + ) |
| 108 | + |
| 109 | + print(f"\nResponse:\n{result.final_output}") |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + asyncio.run(main()) |
0 commit comments