跳过内容

Agents 模块

set_default_openai_key

set_default_openai_key(
    key: str, use_for_tracing: bool = True
) -> None

为 LLM 请求(以及可选的 tracing())设置默认的 OpenAI API 密钥。只有当未设置 OPENAI_API_KEY 环境变量时,才需要这样做。

如果提供,此密钥将代替 OPENAI_API_KEY 环境变量使用。

参数

名称 类型 描述 默认
密钥 str

要使用的 OpenAI 密钥。

required
use_for_tracing bool

是否也使用此密钥将跟踪数据发送到 OpenAI。默认为 True。如果为 False,则需要设置 OPENAI_API_KEY 环境变量,或使用 set_tracing_export_api_key() 函数,并使用您希望用于跟踪的 API 密钥。

True
源代码位于 src/agents/__init__.py
def set_default_openai_key(key: str, use_for_tracing: bool = True) -> None:
    """Set the default OpenAI API key to use for LLM requests (and optionally tracing()). This is
    only necessary if the OPENAI_API_KEY environment variable is not already set.

    If provided, this key will be used instead of the OPENAI_API_KEY environment variable.

    Args:
        key: The OpenAI key to use.
        use_for_tracing: Whether to also use this key to send traces to OpenAI. Defaults to True
            If False, you'll either need to set the OPENAI_API_KEY environment variable or call
            set_tracing_export_api_key() with the API key you want to use for tracing.
    """
    _config.set_default_openai_key(key, use_for_tracing)

set_default_openai_client

set_default_openai_client(
    client: AsyncOpenAI, use_for_tracing: bool = True
) -> None

为 LLM 请求和/或跟踪设置默认的 OpenAI 客户端。如果提供,将使用此客户端代替默认的 OpenAI 客户端。

参数

名称 类型 描述 默认
client AsyncOpenAI

要使用的 OpenAI 客户端。

required
use_for_tracing bool

是否使用此客户端的 API 密钥上传跟踪数据。如果为 False,则需要设置 OPENAI_API_KEY 环境变量,或使用 set_tracing_export_api_key() 函数,并使用您希望用于跟踪的 API 密钥。

True
源代码位于 src/agents/__init__.py
def set_default_openai_client(client: AsyncOpenAI, use_for_tracing: bool = True) -> None:
    """Set the default OpenAI client to use for LLM requests and/or tracing. If provided, this
    client will be used instead of the default OpenAI client.

    Args:
        client: The OpenAI client to use.
        use_for_tracing: Whether to use the API key from this client for uploading traces. If False,
            you'll either need to set the OPENAI_API_KEY environment variable or call
            set_tracing_export_api_key() with the API key you want to use for tracing.
    """
    _config.set_default_openai_client(client, use_for_tracing)

set_default_openai_api

set_default_openai_api(
    api: Literal["chat_completions", "responses"],
) -> None

为 OpenAI LLM 请求设置默认的 API。默认情况下,我们将使用 responses API,但您可以将其设置为使用 chat completions API。

源代码位于 src/agents/__init__.py
def set_default_openai_api(api: Literal["chat_completions", "responses"]) -> None:
    """Set the default API to use for OpenAI LLM requests. By default, we will use the responses API
    but you can set this to use the chat completions API instead.
    """
    _config.set_default_openai_api(api)

set_tracing_export_api_key

set_tracing_export_api_key(api_key: str) -> None

设置后端导出器的 OpenAI API 密钥。

源代码位于 src/agents/tracing/__init__.py
def set_tracing_export_api_key(api_key: str) -> None:
    """
    Set the OpenAI API key for the backend exporter.
    """
    default_exporter().set_api_key(api_key)

set_tracing_disabled

set_tracing_disabled(disabled: bool) -> None

设置是否全局禁用跟踪。

源代码位于 src/agents/tracing/__init__.py
def set_tracing_disabled(disabled: bool) -> None:
    """
    Set whether tracing is globally disabled.
    """
    get_trace_provider().set_disabled(disabled)

set_trace_processors

set_trace_processors(
    processors: list[TracingProcessor],
) -> None

设置跟踪处理器列表。这将替换当前的处理器列表。

源代码位于 src/agents/tracing/__init__.py
def set_trace_processors(processors: list[TracingProcessor]) -> None:
    """
    Set the list of trace processors. This will replace the current list of processors.
    """
    get_trace_provider().set_processors(processors)

enable_verbose_stdout_logging

enable_verbose_stdout_logging()

启用详细的 stdout 日志记录。这对于调试很有用。

源代码位于 src/agents/__init__.py
def enable_verbose_stdout_logging():
    """Enables verbose logging to stdout. This is useful for debugging."""
    logger = logging.getLogger("openai.agents")
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler(sys.stdout))