OpenAIClient
public class OpenAIClient
io/agentscope/extensions/model/openai/OpenAIClient.java:66–732OpenAI 客户端,封装与远端服务的连接和调用。位于 io.agentscope.extensions.model.openai 包。核心成员包括 call、stream、callApi、getTransport、OpenAIClient、DEFAULT_BASE_URL。
Stateless HTTP client for OpenAI-compatible APIs.
This client handles communication with OpenAI's Chat Completion API using direct HTTP calls. All configuration (API key, base URL) is passed per-request, making this client stateless and safe to share across multiple model instances.
Features:
-
Synchronous and streaming request support
-
SSE stream parsing
-
JSON serialization/deserialization
-
Support for OpenAI-compatible APIs (custom base URL)
-
Generic API call support for other OpenAI endpoints (images, audio, etc.)
API endpoints:
-
Chat completions: /v1/chat/completions
-
Images: /v1/images/generations, /v1/images/edits, /v1/images/variations
-
Audio: /v1/audio/speech, /v1/audio/transcriptions, /v1/audio/translations
字段摘要Field Summary
| 字段Field | 类型Type | 说明Description |
|---|---|---|
DEFAULT_BASE_URLstaticfinal | String | 用于OpenAI API的默认基类 URL。 Default base URL for OpenAI API. |
DEFAULT_BASE_URL_WITH_VERSIONstaticfinal | String | 默认基类 URL 使用版本字段。 Default base URL with version. |
CHAT_COMPLETIONS_ENDPOINTstaticfinal | String | 聊天补全端点字段。 Chat completions API endpoint. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public OpenAIClient(HttpTransport transport) | 初始化 `OpenAIClient` 实例。 Create a new stateless OpenAIClient. |
public OpenAIClient() | 创建新的OpenAIClient 使用默认传输层来自工厂。 Create a new OpenAIClient with the default transport from factory. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request) | 向智能体发送输入并返回最终回复。 Make a synchronous API call. |
public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options) | 向智能体发送输入并返回最终回复。 Make a synchronous API call with options. |
public Flux<OpenAIResponse> stream(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options) | 执行流相关操作。 Make a streaming API call with options for headers and query params. |
public String callApi(String apiKey, String baseUrl, String endpoint, Object requestBody) | 调用API。 Make a generic API call to any OpenAI endpoint with JSON request body. |
public String callApi(String apiKey, String baseUrl, String endpoint, Object requestBody, GenerateOptions options) | 调用API。 Make a generic API call to any OpenAI endpoint with JSON request body and custom options. |
public HttpTransport getTransport() | 获取传输层。 Get the HTTP transport used by this client. |
字段详情Field Detail
DEFAULT_BASE_URL
public static final String DEFAULT_BASE_URL
用于OpenAI API的默认基类 URL。
Default base URL for OpenAI API.
io/agentscope/extensions/model/openai/OpenAIClient.java:74DEFAULT_BASE_URL_WITH_VERSION
public static final String DEFAULT_BASE_URL_WITH_VERSION
默认基类 URL 使用版本字段。声明为 public static final String DEFAULT_BASE_URL_WITH_VERSION。
Default base URL with version.
io/agentscope/extensions/model/openai/OpenAIClient.java:77CHAT_COMPLETIONS_ENDPOINT
public static final String CHAT_COMPLETIONS_ENDPOINT
聊天补全端点字段。声明为 public static final String CHAT_COMPLETIONS_ENDPOINT。
Chat completions API endpoint.
io/agentscope/extensions/model/openai/OpenAIClient.java:80构造器详情Constructor Detail
OpenAIClient
public OpenAIClient(HttpTransport transport)
初始化 OpenAIClient 实例。
Create a new stateless OpenAIClient.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
transport | HttpTransport | HTTP 传输层到使用。the HTTP transport to use |
io/agentscope/extensions/model/openai/OpenAIClient.java:89–91OpenAIClient
public OpenAIClient()
创建新的OpenAIClient 使用默认传输层来自工厂。
Create a new OpenAIClient with the default transport from factory.
io/agentscope/extensions/model/openai/OpenAIClient.java:99–101方法详情Method Detail
call
public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request)
向智能体发送输入并返回最终回复。
Make a synchronous API call.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
apiKey | String | API 键(类型 `String`)。the API key for authentication |
baseUrl | String | 基类 URL(类型 `String`)。the base URL (null for default) |
request | OpenAIRequest | OpenAI 请求。the OpenAI request |
OpenAIResponseOpenAIException可能抛出 `OpenAIException`。if the request fails
io/agentscope/extensions/model/openai/OpenAIClient.java:244–246call
public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options)
向智能体发送输入并返回最终回复。
Make a synchronous API call with options.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
apiKey | String | API 键(类型 `String`)。the API key for authentication |
baseUrl | String | 基类 URL(类型 `String`)。the base URL (null for default) |
request | OpenAIRequest | OpenAI 请求。the OpenAI request |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。additional options for headers and query params |
OpenAIResponseOpenAIException可能抛出 `OpenAIException`。if the request fails
io/agentscope/extensions/model/openai/OpenAIClient.java:258–353stream
public Flux<OpenAIResponse> stream(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options)
执行流相关操作。
Make a streaming API call with options for headers and query params.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
apiKey | String | API 键(类型 `String`)。the API key for authentication |
baseUrl | String | 基类 URL(类型 `String`)。the base URL (null for default) |
request | OpenAIRequest | OpenAI 请求。the OpenAI request |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。generation options containing additional headers and query params |
Flux<OpenAIResponse>io/agentscope/extensions/model/openai/OpenAIClient.java:364–466callApi
public String callApi(String apiKey, String baseUrl, String endpoint, Object requestBody)
调用API。
Make a generic API call to any OpenAI endpoint with JSON request body.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
apiKey | String | API 键(类型 `String`)。the API key for authentication |
baseUrl | String | 基类 URL(类型 `String`)。the base URL (null for default) |
endpoint | String | 端点地址(类型 `String`)。the API endpoint path (e.g., "/v1/images/generations") |
requestBody | Object | 请求正文(类型 `Object`)。the JSON request body as a Map or any serializable object |
StringOpenAIException可能抛出 `OpenAIException`。if the request fails
io/agentscope/extensions/model/openai/OpenAIClient.java:632–634callApi
public String callApi(String apiKey, String baseUrl, String endpoint, Object requestBody, GenerateOptions options)
调用API。
Make a generic API call to any OpenAI endpoint with JSON request body and custom options.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
apiKey | String | API 键(类型 `String`)。the API key for authentication |
baseUrl | String | 基类 URL(类型 `String`)。the base URL (null for default) |
endpoint | String | 端点地址(类型 `String`)。the API endpoint path (e.g., "/v1/images/generations") |
requestBody | Object | 请求正文(类型 `Object`)。the JSON request body as a Map or any serializable object |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。additional options for headers and query params |
StringOpenAIException可能抛出 `OpenAIException`。if the request fails
io/agentscope/extensions/model/openai/OpenAIClient.java:647–707getTransport
public HttpTransport getTransport()
获取传输层。
Get the HTTP transport used by this client.
HttpTransportio/agentscope/extensions/model/openai/OpenAIClient.java:729–731