MCPcopy Index your code
hub / github.com/openai/openai-agents-python / main

Function main

examples/agent_patterns/agents_as_tools_conditional.py:83–139  ·  view source on GitHub ↗

Interactive demo with LLM interaction.

()

Source from the content-addressed store, hash-verified

81
82
83async def main():
84 """Interactive demo with LLM interaction."""
85 print("Agents-as-Tools with Conditional Enabling\n")
86 print(
87 "This demonstrates how language response tools are dynamically enabled based on user preferences.\n"
88 )
89
90 print("Choose language preference:")
91 print("1. Spanish only (1 tool)")
92 print("2. French and Spanish (2 tools)")
93 print("3. European languages (3 tools)")
94
95 choice = input_with_fallback("\nSelect option (1-3): ", "2").strip()
96 preference_map = {"1": "spanish_only", "2": "french_spanish", "3": "european"}
97 language_preference = preference_map.get(choice, "spanish_only")
98
99 # Create context and show available tools
100 context = RunContextWrapper(AppContext(language_preference=language_preference))
101 available_tools = await orchestrator.get_all_tools(context)
102 tool_names = [tool.name for tool in available_tools]
103
104 print(f"\nLanguage preference: {language_preference}")
105 print(f"Available tools: {', '.join(tool_names)}")
106 print(f"The LLM will only see and can use these {len(available_tools)} tools\n")
107
108 # Get user request
109 user_request = input_with_fallback(
110 "Ask a question and see responses in available languages:\n",
111 "How do you say good morning?",
112 )
113
114 # Run with LLM interaction
115 print("\nProcessing request...")
116 with trace("Conditional tool access"):
117 result = await Runner.run(
118 starting_agent=orchestrator,
119 input=user_request,
120 context=context.context,
121 )
122 while result.interruptions:
123
124 async def confirm(question: str) -> bool:
125 return confirm_with_fallback(f"{question} (y/n): ", default=True)
126
127 state = result.to_state()
128 for interruption in result.interruptions:
129 prompt = f"\nDo you approve this tool call: {interruption.name} with arguments {interruption.arguments}?"
130 confirmed = await confirm(prompt)
131 if confirmed:
132 state.approve(interruption)
133 print(f"✓ Approved: {interruption.name}")
134 else:
135 state.reject(interruption)
136 print(f"✗ Rejected: {interruption.name}")
137 result = await Runner.run(orchestrator, state)
138
139 print(f"\nResponse:\n{result.final_output}")
140

Callers 1

Calls 11

input_with_fallbackFunction · 0.90
RunContextWrapperClass · 0.90
traceFunction · 0.90
get_all_toolsMethod · 0.80
approveMethod · 0.80
rejectMethod · 0.80
AppContextClass · 0.70
confirmFunction · 0.70
getMethod · 0.45
runMethod · 0.45
to_stateMethod · 0.45

Tested by

no test coverage detected