Interface GPTChatCompletionService

  • All Known Implementing Classes:
    GPTChatCompletionServiceImpl

    public interface GPTChatCompletionService
    Raw abstraction of the ChatGPT chat interface, with only the details that are needed.

    This does deliberately not use the OpenAI API classes because we want to be able to switch to a different API implementation, and hide their complexity from the rest of the code. If we need special parameters, we will add new methods with more specific function.

    • Method Detail

      • streamingChatCompletion

        void streamingChatCompletion​(@Nonnull
                                     GPTChatRequest request,
                                     @Nonnull
                                     GPTCompletionCallback callback)
                              throws GPTException
        Give some messages and receive the streaming response via callback, to reduce waiting time. It possibly waits if a rate limit is reached, but otherwise returns immediately after scheduling an asynchronous call.
        Throws:
        GPTException
      • getTemplate

        @Nonnull
        GPTChatMessagesTemplate getTemplate​(@Nonnull
                                            String templateName)
                                     throws GPTException
        Retrieves a (usually cached) chat template with that name. Mostly for backend internal use. The templates are retrieved from the bundle resources at "chattemplates/", and are cached.
        Parameters:
        templateName - the name of the template to retrieve, e.g. "singleTranslation" .
        Throws:
        GPTException
      • shorten

        @Nonnull
        String shorten​(@Nullable
                       String text,
                       int maxTokens)
                throws GPTException
        Helper method to shorten texts by taking out the middle if too long. In texts longer than this many tokens we replace the middle with " ... (truncated) ... " since ChatGPT can only process a limited number of words / tokens and in the introduction or summary there is probably the most condensed information about the text. The output has then maxTokens tokens, including the ... marker.
        Parameters:
        text - the text to shorten
        maxTokens - the maximum number of tokens in the output
        Throws:
        GPTException
      • countTokens

        int countTokens​(@Nullable
                        String text)
        Counts the number of tokens for the text for the normally used model. Caution: message boundaries need some tokens and slicing text might create a token or two, too, so do not exactly rely on that.
      • isEnabled

        boolean isEnabled()
        Whether ChatGPT completion is enabled. If not, calling the methods that access ChatGPT throws an IllegalStateException.
      • isEnabled

        boolean isEnabled​(GPTConfiguration gptConfig)
        Checks whether isEnabled() and whether gptConfig enables executing GPT calls. (That is currently whether there is an api key either globally or in the gptConfig).
      • isVisionEnabled

        boolean isVisionEnabled()
        Returns true if vision is enabled.