Class AICreateServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- org.apache.sling.api.servlets.SlingSafeMethodsServlet
-
- org.apache.sling.api.servlets.SlingAllMethodsServlet
-
- com.composum.ai.backend.slingbase.AICreateServlet
-
- All Implemented Interfaces:
Serializable,javax.servlet.Servlet,javax.servlet.ServletConfig
public class AICreateServlet extends org.apache.sling.api.servlets.SlingAllMethodsServletServlet providing the various services from the backend as servlet, which are useable for the authors.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected org.osgi.framework.BundleContextbundleContextprotected AIConfigurationServiceconfigurationServiceprotected GPTContentCreationServicecontentCreationServiceprotected com.google.gson.Gsongsonprotected ApproximateMarkdownServicemarkdownServicestatic StringPARAMETER_CHATParameter to transmit additional chat after the first promptPARAMETER_PROMPT.static StringPARAMETER_CONFIGBASEPATHParameter containing the path of the page, for determining the configuration.static StringPARAMETER_INPUT_IMAGE_PATHParameter to transmit a path to an image instead of a text.static StringPARAMETER_MAXTOKENSOptional numerical parameter limiting the number of tokens (about 3/4 english word on average) to be generated.static StringPARAMETER_PROMPTParameter to transmit a prompt on which ChatGPT is to operate - that is, the instructions.static StringPARAMETER_RICHTEXTOptional boolean parameter that indicates the inputText and response are in HTML, not Markdown.static StringPARAMETER_SOURCEParameter to transmit a text on which ChatGPT is to operate - not as instructions but as data.static StringPARAMETER_SOURCEPATHParameter with a JCR path that is used as input text on which ChatGPT is to operate - not as instructions but as data.static StringPARAMETER_STREAMIDThe ID of the stream.static StringPARAMETER_TEXTLENGTHDescription of intended response (generated text) length, optionally including maximum number of tokens, as e.g.static StringSESSIONKEY_STREAMINGSession contains a map at this key that maps the streamids to the streaming handle.protected List<AITool>tools
-
Constructor Summary
Constructors Constructor Description AICreateServlet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivate(org.osgi.framework.BundleContext bundleContext)protected voidaddTool(@NotNull AITool tool)protected List<GPTTool>collectTools(org.apache.sling.api.resource.Resource resource, org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response)protected voiddoGet(@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 voiddoPost(@NotNull org.apache.sling.api.SlingHttpServletRequest request, @NotNull org.apache.sling.api.SlingHttpServletResponse response)Implements the content creation operation.protected StringgetMandatoryParameter(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, String parameterName)protected IntegergetOptionalInt(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, String parameterName)protected GPTChatRequestmakeAdditionalParameters(int maxtokens, String chat, javax.servlet.http.HttpServletResponse response, GPTConfiguration config)protected voidremoveTool(@NotNull AITool tool)protected EventStreamretrieveStream(String streamId, org.apache.sling.api.SlingHttpServletRequest request)protected StringsaveStream(EventStream stream, org.apache.sling.api.SlingHttpServletRequest request)Saves stream for streaming responses into session, to be retrieved withretrieveStream(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
-
-
-
-
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 promptPARAMETER_PROMPT. Format: array of serializedGPTChatMessage. 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 inPARAMETER_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_STREAMID
public static final String PARAMETER_STREAMID
The ID of the stream.- See Also:
- Constant Field Values
-
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 GPTContentCreationService contentCreationService
-
markdownService
protected ApproximateMarkdownService markdownService
-
configurationService
protected AIConfigurationService configurationService
-
bundleContext
protected org.osgi.framework.BundleContext bundleContext
-
gson
protected com.google.gson.Gson gson
-
-
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)
-
saveStream
protected String saveStream(EventStream stream, org.apache.sling.api.SlingHttpServletRequest request)
Saves stream for streaming responses into session, to be retrieved withretrieveStream(String, SlingHttpServletRequest).
-
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.ServletExceptionReturns 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 keySESSIONKEY_STREAMINGand 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:
doGetin classorg.apache.sling.api.servlets.SlingSafeMethodsServlet- Throws:
IOExceptionjavax.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, IOExceptionImplements 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
GPTChatMessagearray - 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.
doGet(SlingHttpServletRequest, SlingHttpServletResponse)to deliver the result as event stream. Otherwise, it'll normally be an HTTP 400 with an error message.- Overrides:
doPostin classorg.apache.sling.api.servlets.SlingAllMethodsServlet- Throws:
javax.servlet.ServletExceptionIOException
-
makeAdditionalParameters
@Nonnull protected GPTChatRequest makeAdditionalParameters(int maxtokens, String chat, javax.servlet.http.HttpServletResponse response, GPTConfiguration config) throws IOException
- Throws:
IOException
-
-