AS AgentScopeAPI 手册 / API Reference

OpenAIClient

public class OpenAIClient
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:66–732

OpenAI 客户端,封装与远端服务的连接和调用。位于 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_URL
staticfinal
String
用于OpenAI API的默认基类 URL。
Default base URL for OpenAI API.
DEFAULT_BASE_URL_WITH_VERSION
staticfinal
String
默认基类 URL 使用版本字段。
Default base URL with version.
CHAT_COMPLETIONS_ENDPOINT
staticfinal
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
staticfinal

用于OpenAI API的默认基类 URL。

Default base URL for OpenAI API.

源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:74

DEFAULT_BASE_URL_WITH_VERSION

public static final String DEFAULT_BASE_URL_WITH_VERSION
staticfinal

默认基类 URL 使用版本字段。声明为 public static final String DEFAULT_BASE_URL_WITH_VERSION。

Default base URL with version.

源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:77

CHAT_COMPLETIONS_ENDPOINT

public static final String CHAT_COMPLETIONS_ENDPOINT
staticfinal

聊天补全端点字段。声明为 public static final String CHAT_COMPLETIONS_ENDPOINT。

Chat completions API endpoint.

源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:80

构造器详情Constructor Detail

OpenAIClient

public OpenAIClient(HttpTransport transport)

初始化 OpenAIClient 实例。

Create a new stateless OpenAIClient.

参数Parameters
名称Name类型Type说明Description
transportHttpTransportHTTP 传输层到使用。the HTTP transport to use
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:89–91

OpenAIClient

public OpenAIClient()

创建新的OpenAIClient 使用默认传输层来自工厂。

Create a new OpenAIClient with the default transport from factory.

源码Source 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.

参数Parameters
名称Name类型Type说明Description
apiKeyStringAPI 键(类型 `String`)。the API key for authentication
baseUrlString基类 URL(类型 `String`)。the base URL (null for default)
requestOpenAIRequestOpenAI 请求。the OpenAI request
返回Returns OpenAIResponse
OpenAI 响应。the OpenAI response
抛出Throws
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:244–246

call

public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options)

向智能体发送输入并返回最终回复。

Make a synchronous API call with options.

参数Parameters
名称Name类型Type说明Description
apiKeyStringAPI 键(类型 `String`)。the API key for authentication
baseUrlString基类 URL(类型 `String`)。the base URL (null for default)
requestOpenAIRequestOpenAI 请求。the OpenAI request
optionsGenerateOptions选项(类型 `GenerateOptions`)。additional options for headers and query params
返回Returns OpenAIResponse
OpenAI 响应。the OpenAI response
抛出Throws
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:258–353

stream

public Flux<OpenAIResponse> stream(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options)

执行流相关操作。

Make a streaming API call with options for headers and query params.

参数Parameters
名称Name类型Type说明Description
apiKeyStringAPI 键(类型 `String`)。the API key for authentication
baseUrlString基类 URL(类型 `String`)。the base URL (null for default)
requestOpenAIRequestOpenAI 请求。the OpenAI request
optionsGenerateOptions选项(类型 `GenerateOptions`)。generation options containing additional headers and query params
返回Returns Flux<OpenAIResponse>
返回Flux。a Flux of OpenAI responses (one per SSE event)
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:364–466

callApi

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.

参数Parameters
名称Name类型Type说明Description
apiKeyStringAPI 键(类型 `String`)。the API key for authentication
baseUrlString基类 URL(类型 `String`)。the base URL (null for default)
endpointString端点地址(类型 `String`)。the API endpoint path (e.g., "/v1/images/generations")
requestBodyObject请求正文(类型 `Object`)。the JSON request body as a Map or any serializable object
返回Returns String
返回字符串结果。the raw HTTP response body as a String
抛出Throws
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:632–634

callApi

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.

参数Parameters
名称Name类型Type说明Description
apiKeyStringAPI 键(类型 `String`)。the API key for authentication
baseUrlString基类 URL(类型 `String`)。the base URL (null for default)
endpointString端点地址(类型 `String`)。the API endpoint path (e.g., "/v1/images/generations")
requestBodyObject请求正文(类型 `Object`)。the JSON request body as a Map or any serializable object
optionsGenerateOptions选项(类型 `GenerateOptions`)。additional options for headers and query params
返回Returns String
返回字符串结果。the raw HTTP response body as a String
抛出Throws
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:647–707

getTransport

public HttpTransport getTransport()

获取传输层。

Get the HTTP transport used by this client.

返回Returns HttpTransport
HTTP 传输层。the HTTP transport
源码Source io/agentscope/extensions/model/openai/OpenAIClient.java:729–731
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.