Package org.springframework.ai.openai
Class OpenAiChatClient
java.lang.Object
org.springframework.ai.model.function.AbstractFunctionCallSupport<OpenAiApi.ChatCompletionMessage,OpenAiApi.ChatCompletionRequest,org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>>
org.springframework.ai.openai.OpenAiChatClient
- All Implemented Interfaces:
ChatClient
,StreamingChatClient
,ModelClient<Prompt,
,ChatResponse> StreamingModelClient<Prompt,
ChatResponse>
public class OpenAiChatClient
extends AbstractFunctionCallSupport<OpenAiApi.ChatCompletionMessage,OpenAiApi.ChatCompletionRequest,org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>>
implements ChatClient, StreamingChatClient
- Author:
- Mark Pollack, Christian Tzolov, Ueibin Kim, John Blum, Josh Long, Jemin Huh
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionfinal org.springframework.retry.support.RetryTemplate
The retry template used to retry the OpenAI API calls.Fields inherited from class org.springframework.ai.model.function.AbstractFunctionCallSupport
functionCallbackContext, functionCallbackRegister, IS_RUNTIME_CALL
-
Constructor Summary
ConstructorDescriptionOpenAiChatClient
(OpenAiApi openAiApi) Creates an instance of the OpenAiChatClient.OpenAiChatClient
(OpenAiApi openAiApi, OpenAiChatOptions options) Initializes an instance of the OpenAiChatClient.OpenAiChatClient
(OpenAiApi openAiApi, OpenAiChatOptions options, FunctionCallbackContext functionCallbackContext, org.springframework.retry.support.RetryTemplate retryTemplate) Initializes a new instance of the OpenAiChatClient. -
Method Summary
Modifier and TypeMethodDescriptionExecutes a method call to the AI model.protected org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>
protected OpenAiApi.ChatCompletionRequest
doCreateToolResponseRequest
(OpenAiApi.ChatCompletionRequest previousRequest, OpenAiApi.ChatCompletionMessage responseMessage, List<OpenAiApi.ChatCompletionMessage> conversationHistory) protected OpenAiApi.ChatCompletionMessage
doGetToolResponseMessage
(org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletion) protected List<OpenAiApi.ChatCompletionMessage>
protected boolean
isToolFunctionCall
(org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletion) reactor.core.publisher.Flux<ChatResponse>
Executes a method call to the AI model.Methods inherited from class org.springframework.ai.model.function.AbstractFunctionCallSupport
callWithFunctionSupport, getFunctionCallbackRegister, handleFunctionCallbackConfigurations, handleFunctionCallOrReturn, resolveFunctionCallbacks
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.ai.chat.ChatClient
call
Methods inherited from interface org.springframework.ai.chat.StreamingChatClient
stream
-
Field Details
-
retryTemplate
public final org.springframework.retry.support.RetryTemplate retryTemplateThe retry template used to retry the OpenAI API calls.
-
-
Constructor Details
-
OpenAiChatClient
Creates an instance of the OpenAiChatClient.- Parameters:
openAiApi
- The OpenAiApi instance to be used for interacting with the OpenAI Chat API.- Throws:
IllegalArgumentException
- if openAiApi is null
-
OpenAiChatClient
Initializes an instance of the OpenAiChatClient.- Parameters:
openAiApi
- The OpenAiApi instance to be used for interacting with the OpenAI Chat API.options
- The OpenAiChatOptions to configure the chat client.
-
OpenAiChatClient
public OpenAiChatClient(OpenAiApi openAiApi, OpenAiChatOptions options, FunctionCallbackContext functionCallbackContext, org.springframework.retry.support.RetryTemplate retryTemplate) Initializes a new instance of the OpenAiChatClient.- Parameters:
openAiApi
- The OpenAiApi instance to be used for interacting with the OpenAI Chat API.options
- The OpenAiChatOptions to configure the chat client.functionCallbackContext
- The function callback context.retryTemplate
- The retry template.
-
-
Method Details
-
call
Description copied from interface:ModelClient
Executes a method call to the AI model.- Specified by:
call
in interfaceChatClient
- Specified by:
call
in interfaceModelClient<Prompt,
ChatResponse> - Parameters:
prompt
- the request object to be sent to the AI model- Returns:
- the response from the AI model
-
stream
Description copied from interface:StreamingModelClient
Executes a method call to the AI model.- Specified by:
stream
in interfaceStreamingChatClient
- Specified by:
stream
in interfaceStreamingModelClient<Prompt,
ChatResponse> - Parameters:
prompt
- the request object to be sent to the AI model- Returns:
- the streaming response from the AI model
-
doCreateToolResponseRequest
protected OpenAiApi.ChatCompletionRequest doCreateToolResponseRequest(OpenAiApi.ChatCompletionRequest previousRequest, OpenAiApi.ChatCompletionMessage responseMessage, List<OpenAiApi.ChatCompletionMessage> conversationHistory) - Specified by:
doCreateToolResponseRequest
in classAbstractFunctionCallSupport<OpenAiApi.ChatCompletionMessage,
OpenAiApi.ChatCompletionRequest, org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>>
-
doGetUserMessages
protected List<OpenAiApi.ChatCompletionMessage> doGetUserMessages(OpenAiApi.ChatCompletionRequest request) - Specified by:
doGetUserMessages
in classAbstractFunctionCallSupport<OpenAiApi.ChatCompletionMessage,
OpenAiApi.ChatCompletionRequest, org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>>
-
doGetToolResponseMessage
protected OpenAiApi.ChatCompletionMessage doGetToolResponseMessage(org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletion) - Specified by:
doGetToolResponseMessage
in classAbstractFunctionCallSupport<OpenAiApi.ChatCompletionMessage,
OpenAiApi.ChatCompletionRequest, org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>>
-
doChatCompletion
protected org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> doChatCompletion(OpenAiApi.ChatCompletionRequest request) - Specified by:
doChatCompletion
in classAbstractFunctionCallSupport<OpenAiApi.ChatCompletionMessage,
OpenAiApi.ChatCompletionRequest, org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>>
-
isToolFunctionCall
protected boolean isToolFunctionCall(org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion> chatCompletion) - Specified by:
isToolFunctionCall
in classAbstractFunctionCallSupport<OpenAiApi.ChatCompletionMessage,
OpenAiApi.ChatCompletionRequest, org.springframework.http.ResponseEntity<OpenAiApi.ChatCompletion>>
-