Class OpenAiSdkChatModel

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

public class OpenAiSdkChatModel extends Object implements ChatModel
Chat Model implementation using the OpenAI Java SDK.
Author:
Julien Dubois, Christian Tzolov
  • Constructor Details

    • OpenAiSdkChatModel

      public OpenAiSdkChatModel()
      Creates a new OpenAiSdkChatModel with default options.
    • OpenAiSdkChatModel

      public OpenAiSdkChatModel(OpenAiSdkChatOptions options)
      Creates a new OpenAiSdkChatModel with the given options.
      Parameters:
      options - the chat options
    • OpenAiSdkChatModel

      public OpenAiSdkChatModel(OpenAiSdkChatOptions options, io.micrometer.observation.ObservationRegistry observationRegistry)
      Creates a new OpenAiSdkChatModel with the given options and observation registry.
      Parameters:
      options - the chat options
      observationRegistry - the observation registry
    • OpenAiSdkChatModel

      public OpenAiSdkChatModel(OpenAiSdkChatOptions options, ToolCallingManager toolCallingManager, io.micrometer.observation.ObservationRegistry observationRegistry)
      Creates a new OpenAiSdkChatModel with the given options, tool calling manager, and observation registry.
      Parameters:
      options - the chat options
      toolCallingManager - the tool calling manager
      observationRegistry - the observation registry
    • OpenAiSdkChatModel

      public OpenAiSdkChatModel(com.openai.client.OpenAIClient openAIClient, com.openai.client.OpenAIClientAsync openAiClientAsync)
      Creates a new OpenAiSdkChatModel with the given OpenAI clients.
      Parameters:
      openAIClient - the synchronous OpenAI client
      openAiClientAsync - the asynchronous OpenAI client
    • OpenAiSdkChatModel

      public OpenAiSdkChatModel(com.openai.client.OpenAIClient openAIClient, com.openai.client.OpenAIClientAsync openAiClientAsync, OpenAiSdkChatOptions options)
      Creates a new OpenAiSdkChatModel with the given OpenAI clients and options.
      Parameters:
      openAIClient - the synchronous OpenAI client
      openAiClientAsync - the asynchronous OpenAI client
      options - the chat options
    • OpenAiSdkChatModel

      public OpenAiSdkChatModel(com.openai.client.OpenAIClient openAIClient, com.openai.client.OpenAIClientAsync openAiClientAsync, OpenAiSdkChatOptions options, io.micrometer.observation.ObservationRegistry observationRegistry)
      Creates a new OpenAiSdkChatModel with the given OpenAI clients, options, and observation registry.
      Parameters:
      openAIClient - the synchronous OpenAI client
      openAiClientAsync - the asynchronous OpenAI client
      options - the chat options
      observationRegistry - the observation registry
    • OpenAiSdkChatModel

      public OpenAiSdkChatModel(com.openai.client.OpenAIClient openAiClient, com.openai.client.OpenAIClientAsync openAiClientAsync, OpenAiSdkChatOptions options, ToolCallingManager toolCallingManager, io.micrometer.observation.ObservationRegistry observationRegistry, ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate)
      Creates a new OpenAiSdkChatModel with all configuration options.
      Parameters:
      openAiClient - the synchronous OpenAI client
      openAiClientAsync - the asynchronous OpenAI client
      options - the chat options
      toolCallingManager - the tool calling manager
      observationRegistry - the observation registry
      toolExecutionEligibilityPredicate - the predicate to determine tool execution eligibility
  • Method Details

    • getOptions

      public OpenAiSdkChatOptions 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
    • internalCall

      public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatResponse)
      Internal method to handle chat completion calls with tool execution support.
      Parameters:
      prompt - the prompt for the chat completion
      previousChatResponse - the previous chat response for accumulating usage
      Returns:
      the chat response
    • 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 AssistantMessage safeAssistantMessage(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
    • internalStream

      public reactor.core.publisher.Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousChatResponse)
      Internal method to handle streaming chat completion calls with tool execution support.
      Parameters:
      prompt - the prompt for the chat completion
      previousChatResponse - the previous chat response for accumulating usage
      Returns:
      a Flux of chat responses
    • parseToolChoice

      public static com.openai.models.chat.completions.ChatCompletionToolChoiceOption parseToolChoice(com.fasterxml.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