Class OpenAIChatBuilderImpl

  • All Implemented Interfaces:
    AIChatBuilder

    public class OpenAIChatBuilderImpl
    extends Object
    implements AIChatBuilder
    Implementation of AIChatBuilder for creating and executing OpenAI chat completion requests. That does work for interfaces similar to OpenAI chat completion - e.g. Anthropic Claude , local models with LM Studio , probably more.
    See Also:
    "https://platform.openai.com/docs/api-reference/chat", "https://docs.anthropic.com/claude/reference/messages_post"
    • Field Detail

      • MODEL_OPENAIJSON

        public static final String MODEL_OPENAIJSON
        Pseudo model that outputs the json that would be sent to OpenAI and writes that to the output, instead of actually calling OpenAI. Mostly for debugging.
        See Also:
        Constant Field Values
      • ENV_ANTHROPIC_VERSION

        public static final String ENV_ANTHROPIC_VERSION
        Environment variable for the Anthropic API version.
        See Also:
        Constant Field Values
      • ENV_OPENAI_API_KEY

        public static final String ENV_OPENAI_API_KEY
        Environment variable for the OpenAI API key.
        See Also:
        Constant Field Values
      • ENV_ANTHROPIC_API_KEY

        public static final String ENV_ANTHROPIC_API_KEY
        Environment variable for the Anthropic API key.
        See Also:
        Constant Field Values
      • gson

        protected static final com.google.gson.Gson gson
      • CODEBLOCK_PATTERN

        protected final Pattern CODEBLOCK_PATTERN
      • model

        protected String model
      • apiKey

        protected String apiKey
      • organizationId

        protected String organizationId
      • maxTokens

        protected int maxTokens
      • systemMsgAsFirstUserMsg

        protected boolean systemMsgAsFirstUserMsg
    • Constructor Detail

      • OpenAIChatBuilderImpl

        public OpenAIChatBuilderImpl()
    • Method Detail

      • url

        public AIChatBuilder url​(String url)
        Description copied from interface: AIChatBuilder
        Sets the URL for the chat completion request.
        Specified by:
        url in interface AIChatBuilder
        Parameters:
        url - The URL.
        Returns:
        The builder instance for chaining.
      • key

        public AIChatBuilder key​(String key)
        Description copied from interface: AIChatBuilder
        Sets the API key for the chat completion request.
        Specified by:
        key in interface AIChatBuilder
        Parameters:
        key - The API key.
        Returns:
        The builder instance for chaining.
      • organizationId

        public AIChatBuilder organizationId​(String organizationId)
        Description copied from interface: AIChatBuilder
        Sets the organization ID for the chat completion request - if applicable (OpenAI).
        Specified by:
        organizationId in interface AIChatBuilder
        Parameters:
        organizationId - The organization ID.
        Returns:
        The builder instance for chaining.
      • maxTokens

        public AIChatBuilder maxTokens​(int maxTokens)
        Description copied from interface: AIChatBuilder
        Sets the maximum number of tokens the completion can use.
        Specified by:
        maxTokens in interface AIChatBuilder
        Parameters:
        maxTokens - The maximum number of tokens.
        Returns:
        The builder instance for chaining.
      • model

        public AIChatBuilder model​(String model)
        Description copied from interface: AIChatBuilder
        Sets the AI model to be used for the chat completion.
        Specified by:
        model in interface AIChatBuilder
        Parameters:
        model - The model name.
        Returns:
        The builder instance for chaining.
      • systemMsg

        public AIChatBuilder systemMsg​(String text)
        Description copied from interface: AIChatBuilder
        Adds a system message to the chat.
        Specified by:
        systemMsg in interface AIChatBuilder
        Parameters:
        text - The text of the message.
        Returns:
        The builder instance for chaining.
      • systemMsgAsFirstUserMsg

        public AIChatBuilder systemMsgAsFirstUserMsg()
        Description copied from interface: AIChatBuilder
        Put the system message as a first user message instead. For models like OpenAI o1-preview a system message is supported by the API but not the model, so this is a workaround for those.
        Specified by:
        systemMsgAsFirstUserMsg in interface AIChatBuilder
      • userMsg

        public AIChatBuilder userMsg​(String text)
        Description copied from interface: AIChatBuilder
        Adds a user message to the chat.
        Specified by:
        userMsg in interface AIChatBuilder
        Parameters:
        text - The text of the message.
        Returns:
        The builder instance for chaining.
      • assistantMsg

        public AIChatBuilder assistantMsg​(String text)
        Description copied from interface: AIChatBuilder
        Adds an assistant message to the chat.
        Specified by:
        assistantMsg in interface AIChatBuilder
        Parameters:
        text - The text of the message.
        Returns:
        The builder instance for chaining.
      • determineApiKey

        protected String determineApiKey()
        This returns apiKey if given, otherwise resorts to environment variables depending on the url
      • isClaude

        protected boolean isClaude()
      • isOpenAI

        protected boolean isOpenAI()
      • execute

        public String execute()
        Description copied from interface: AIChatBuilder
        Executes the chat completion request and returns the response.
        Specified by:
        execute in interface AIChatBuilder
        Returns:
        The chat completion response.
      • toJson

        public String toJson()
        Description copied from interface: AIChatBuilder
        Converts the chat completion request to a JSON string.
        Specified by:
        toJson in interface AIChatBuilder
        Returns:
        The JSON string.
      • extractResponse

        protected String extractResponse​(String json)