Class OpenAIClient
java.lang.Object
io.agentscope.extensions.model.openai.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
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new OpenAIClient with the default transport from factory.OpenAIClient(io.agentscope.core.model.transport.HttpTransport transport) Create a new stateless OpenAIClient. -
Method Summary
Modifier and TypeMethodDescriptioncall(String apiKey, String baseUrl, OpenAIRequest request) Make a synchronous API call.call(String apiKey, String baseUrl, OpenAIRequest request, io.agentscope.core.model.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, io.agentscope.core.model.GenerateOptions options) Make a generic API call to any OpenAI endpoint with JSON request body and custom options.io.agentscope.core.model.transport.HttpTransportGet the HTTP transport used by this client.reactor.core.publisher.Flux<OpenAIResponse>stream(String apiKey, String baseUrl, OpenAIRequest request, io.agentscope.core.model.GenerateOptions options) Make a streaming API call with options for headers and query params.
-
Field Details
-
DEFAULT_BASE_URL
Default base URL for OpenAI API.- See Also:
-
DEFAULT_BASE_URL_WITH_VERSION
Default base URL with version.- See Also:
-
CHAT_COMPLETIONS_ENDPOINT
Chat completions API endpoint.- See Also:
-
-
Constructor Details
-
OpenAIClient
public OpenAIClient(io.agentscope.core.model.transport.HttpTransport transport) Create a new stateless OpenAIClient.- Parameters:
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.
-
-
Method Details
-
call
Make a synchronous API call.- Parameters:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)request- the OpenAI request- Returns:
- the OpenAI response
- Throws:
OpenAIException- if the request fails
-
call
public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request, io.agentscope.core.model.GenerateOptions options) Make a synchronous API call with options.- Parameters:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)request- the OpenAI requestoptions- additional options for headers and query params- Returns:
- the OpenAI response
- Throws:
OpenAIException- if the request fails
-
stream
public reactor.core.publisher.Flux<OpenAIResponse> stream(String apiKey, String baseUrl, OpenAIRequest request, io.agentscope.core.model.GenerateOptions options) Make a streaming API call with options for headers and query params.- Parameters:
apiKey- the API key for authenticationbaseUrl- the base URL (null for default)request- the OpenAI requestoptions- generation options containing additional headers and query params- Returns:
- 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
- Parameters:
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- Returns:
- the raw HTTP response body as a String
- Throws:
OpenAIException- if the request fails
-
callApi
public String callApi(String apiKey, String baseUrl, String endpoint, Object requestBody, io.agentscope.core.model.GenerateOptions options) Make a generic API call to any OpenAI endpoint with JSON request body and custom options.- Parameters:
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- Returns:
- the raw HTTP response body as a String
- Throws:
OpenAIException- if the request fails
-
getTransport
public io.agentscope.core.model.transport.HttpTransport getTransport()Get the HTTP transport used by this client.- Returns:
- the HTTP transport
-