Class OpenAiApi

java.lang.Object
org.springframework.ai.openai.api.OpenAiApi

public class OpenAiApi extends Object
Single class implementation of the OpenAI Chat Completion API: https://platform.openai.com/docs/api-reference/chat and OpenAI Embedding API: https://platform.openai.com/docs/api-reference/embeddings.
Author:
Christian Tzolov, Michael Lavelle
  • Field Details

    • DEFAULT_CHAT_MODEL

      public static final String DEFAULT_CHAT_MODEL
    • DEFAULT_EMBEDDING_MODEL

      public static final String DEFAULT_EMBEDDING_MODEL
  • Constructor Details

    • OpenAiApi

      public OpenAiApi(String openAiToken)
      Create an new chat completion api with base URL set to https://api.openai.com
      Parameters:
      openAiToken - OpenAI apiKey.
    • OpenAiApi

      public OpenAiApi(String baseUrl, String openAiToken)
      Create a new chat completion api.
      Parameters:
      baseUrl - api base URL.
      openAiToken - OpenAI apiKey.
    • OpenAiApi

      public OpenAiApi(String baseUrl, String openAiToken, org.springframework.web.client.RestClient.Builder restClientBuilder)
      Create a new chat completion api.
      Parameters:
      baseUrl - api base URL.
      openAiToken - OpenAI apiKey.
      restClientBuilder - RestClient builder.
    • OpenAiApi

      public OpenAiApi(String baseUrl, String openAiToken, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler)
      Create a new chat completion api.
      Parameters:
      baseUrl - api base URL.
      openAiToken - OpenAI apiKey.
      restClientBuilder - RestClient builder.
      responseErrorHandler - Response error handler.
  • Method Details

    • chatCompletionEntity

      public org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletionEntity(OpenAiApi.ChatCompletionRequest chatRequest)
      Creates a model response for the given chat conversation.
      Parameters:
      chatRequest - The chat completion request.
      Returns:
      Entity response with OpenAiApi.ChatCompletion as a body and HTTP status code and headers.
    • chatCompletionStream

      public reactor.core.publisher.Flux<OpenAiApi.ChatCompletionChunk> chatCompletionStream(OpenAiApi.ChatCompletionRequest chatRequest)
      Creates a streaming chat response for the given chat conversation.
      Parameters:
      chatRequest - The chat completion request. Must have the stream property set to true.
      Returns:
      Returns a Flux stream from chat completion chunks.
    • embeddings

      public <T> org.springframework.http.ResponseEntity<OpenAiApi.EmbeddingList<OpenAiApi.Embedding>> embeddings(OpenAiApi.EmbeddingRequest<T> embeddingRequest)
      Creates an embedding vector representing the input text or token array.
      Type Parameters:
      T - Type of the entity in the data list. Can be a String or List of tokens (e.g. Integers). For embedding multiple inputs in a single request, You can pass a List of String or List of List of tokens. For example:
      List.of("text1", "text2", "text3") or List.of(List.of(1, 2, 3), List.of(3, 4, 5)) 
      Parameters:
      embeddingRequest - The embedding request.
      Returns:
      Returns list of OpenAiApi.Embedding wrapped in OpenAiApi.EmbeddingList.