Runner
Runner
源代码在 src/agents/run.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
run async classmethod
run(
starting_agent: Agent[TContext],
input: str | list[TResponseInputItem],
*,
context: TContext | None = None,
max_turns: int = DEFAULT_MAX_TURNS,
hooks: RunHooks[TContext] | None = None,
run_config: RunConfig | None = None,
previous_response_id: str | None = None,
auto_previous_response_id: bool = False,
conversation_id: str | None = None,
session: Session | None = None,
) -> RunResult
从给定的 agent 开始运行一个工作流。
agent 将在一个循环中运行,直到生成最终输出。循环的运行方式如下
- 代理使用给定的输入被调用。
- 如果存在最终输出(即 agent 产生
agent.output_type类型的内容),则循环终止。 - 如果存在移交,我们将再次运行循环,使用新的 agent。
- 否则,我们运行工具调用(如果有的话),并重新运行循环。
在两种情况下,代理可能会引发异常
- 如果超过了最大回合数,将引发 MaxTurnsExceeded 异常。
- 如果触发了护栏触发器,则会引发 GuardrailTripwireTriggered 异常。
注意
仅运行第一个 agent 的输入 guardrails。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
starting_agent
|
Agent[TContext]
|
要运行的起始代理。 |
required |
input
|
str | list[TResponseInputItem]
|
agent 的初始输入。您可以传递单个字符串作为用户消息,也可以传递输入项列表。 |
required |
context
|
TContext | None
|
使用哪个 context 运行 agent。 |
None
|
max_turns
|
int
|
运行 agent 的最大回合数。一个回合定义为一次 AI 调用(包括可能发生的任何工具调用)。 |
DEFAULT_MAX_TURNS
|
hooks
|
RunHooks[TContext] | None
|
一个对象,它在各种生命周期事件发生时接收回调。 |
None
|
run_config
|
RunConfig | None
|
整个 agent 运行的全局设置。 |
None
|
previous_response_id
|
str | None
|
前一个响应的 ID。如果通过 Responses API 使用 OpenAI 模型,这允许您跳过传递前一回合的输入。 |
None
|
conversation_id
|
str | None
|
会话 ID(https://platform.openai.com/docs/guides/conversation-state?api-mode=responses)。如果提供,将使用会话来读取和写入项。每个 agent 都可以访问到目前为止的会话历史记录,并且其输出项将被写入会话。我们建议仅在专门使用 OpenAI 模型时才使用此功能;其他模型提供程序不会写入 Conversation 对象,因此最终只会存储部分会话。 |
None
|
session
|
Session | None
|
用于自动会话历史记录管理的会话。 |
None
|
返回值
| 类型 | 描述 |
|---|---|
RunResult
|
一个运行结果,包含所有输入、guardrail 结果和最后一个 agent 的输出 |
RunResult
|
agent 可能会执行移交,因此我们不知道输出的具体 |
RunResult
|
类型。 |
源代码在 src/agents/run.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
run_sync classmethod
run_sync(
starting_agent: Agent[TContext],
input: str | list[TResponseInputItem],
*,
context: TContext | None = None,
max_turns: int = DEFAULT_MAX_TURNS,
hooks: RunHooks[TContext] | None = None,
run_config: RunConfig | None = None,
previous_response_id: str | None = None,
auto_previous_response_id: bool = False,
conversation_id: str | None = None,
session: Session | None = None,
) -> RunResult
同步运行一个工作流,从给定的 agent 开始。
注意
这只是包装了 run 方法,因此如果已经存在事件循环(例如,在异步函数内部,或者在 Jupyter notebook 或 FastAPI 等异步上下文中),它将无法工作。对于这些情况,请使用 run 方法。
agent 将在一个循环中运行,直到生成最终输出。循环运行
- 代理使用给定的输入被调用。
- 如果存在最终输出(即 agent 产生
agent.output_type类型的内容),则循环终止。 - 如果存在移交,我们将再次运行循环,使用新的 agent。
- 否则,我们运行工具调用(如果有的话),并重新运行循环。
在两种情况下,代理可能会引发异常
- 如果超过了最大回合数,将引发 MaxTurnsExceeded 异常。
- 如果触发了护栏触发器,则会引发 GuardrailTripwireTriggered 异常。
注意
仅运行第一个 agent 的输入 guardrails。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
starting_agent
|
Agent[TContext]
|
要运行的起始代理。 |
required |
input
|
str | list[TResponseInputItem]
|
agent 的初始输入。您可以传递单个字符串作为用户消息,也可以传递输入项列表。 |
required |
context
|
TContext | None
|
使用哪个 context 运行 agent。 |
None
|
max_turns
|
int
|
运行 agent 的最大回合数。一个回合定义为一次 AI 调用(包括可能发生的任何工具调用)。 |
DEFAULT_MAX_TURNS
|
hooks
|
RunHooks[TContext] | None
|
一个对象,它在各种生命周期事件发生时接收回调。 |
None
|
run_config
|
RunConfig | None
|
整个 agent 运行的全局设置。 |
None
|
previous_response_id
|
str | None
|
前一个响应的 ID,如果使用 OpenAI 模型通过 Responses API,这允许您跳过传递前一回合的输入。 |
None
|
conversation_id
|
str | None
|
存储会话的 ID(如果有)。 |
None
|
session
|
Session | None
|
用于自动会话历史记录管理的会话。 |
None
|
返回值
| 类型 | 描述 |
|---|---|
RunResult
|
一个运行结果,包含所有输入、guardrail 结果和最后一个 agent 的输出 |
RunResult
|
agent 可能会执行移交,因此我们不知道输出的具体 |
RunResult
|
类型。 |
源代码在 src/agents/run.py
run_streamed classmethod
run_streamed(
starting_agent: Agent[TContext],
input: str | list[TResponseInputItem],
context: TContext | None = None,
max_turns: int = DEFAULT_MAX_TURNS,
hooks: RunHooks[TContext] | None = None,
run_config: RunConfig | None = None,
previous_response_id: str | None = None,
auto_previous_response_id: bool = False,
conversation_id: str | None = None,
session: Session | None = None,
) -> RunResultStreaming
以流式模式运行一个工作流,从给定的 agent 开始。
返回的结果对象包含一个方法,您可以使用它来流式传输生成时语义事件。
agent 将在一个循环中运行,直到生成最终输出。循环的运行方式如下
- 代理使用给定的输入被调用。
- 如果存在最终输出(即 agent 产生
agent.output_type类型的内容),则循环终止。 - 如果存在移交,我们将再次运行循环,使用新的 agent。
- 否则,我们运行工具调用(如果有的话),并重新运行循环。
在两种情况下,代理可能会引发异常
- 如果超过了最大回合数,将引发 MaxTurnsExceeded 异常。
- 如果触发了护栏触发器,则会引发 GuardrailTripwireTriggered 异常。
注意
仅运行第一个 agent 的输入 guardrails。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
starting_agent
|
Agent[TContext]
|
要运行的起始代理。 |
required |
input
|
str | list[TResponseInputItem]
|
agent 的初始输入。您可以传递单个字符串作为用户消息,也可以传递输入项列表。 |
required |
context
|
TContext | None
|
使用哪个 context 运行 agent。 |
None
|
max_turns
|
int
|
运行 agent 的最大回合数。一个回合定义为一次 AI 调用(包括可能发生的任何工具调用)。 |
DEFAULT_MAX_TURNS
|
hooks
|
RunHooks[TContext] | None
|
一个对象,它在各种生命周期事件发生时接收回调。 |
None
|
run_config
|
RunConfig | None
|
整个 agent 运行的全局设置。 |
None
|
previous_response_id
|
str | None
|
前一个响应的 ID,如果使用 OpenAI 模型通过 Responses API,这允许您跳过传递前一回合的输入。 |
None
|
conversation_id
|
str | None
|
存储会话的 ID(如果有)。 |
None
|
session
|
Session | None
|
用于自动会话历史记录管理的会话。 |
None
|
返回值
| 类型 | 描述 |
|---|---|
RunResultStreaming
|
一个结果对象,包含有关运行的数据,以及一个方法来 |
RunResultStreaming
|
流式传输事件。 |
源代码在 src/agents/run.py
RunConfig dataclass
配置整个 agent 运行的设置。
源代码在 src/agents/run.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
model class-attribute instance-attribute
model: str | Model | None = None
整个 agent 运行使用的模型。如果设置,将覆盖每个 agent 上设置的模型。下面传递的 model_provider 必须能够解析此模型名称。
model_provider class-attribute instance-attribute
model_provider: ModelProvider = field(
default_factory=MultiProvider
)
在查找字符串模型名称时使用的模型提供程序。默认值为 OpenAI。
model_settings class-attribute instance-attribute
model_settings: ModelSettings | None = None
配置全局模型设置。任何非空值都将覆盖 agent 特定的模型设置。
handoff_input_filter class-attribute instance-attribute
handoff_input_filter: HandoffInputFilter | None = None
应用于所有移交的全局输入过滤器。如果设置了 Handoff.input_filter,则该过滤器优先。输入过滤器允许您编辑发送到新 agent 的输入。有关更多详细信息,请参阅 Handoff.input_filter 中的文档。
nest_handoff_history class-attribute instance-attribute
在没有自定义输入过滤器的情况下,在移交之前将先前的运行历史记录包装在单个助手消息中。设置为 False 以保留先前版本中的原始转录行为。
handoff_history_mapper class-attribute instance-attribute
handoff_history_mapper: HandoffHistoryMapper | None = None
可选函数,接收规范化的转录记录(历史记录 + 移交项),并返回应传递给下一个 agent 的输入历史记录。当 nest_handoff_history 为 True 时,此函数仅运行。
input_guardrails class-attribute instance-attribute
input_guardrails: list[InputGuardrail[Any]] | None = None
应用于初始运行输入的输入 guardrails 列表。
output_guardrails class-attribute instance-attribute
output_guardrails: list[OutputGuardrail[Any]] | None = None
应用于运行最终输出的输出 guardrails 列表。
tracing_disabled class-attribute instance-attribute
是否禁用 agent 运行的跟踪。如果禁用,我们将不会跟踪 agent 运行。
trace_include_sensitive_data class-attribute instance-attribute
是否在跟踪中包含潜在的敏感数据(例如:工具调用或 LLM 生成的输入/输出)。如果为 False,我们将仍然为这些事件创建 span,但敏感数据将不会被包含。
workflow_name class-attribute instance-attribute
运行的名称,用于跟踪。应该是运行的逻辑名称,例如“代码生成工作流”或“客户支持 agent”。
trace_id class-attribute instance-attribute
用于跟踪的自定义跟踪 ID。如果未提供,我们将生成一个新的跟踪 ID。
group_id class-attribute instance-attribute
用于跟踪的分组标识符,以链接来自同一对话或进程的多个跟踪。例如,您可以使用聊天线程 ID。
trace_metadata class-attribute instance-attribute
包含在跟踪中的可选附加元数据字典。
session_input_callback class-attribute instance-attribute
session_input_callback: SessionInputCallback | None = None
定义如何处理在提供新输入时会话历史记录。- None(默认):将新输入附加到会话历史记录中。- SessionInputCallback:一个自定义函数,它接收历史记录和新输入,并返回所需的组合项列表。