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.
Author:
Julien Dubois, Christian Tzolov, Soby Chacko, Ilayaperumal Gopinathan
  • 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.
      Returns:
      the chat options
    • 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
    • safeAssistantMessage

      public @Nullable AssistantMessage safeAssistantMessage(@Nullable ChatResponse response)
      Safely extracts the assistant message from a chat response.
      Parameters:
      response - the chat response
      Returns:
      the assistant message, or null if not available
    • parseToolChoice

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

      public ChatOptions getDefaultOptions()
      Specified by:
      getDefaultOptions in interface ChatModel
    • setObservationConvention

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