类class
agentscope.modelOpenAIChatModel
class OpenAIChatModel(ChatModelBase)
源码Source
agentscope/model/_openai_chat/_model.py:38–647OpenAIChatModel 通过 OpenAI Chat Completions 协议连接 OpenAI 及兼容端点,支持流式输出、工具调用、结构化输出与自定义格式化器。位于 agentscope.model 包,继承自 ChatModelBase。
The OpenAI Chat Completions model. Declared in agentscope.model. Extends ChatModelBase.
核心精讲Deep dive
OpenAIChatModel:OpenAI 兼容模型OpenAIChatModel: OpenAI-compatible models
OpenAIChatModel 通过 OpenAI Chat Completions 协议连接 OpenAI 及兼容端点,支持流式输出、工具调用、结构化输出与自定义 formatter。
OpenAIChatModel speaks the OpenAI Chat Completions protocol to OpenAI and compatible endpoints, with streaming, tool calls, structured output and a customizable formatter.
- api_key 放在 OpenAICredential 中,base_url 用于兼容端点。The API key lives in OpenAICredential; base_url points at compatible endpoints.
- stream=True 时返回异步生成器,适合逐字渲染。stream=True returns an async generator suitable for incremental rendering.
- context_size 用于上下文预算,默认 128k。context_size sets the context budget, defaulting to 128k.
连接 OpenAI 兼容端点Connect to an OpenAI-compatible endpoint
import os
from agentscope.credential import OpenAICredential
from agentscope.model import OpenAIChatModel
model = OpenAIChatModel(
credential=OpenAICredential(
api_key=os.environ["OPENAI_API_KEY"],
base_url=os.environ.get("OPENAI_BASE_URL"),
),
model="gpt-4.1-mini",
stream=True,
context_size=128_000,
)嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Parameters | 类class | 参数用于 OpenAI 聊天模型。 The parameters for the OpenAI Chat model. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(credential: OpenAICredential, model: str, parameters: 'OpenAIChatModel.Parameters | None' = None, stream:… | 初始化 `OpenAIChatModel` 实例。 Initialize the OpenAI chat model. |
属性摘要Attribute Summary
| 属性Attribute | 类型Type | 说明Description | 值Value |
|---|---|---|---|
type | Literal['openai_chat'] | 类型字段。 | 'openai_chat' |
构造器详情Constructor Detail
OpenAIChatModel
def __init__(credential: OpenAICredential, model: str, parameters: 'OpenAIChatModel.Parameters | None' = None, stream: bool = True, max_retries: int = 3, retry_delay: float = 1.0, context_size: int = 128000, formatter: FormatterBase | None = None, client_kwargs: dict[str, Any] | None = None, extra_body: dict[str, Any] | None = None) -> None
初始化 OpenAIChatModel 实例。
Initialize the OpenAI chat model.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
credential | OpenAICredential | 凭证对象(类型 `OpenAICredential`)。The OpenAI credential used to authenticate API calls. |
model | str | 模型实例(类型 `str`)。The OpenAI model name, e.g. `gpt-4.1`. |
parameters默认default None | 'OpenAIChatModel.Parameters | None' | 参数(类型 `'OpenAIChatModel.Parameters | None'`)。The OpenAI Chat API parameters. When `None`, the default parameters will be used. |
stream默认default True | bool | 判断是否到启用流式输出。Whether to enable streaming output. |
max_retries默认default 3 | int | 最大重试次数(类型 `int`)。The maximum number of retries for the OpenAI API. |
retry_delay默认default 1.0 | float | 重试延迟(类型 `float`)。Seconds to sleep between retry attempts. |
context_size默认default 128000 | int | 上下文大小(类型 `int`)。The model context size used for context compression. |
formatter默认default None | FormatterBase | None | 格式化器(类型 `FormatterBase | None`)。The formatter that converts `Msg` objects to the format required by the OpenAI API. When `None`, an `OpenAIChatFormatter` instance will be used. |
client_kwargs默认default None | dict[str, Any] | None | 客户端关键字参数(类型 `dict[str, Any] | None`)。Extra keyword arguments forwarded to `openai.AsyncClient` (e.g. `timeout`, `default_headers`, `http_client`). |
extra_body默认default None | dict[str, Any] | None | extra 正文(类型 `dict[str, Any] | None`)。Additional request body fields forwarded to OpenAI-compatible APIs. |
源码Source
agentscope/model/_openai_chat/_model.py:114–177属性详情Attribute Detail
type
Literal['openai_chat'] = 'openai_chat'类型字段。声明为 Literal['openai_chat']。
The type field.
源码Source
agentscope/model/_openai_chat/_model.py:111嵌套类详情Nested Class Detail
Parameters
class Parameters参数用于 OpenAI 聊天模型。位于 agentscope.model 包。
The parameters for the OpenAI Chat model. Declared in agentscope.model.