Package org.springframework.ai.openai
Class OpenAiChatModel
java.lang.Object
org.springframework.ai.openai.OpenAiChatModel
- All Implemented Interfaces:
ChatModel,StreamingChatModel,Model<Prompt,,ChatResponse> StreamingModel<Prompt,ChatResponse>
Chat Model implementation using the OpenAI Java SDK.
The assistant message of each choice is built from ordered MessageParts:
- the
reasoning_content(orreasoning) field that some OpenAI-compatible servers return becomes aReasoningPartwithout a payload; - the
contentbecomes aTextPart; - each
functiontool call becomes aToolCallPart, and the extra fields of the tool call, if any, become itsPAYLOAD_TOOL_CALL_ADDITIONAL_PROPERTIESpayload; - the audio output becomes a
MediaPart.
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creatingOpenAiChatModelinstances.static classResponse format (text, json_object, json_schema) for OpenAiChatModel responses. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringTheproviderof the payloads this model produces.static final StringThekindof aToolCallPartpayload that holds the extra fields an OpenAI-compatible server returned on that tool call, as a JSON object string. -
Method Summary
Modifier and TypeMethodDescriptionstatic OpenAiChatModel.Builderbuilder()Creates a new builder forOpenAiChatModel.Executes a method call to the AI model.Deprecated, for removal: This API element is subject to removal in a future version.Gets the chat options for this model.static com.openai.models.chat.completions.ChatCompletionToolChoiceOptionparseToolChoice(tools.jackson.databind.JsonNode node) voidsetObservationConvention(ChatModelObservationConvention observationConvention) Use the provided convention for reporting observation datareactor.core.publisher.Flux<ChatResponse>Executes a method call to the AI model.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.chat.model.StreamingChatModel
stream, stream
-
Field Details
-
CHAT_COMPLETIONS_PROVIDER
Theproviderof 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
Thekindof aToolCallPartpayload 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
Creates a new builder forOpenAiChatModel.- Returns:
- a new builder instance
-
getOptions
Gets the chat options for this model.- Specified by:
getOptionsin interfaceChatModel- Returns:
- the chat options
- Since:
- 2.0.0
-
getDefaultOptions
Deprecated, for removal: This API element is subject to removal in a future version.usegetOptions()instead.- Specified by:
getDefaultOptionsin interfaceChatModel
-
call
Description copied from interface:ModelExecutes a method call to the AI model. -
stream
Description copied from interface:StreamingModelExecutes a method call to the AI model.- Specified by:
streamin interfaceChatModel- Specified by:
streamin interfaceStreamingChatModel- Specified by:
streamin interfaceStreamingModel<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
Use the provided convention for reporting observation data- Parameters:
observationConvention- The provided convention
-
getOptions()instead.