run_demo_loop(
agent: Agent[Any],
*,
stream: bool = True,
context: TContext | None = None,
) -> None
使用给定的代理运行一个简单的 REPL 循环。
此工具允许从命令行快速手动测试和调试代理。对话状态在多轮对话中保持不变。输入 exit 或 quit 以停止循环。
参数
| 名称 |
类型 |
描述 |
默认 |
agent
|
Agent[Any]
|
|
required
|
stream
|
bool
|
|
True
|
context
|
TContext | None
|
|
None
|
源代码位于 src/agents/repl.py
| async def run_demo_loop(
agent: Agent[Any], *, stream: bool = True, context: TContext | None = None
) -> None:
"""Run a simple REPL loop with the given agent.
This utility allows quick manual testing and debugging of an agent from the
command line. Conversation state is preserved across turns. Enter ``exit``
or ``quit`` to stop the loop.
Args:
agent: The starting agent to run.
stream: Whether to stream the agent output.
context: Additional context information to pass to the runner.
"""
current_agent = agent
input_items: list[TResponseInputItem] = []
while True:
try:
user_input = input(" > ")
except (EOFError, KeyboardInterrupt):
print()
break
if user_input.strip().lower() in {"exit", "quit"}:
break
if not user_input:
continue
input_items.append({"role": "user", "content": user_input})
result: RunResultBase
if stream:
result = Runner.run_streamed(current_agent, input=input_items, context=context)
async for event in result.stream_events():
if isinstance(event, RawResponsesStreamEvent):
if isinstance(event.data, ResponseTextDeltaEvent):
print(event.data.delta, end="", flush=True)
elif isinstance(event, RunItemStreamEvent):
if event.item.type == "tool_call_item":
print("\n[tool called]", flush=True)
elif event.item.type == "tool_call_output_item":
print(f"\n[tool output: {event.item.output}]", flush=True)
elif isinstance(event, AgentUpdatedStreamEvent):
print(f"\n[Agent updated: {event.new_agent.name}]", flush=True)
print()
else:
result = await Runner.run(current_agent, input_items, context=context)
if result.final_output is not None:
print(result.final_output)
current_agent = result.last_agent
input_items = result.to_input_list()
|