Class OpenAiChain<P extends OpenAiParameters<P>,I extends P,O extends OpenAiResponse<?>>
java.lang.Object
com.github.hakenadu.javalangchains.chains.llm.LargeLanguageModelChain
com.github.hakenadu.javalangchains.chains.llm.openai.OpenAiChain<P,I,O>
- Type Parameters:
P- the static parameter typeI- the static request typeO- the static response type
- Direct Known Subclasses:
OpenAiChatCompletionsChain,OpenAiCompletionsChain
public abstract class OpenAiChain<P extends OpenAiParameters<P>,I extends P,O extends OpenAiResponse<?>> extends LargeLanguageModelChain
LargeLanguageModelChain for usage with the OpenAI /completions API-
Field Summary
-
Constructor Summary
Constructors Modifier Constructor Description protectedOpenAiChain(String promptTemplate, String requestPath, Class<O> responseClass, P parameters, String apiKey)protectedOpenAiChain(String promptTemplate, String requestPath, Class<O> responseClass, P parameters, String apiKey, com.fasterxml.jackson.databind.ObjectMapper objectMapper, org.springframework.web.reactive.function.client.WebClient webClient) -
Method Summary
Modifier and Type Method Description static com.fasterxml.jackson.databind.ObjectMappercreateDefaultObjectMapper()static org.springframework.web.reactive.function.client.WebClientcreateDefaultWebClient()protected abstract StringcreateOutput(O response)creates the chain output from the response entityprotected abstract IcreateRequest(Map<String,String> input)creates the request entity from the current documentprotected org.springframework.web.reactive.function.client.WebClient.ResponseSpeccreateResponseSpec(I request, org.springframework.web.reactive.function.client.WebClient webClient, com.fasterxml.jackson.databind.ObjectMapper objectMapper)executes the request to the OpenAI API.protected StringgetApiKey()protected StringrequestToBody(I request, com.fasterxml.jackson.databind.ObjectMapper objectMapper)Serializes the request entityStringrun(Map<String,String> input)Execute thisChainMethods inherited from class com.github.hakenadu.javalangchains.chains.llm.LargeLanguageModelChain
getPromptTemplate
-
Constructor Details
-
OpenAiChain
protected OpenAiChain(String promptTemplate, String requestPath, Class<O> responseClass, P parameters, String apiKey, com.fasterxml.jackson.databind.ObjectMapper objectMapper, org.springframework.web.reactive.function.client.WebClient webClient)- Parameters:
promptTemplate-LargeLanguageModelChain.getPromptTemplate()requestPath-requestPathresponseClass-responseClassparameters-parametersrapiKey-apiKeyobjectMapper-objectMapperwebClient-webClient
-
OpenAiChain
protected OpenAiChain(String promptTemplate, String requestPath, Class<O> responseClass, P parameters, String apiKey)- Parameters:
promptTemplate-LargeLanguageModelChain.getPromptTemplate()requestPath-requestPathresponseClass-responseClassparameters-parametersapiKey-apiKey
-
-
Method Details
-
createRequest
creates the request entity from the current document- Parameters:
input- the current document- Returns:
- the request entity
-
createOutput
creates the chain output from the response entity- Parameters:
response- the response entity- Returns:
- this chain's output
-
run
Description copied from interface:ChainExecute thisChain- Parameters:
input- this chain's input- Returns:
- this chain's output
-
createResponseSpec
protected org.springframework.web.reactive.function.client.WebClient.ResponseSpec createResponseSpec(I request, org.springframework.web.reactive.function.client.WebClient webClient, com.fasterxml.jackson.databind.ObjectMapper objectMapper)executes the request to the OpenAI API. Protected so that it may be overridden for other OpenAI API Providers.- Parameters:
request- the request entitywebClient- theWebClientto use for the requestobjectMapper- theObjectMapperused for body serialization- Returns:
- the
WebClient.ResponseSpec
-
requestToBody
protected String requestToBody(I request, com.fasterxml.jackson.databind.ObjectMapper objectMapper)Serializes the request entity- Parameters:
request- the request entity to serializeobjectMapper-ObjectMapperused for serialization- Returns:
- serialized the serialized request body
-
getApiKey
- Returns:
apiKeyp
-
createDefaultObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper createDefaultObjectMapper()- Returns:
- a default configured
ObjectMapper
-
createDefaultWebClient
public static org.springframework.web.reactive.function.client.WebClient createDefaultWebClient()- Returns:
- a default configured
WebClient
-