Class AICreateServlet

  • All Implemented Interfaces:
    Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public class AICreateServlet
    extends org.apache.sling.api.servlets.SlingAllMethodsServlet
    Servlet providing the various services from the backend as servlet, which are useable for the authors.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      AICreateServlet()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activate​(org.osgi.framework.BundleContext bundleContext)  
      protected void addTool​(@NotNull AITool tool)  
      protected List<com.composum.ai.backend.base.service.chat.GPTTool> collectTools​(org.apache.sling.api.resource.Resource resource, org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response)  
      protected void doGet​(@NotNull org.apache.sling.api.SlingHttpServletRequest request, @NotNull org.apache.sling.api.SlingHttpServletResponse response)
      Returns an event stream that was prepared by a previous operation, as a second request after a POST request returning a 202 with a 'Location' header to this servlet, since only GET requests are supported by the EventStream class in browser.
      protected void doPost​(@NotNull org.apache.sling.api.SlingHttpServletRequest request, @NotNull org.apache.sling.api.SlingHttpServletResponse response)
      Implements the content creation operation.
      protected String getMandatoryParameter​(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, String parameterName)  
      protected Integer getOptionalInt​(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, String parameterName)  
      protected com.composum.ai.backend.base.service.chat.GPTChatRequest makeAdditionalParameters​(int maxtokens, String chat, javax.servlet.http.HttpServletResponse response, com.composum.ai.backend.base.service.chat.GPTConfiguration config)  
      protected void removeTool​(@NotNull AITool tool)  
      protected EventStream retrieveStream​(String streamId, org.apache.sling.api.SlingHttpServletRequest request)  
      protected String saveStream​(EventStream stream, org.apache.sling.api.SlingHttpServletRequest request)
      Saves stream for streaming responses into session, to be retrieved with retrieveStream(String, SlingHttpServletRequest).
      • Methods inherited from class org.apache.sling.api.servlets.SlingAllMethodsServlet

        doDelete, doPut, getAllowedRequestMethods, isMethodValid, mayService
      • Methods inherited from class org.apache.sling.api.servlets.SlingSafeMethodsServlet

        doGeneric, doHead, doOptions, doTrace, getServletInfo, handleMethodNotImplemented, service, service
      • Methods inherited from class javax.servlet.GenericServlet

        destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, init, log, log
    • Field Detail

      • PARAMETER_SOURCE

        public static final String PARAMETER_SOURCE
        Parameter to transmit a text on which ChatGPT is to operate - not as instructions but as data.
        See Also:
        Constant Field Values
      • PARAMETER_SOURCEPATH

        public static final String PARAMETER_SOURCEPATH
        Parameter with a JCR path that is used as input text on which ChatGPT is to operate - not as instructions but as data.
        See Also:
        Constant Field Values
      • PARAMETER_PROMPT

        public static final String PARAMETER_PROMPT
        Parameter to transmit a prompt on which ChatGPT is to operate - that is, the instructions.
        See Also:
        Constant Field Values
      • PARAMETER_CHAT

        public static final String PARAMETER_CHAT
        Parameter to transmit additional chat after the first prompt PARAMETER_PROMPT. Format: array of serialized GPTChatMessage. E.g. [{"role":"assistant","content":"That's good."}, {"role":"user","content":"Why exactly?"}, ].
        See Also:
        Constant Field Values
      • PARAMETER_MAXTOKENS

        public static final String PARAMETER_MAXTOKENS
        Optional numerical parameter limiting the number of tokens (about 3/4 english word on average) to be generated. That might lead to cutoff, as this is a hard limit and ChatGPT doesn't know about that during generation. So it's advisable to specify the desired text length in the prompt, too. - Note there is an alternative in PARAMETER_TEXTLENGTH.
        See Also:
        Constant Field Values
      • PARAMETER_RICHTEXT

        public static final String PARAMETER_RICHTEXT
        Optional boolean parameter that indicates the inputText and response are in HTML, not Markdown.
        See Also:
        Constant Field Values
      • PARAMETER_TEXTLENGTH

        public static final String PARAMETER_TEXTLENGTH
        Description of intended response (generated text) length, optionally including maximum number of tokens, as e.g. in "1000|Several paragraphs of text".
        See Also:
        Constant Field Values
      • PARAMETER_INPUT_IMAGE_PATH

        public static final String PARAMETER_INPUT_IMAGE_PATH
        Parameter to transmit a path to an image instead of a text.
        See Also:
        Constant Field Values
      • SESSIONKEY_STREAMING

        public static final String SESSIONKEY_STREAMING
        Session contains a map at this key that maps the streamids to the streaming handle.
      • PARAMETER_CONFIGBASEPATH

        public static final String PARAMETER_CONFIGBASEPATH
        Parameter containing the path of the page, for determining the configuration.
        See Also:
        Constant Field Values
      • contentCreationService

        protected com.composum.ai.backend.base.service.chat.GPTContentCreationService contentCreationService
      • bundleContext

        protected org.osgi.framework.BundleContext bundleContext
      • gson

        protected com.google.gson.Gson gson
    • Constructor Detail

      • AICreateServlet

        public AICreateServlet()
    • Method Detail

      • addTool

        protected void addTool​(@NotNull
                               @NotNull AITool tool)
      • removeTool

        protected void removeTool​(@NotNull
                                  @NotNull AITool tool)
      • activate

        public void activate​(org.osgi.framework.BundleContext bundleContext)
      • retrieveStream

        protected EventStream retrieveStream​(String streamId,
                                             org.apache.sling.api.SlingHttpServletRequest request)
      • doGet

        protected void doGet​(@NotNull
                             @NotNull org.apache.sling.api.SlingHttpServletRequest request,
                             @NotNull
                             @NotNull org.apache.sling.api.SlingHttpServletResponse response)
                      throws IOException,
                             javax.servlet.ServletException
        Returns an event stream that was prepared by a previous operation, as a second request after a POST request returning a 202 with a 'Location' header to this servlet, since only GET requests are supported by the EventStream class in browser. The event stream is stored in the session under the key SESSIONKEY_STREAMING and is removed after the request.

        In the event stream the generated response is put into 'data' . When the creation is finished, we create an event event 'finished' into the stream with data JSON like this: {"success":true,"data":{"result":{"finishreason":"STOP"}}} In case of errors, there will be an 'exception' event into the stream with data JSON like this: {"success":false,"title":"Internal error","messages":[{"level":"error","text":"something happened"}]}

        Overrides:
        doGet in class org.apache.sling.api.servlets.SlingSafeMethodsServlet
        Throws:
        IOException
        javax.servlet.ServletException
      • getOptionalInt

        protected Integer getOptionalInt​(org.apache.sling.api.SlingHttpServletRequest request,
                                         org.apache.sling.api.SlingHttpServletResponse response,
                                         String parameterName)
                                  throws IOException
        Throws:
        IOException
      • getMandatoryParameter

        protected String getMandatoryParameter​(org.apache.sling.api.SlingHttpServletRequest request,
                                               org.apache.sling.api.SlingHttpServletResponse response,
                                               String parameterName)
                                        throws IOException
        Throws:
        IOException
      • doPost

        protected void doPost​(@NotNull
                              @NotNull org.apache.sling.api.SlingHttpServletRequest request,
                              @NotNull
                              @NotNull org.apache.sling.api.SlingHttpServletResponse response)
                       throws javax.servlet.ServletException,
                              IOException
        Implements the content creation operation. Input parameters are:
        • prompt: the prompt to execute
        • textLength: the maximum length of the generated text. If it starts with a number and a | then the number is used as maxwords parameter, limiting the number of tokens, and the rest is transmitted in the prompt to ChatGPT.
        • inputPath: if a path is given, the markdown generated by the path is used as input
        • inputText: alternatively to the path, this text is used as input
        • chat: additional chat history before the prompt, as serialized GPTChatMessage array
        • richText: if true, the inputText and response are in HTML, not Markdown
        • maxtokens: optional numerical parameter limiting the number of tokens (about 3/4 english word on average) to be generated. That might lead to cutoff, as this is a hard limit and ChatGPT doesn't know about that during generation. So it's advisable to specify the desired text length in the prompt, too.
        A successful response will return an HTTP 200 with a JSON map with a "streamid" with an streamid to access the response stream, which can be used with doGet(SlingHttpServletRequest, SlingHttpServletResponse) to deliver the result as event stream. Otherwise, it'll normally be an HTTP 400 with an error message.
        Overrides:
        doPost in class org.apache.sling.api.servlets.SlingAllMethodsServlet
        Throws:
        javax.servlet.ServletException
        IOException
      • makeAdditionalParameters

        @Nonnull
        protected com.composum.ai.backend.base.service.chat.GPTChatRequest makeAdditionalParameters​(int maxtokens,
                                                                                                    String chat,
                                                                                                    javax.servlet.http.HttpServletResponse response,
                                                                                                    com.composum.ai.backend.base.service.chat.GPTConfiguration config)
                                                                                             throws IOException
        Throws:
        IOException
      • collectTools

        @Nullable
        protected List<com.composum.ai.backend.base.service.chat.GPTTool> collectTools​(@Nonnull
                                                                                       org.apache.sling.api.resource.Resource resource,
                                                                                       @Nonnull
                                                                                       org.apache.sling.api.SlingHttpServletRequest request,
                                                                                       @Nonnull
                                                                                       org.apache.sling.api.SlingHttpServletResponse response)