Class OpenAiApi
java.lang.Object
org.springframework.ai.openai.api.OpenAiApi
Single class implementation of the
OpenAI Chat Completion
API and OpenAI
Embedding API.
- Author:
- Christian Tzolov, Michael Lavelle, Mariusz Bernacki, Thomas Vitale, David Frizelle, Alexandros Pappas, Filip Hrisafov
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final recordRepresents a chat completion response returned by model, based on the provided input.static final recordRepresents a streamed chunk of a chat completion response returned by model, based on the provided input.static enumThe reason the model stopped generating tokens.static final recordMessage comprising the conversation.static final recordCreates a model request for the given chat conversation.static enumOpenAI Chat Completion Models.static final recordRepresents an embedding vector returned by embedding endpoint.static final recordList of multiple embedding responses.static enumOpenAI Embeddings Models: Embeddings.static final recordCreates an embedding vector representing the input text.static classRepresents a tool the model may call.static final recordLog probability information for the choice.static enumThe type of modality for the model completion.static enumSpecifies the processing type used for serving the request.static final recordUsage statistics for the completion request. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final OpenAiApi.ChatModelstatic final Stringstatic final Stringstatic final String -
Constructor Summary
ConstructorsConstructorDescriptionOpenAiApi(String baseUrl, ApiKey apiKey, org.springframework.util.MultiValueMap<String, String> headers, String completionsPath, String embeddingsPath, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new chat completion api. -
Method Summary
Modifier and TypeMethodDescriptionstatic OpenAiApi.Builderbuilder()org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletionEntity(OpenAiApi.ChatCompletionRequest chatRequest) Creates a model response for the given chat conversation.org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletionEntity(OpenAiApi.ChatCompletionRequest chatRequest, org.springframework.util.MultiValueMap<String, String> additionalHttpHeader) Creates a model response for the given chat conversation.reactor.core.publisher.Flux<OpenAiApi.ChatCompletionChunk> chatCompletionStream(OpenAiApi.ChatCompletionRequest chatRequest) Creates a streaming chat response for the given chat conversation.reactor.core.publisher.Flux<OpenAiApi.ChatCompletionChunk> chatCompletionStream(OpenAiApi.ChatCompletionRequest chatRequest, org.springframework.util.MultiValueMap<String, String> additionalHttpHeader) Creates a streaming chat response for the given chat conversation.<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.static StringReturns a string containing all text values from the given media content list.mutate()Returns a builder pre-populated with the current configuration for mutation.
-
Field Details
-
HTTP_USER_AGENT_HEADER
- See Also:
-
SPRING_AI_USER_AGENT
- See Also:
-
DEFAULT_CHAT_MODEL
-
DEFAULT_EMBEDDING_MODEL
-
-
Constructor Details
-
OpenAiApi
public OpenAiApi(String baseUrl, ApiKey apiKey, org.springframework.util.MultiValueMap<String, String> headers, String completionsPath, String embeddingsPath, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new chat completion api.- Parameters:
baseUrl- api base URL.apiKey- OpenAI apiKey.headers- the http headers to use.completionsPath- the path to the chat completions endpoint.embeddingsPath- the path to the embeddings endpoint.restClientBuilder- RestClient builder.webClientBuilder- WebClient builder.responseErrorHandler- Response error handler.
-
-
Method Details
-
mutate
Returns a builder pre-populated with the current configuration for mutation. -
builder
-
getTextContent
Returns a string containing all text values from the given media content list. Only elements of type "text" are processed and concatenated in order.- Parameters:
content- The list ofOpenAiApi.ChatCompletionMessage.MediaContent- Returns:
- a string containing all text values from "text" type elements
- Throws:
IllegalArgumentException- if content is null
-
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.ChatCompletionas a body and HTTP status code and headers.
-
chatCompletionEntity
public org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletionEntity(OpenAiApi.ChatCompletionRequest chatRequest, org.springframework.util.MultiValueMap<String, String> additionalHttpHeader) Creates a model response for the given chat conversation.- Parameters:
chatRequest- The chat completion request.additionalHttpHeader- Optional, additional HTTP headers to be added to the request.- Returns:
- Entity response with
OpenAiApi.ChatCompletionas 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
Fluxstream from chat completion chunks.
-
chatCompletionStream
public reactor.core.publisher.Flux<OpenAiApi.ChatCompletionChunk> chatCompletionStream(OpenAiApi.ChatCompletionRequest chatRequest, org.springframework.util.MultiValueMap<String, String> additionalHttpHeader) Creates a streaming chat response for the given chat conversation.- Parameters:
chatRequest- The chat completion request. Must have the stream property set to true.additionalHttpHeader- Optional, additional HTTP headers to be added to the request.- Returns:
- Returns a
Fluxstream 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 aStringorListof tokens (e.g. Integers). For embedding multiple inputs in a single request, You can pass aListofStringorListofListof 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.Embeddingwrapped inOpenAiApi.EmbeddingList.
-