类 OpenAIClient

java.lang.Object
io.agentscope.core.model.OpenAIClient

public class OpenAIClient extends Object
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
  • 字段详细资料

    • DEFAULT_BASE_URL

      public static final String DEFAULT_BASE_URL
      Default base URL for OpenAI API.
      另请参阅:
    • DEFAULT_BASE_URL_WITH_VERSION

      public static final String DEFAULT_BASE_URL_WITH_VERSION
      Default base URL with version.
      另请参阅:
    • CHAT_COMPLETIONS_ENDPOINT

      public static final String CHAT_COMPLETIONS_ENDPOINT
      Chat completions API endpoint.
      另请参阅:
  • 构造器详细资料

    • OpenAIClient

      public OpenAIClient(HttpTransport transport)
      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

      public OpenAIResponse call(String apiKey, String baseUrl, OpenAIRequest request)
      Make a synchronous API call.
      参数:
      apiKey - the API key for authentication
      baseUrl - 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 authentication
      baseUrl - the base URL (null for default)
      request - the OpenAI request
      options - 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 authentication
      baseUrl - the base URL (null for default)
      request - the OpenAI request
      options - generation options containing additional headers and query params
      返回:
      a Flux of OpenAI responses (one per SSE event)
    • callApi

      public String callApi(String apiKey, String baseUrl, String endpoint, Object requestBody)
      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 authentication
      baseUrl - 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 authentication
      baseUrl - 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
      options - additional options for headers and query params
      返回:
      the raw HTTP response body as a String
      抛出:
      OpenAIException - if the request fails
    • getTransport

      public HttpTransport getTransport()
      Get the HTTP transport used by this client.
      返回:
      the HTTP transport