Class OpenAiChatModel

java.lang.Object
org.springframework.ai.openai.OpenAiChatModel
All Implemented Interfaces:
ChatModel, StreamingChatModel, Model<Prompt,ChatResponse>, StreamingModel<Prompt,ChatResponse>

public final class OpenAiChatModel extends Object implements ChatModel
Chat Model implementation using the OpenAI Java SDK.

The assistant message of each choice is built from ordered MessageParts:

Chat Completions has no way to interleave these items on the wire, so an assistant message is replayed as one message: the text parts are joined into the content, the reasoning parts are joined into reasoning_content, unless another provider signed some of them, and the tool call parts become the tool_calls. Media and other parts are not replayed. User message parts are sent in their order, so text and media can interleave.
Author:
Julien Dubois, Christian Tzolov, Soby Chacko, Ilayaperumal Gopinathan, Thomas Vitale, Eric Bottard, Taewoong Kim, Jewoo Shin, guan xu, Raphael Vullriede, Dimitar Proynov
  • Field Details

    • CHAT_COMPLETIONS_PROVIDER

      public static final String CHAT_COMPLETIONS_PROVIDER
      The provider of the payloads this model produces. Only payloads of this provider are replayed by this model.
      Since:
      2.1.0
      See Also:
    • PAYLOAD_TOOL_CALL_ADDITIONAL_PROPERTIES

      public static final String PAYLOAD_TOOL_CALL_ADDITIONAL_PROPERTIES
      The kind of a ToolCallPart payload that holds the extra fields an OpenAI-compatible server returned on that tool call, as a JSON object string. The fields are sent back verbatim when the tool call is replayed.
      Since:
      2.1.0
      See Also:
  • Method Details

    • builder

      public static OpenAiChatModel.Builder builder()
      Creates a new builder for OpenAiChatModel.
      Returns:
      a new builder instance
    • getOptions

      public OpenAiChatOptions getOptions()
      Gets the chat options for this model.
      Specified by:
      getOptions in interface ChatModel
      Returns:
      the chat options
      Since:
      2.0.0
    • getDefaultOptions

      @Deprecated(forRemoval=true) public ChatOptions getDefaultOptions()
      Deprecated, for removal: This API element is subject to removal in a future version.
      use getOptions() instead.
      Specified by:
      getDefaultOptions in interface ChatModel
    • call

      public ChatResponse call(Prompt prompt)
      Description copied from interface: Model
      Executes a method call to the AI model.
      Specified by:
      call in interface ChatModel
      Specified by:
      call in interface Model<Prompt,ChatResponse>
      Parameters:
      prompt - the request object to be sent to the AI model
      Returns:
      the response from the AI model
    • stream

      public reactor.core.publisher.Flux<ChatResponse> stream(Prompt prompt)
      Description copied from interface: StreamingModel
      Executes a method call to the AI model.
      Specified by:
      stream in interface ChatModel
      Specified by:
      stream in interface StreamingChatModel
      Specified by:
      stream in interface StreamingModel<Prompt,ChatResponse>
      Parameters:
      prompt - the request object to be sent to the AI model
      Returns:
      the streaming response from the AI model
    • parseToolChoice

      public static com.openai.models.chat.completions.ChatCompletionToolChoiceOption parseToolChoice(tools.jackson.databind.JsonNode node)
    • setObservationConvention

      public void setObservationConvention(ChatModelObservationConvention observationConvention)
      Use the provided convention for reporting observation data
      Parameters:
      observationConvention - The provided convention