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 type
I - the static request type
O - the static response type
All Implemented Interfaces:
Chain<Map<String,​String>,​String>
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
  • Constructor Details

  • Method Details

    • createRequest

      protected abstract I createRequest​(Map<String,​String> input)
      creates the request entity from the current document
      Parameters:
      input - the current document
      Returns:
      the request entity
    • createOutput

      protected abstract String createOutput​(O response)
      creates the chain output from the response entity
      Parameters:
      response - the response entity
      Returns:
      this chain's output
    • run

      public String run​(Map<String,​String> input)
      Description copied from interface: Chain
      Execute this Chain
      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 entity
      webClient - the WebClient to use for the request
      objectMapper - the ObjectMapper used 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 serialize
      objectMapper - ObjectMapper used for serialization
      Returns:
      serialized the serialized request body
    • getApiKey

      protected final String 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