类 OpenAIClient
java.lang.Object
io.agentscope.core.model.OpenAIClient
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
-
字段概要
字段 -
构造器概要
构造器构造器说明Create a new OpenAIClient with the default transport from factory.OpenAIClient(HttpTransport transport) Create a new stateless OpenAIClient. -
方法概要
修饰符和类型方法说明call(String apiKey, String baseUrl, OpenAIRequest request) Make a synchronous API call.call(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options) Make a synchronous API call with options.Make a generic API call to any OpenAI endpoint with JSON request body.callApi(String apiKey, String baseUrl, String endpoint, Object requestBody, GenerateOptions options) Make a generic API call to any OpenAI endpoint with JSON request body and custom options.Get the HTTP transport used by this client.reactor.core.publisher.Flux<OpenAIResponse> stream(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options) Make a streaming API call with options for headers and query params.
-
字段详细资料
-
DEFAULT_BASE_URL
Default base URL for OpenAI API.- 另请参阅:
-
DEFAULT_BASE_URL_WITH_VERSION
Default base URL with version.- 另请参阅:
-
CHAT_COMPLETIONS_ENDPOINT
Chat completions API endpoint.- 另请参阅:
-
-
构造器详细资料
-
OpenAIClient
Create a new stateless OpenAIClient.- 参数:
transport- the HTTP transport to use
-
OpenAIClient
public OpenAIClient()Create a new OpenAIClient with the default transport from factory.Uses
HttpTransportFactory.getDefault()for the transport.
-
-
方法详细资料
-
call
Make a synchronous API call.- 参数:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)request- the OpenAI request- 返回:
- the OpenAI response
- 抛出:
OpenAIException- if the request fails
-
call
public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options) Make a synchronous API call with options.- 参数:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)request- the OpenAI requestoptions- additional options for headers and query params- 返回:
- the OpenAI response
- 抛出:
OpenAIException- if the request fails
-
stream
public reactor.core.publisher.Flux<OpenAIResponse> stream(String apiKey, String baseUrl, OpenAIRequest request, GenerateOptions options) Make a streaming API call with options for headers and query params.- 参数:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)request- the OpenAI requestoptions- generation options containing additional headers and query params- 返回:
- a Flux of OpenAI responses (one per SSE event)
-
callApi
Make a generic API call to any OpenAI endpoint with JSON request body.This method can be used for endpoints other than chat completions, such as:
- /v1/images/generations
- /v1/audio/speech
- Other JSON-based OpenAI API endpoints
- 参数:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)endpoint- the API endpoint path (e.g., "/v1/images/generations")requestBody- the JSON request body as a Map or any serializable object- 返回:
- the raw HTTP response body as a String
- 抛出:
OpenAIException- if the request fails
-
callApi
public String callApi(String apiKey, String baseUrl, String endpoint, Object requestBody, GenerateOptions options) Make a generic API call to any OpenAI endpoint with JSON request body and custom options.- 参数:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)endpoint- the API endpoint path (e.g., "/v1/images/generations")requestBody- the JSON request body as a Map or any serializable objectoptions- additional options for headers and query params- 返回:
- the raw HTTP response body as a String
- 抛出:
OpenAIException- if the request fails
-
getTransport
Get the HTTP transport used by this client.- 返回:
- the HTTP transport
-