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.SlingAllMethodsServlet
Servlet 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.BundleContext
bundleContext
protected AIConfigurationService
configurationService
protected com.composum.ai.backend.base.service.chat.GPTContentCreationService
contentCreationService
protected com.google.gson.Gson
gson
protected ApproximateMarkdownService
markdownService
static String
PARAMETER_CHAT
Parameter to transmit additional chat after the first promptPARAMETER_PROMPT
.static String
PARAMETER_CONFIGBASEPATH
Parameter containing the path of the page, for determining the configuration.static String
PARAMETER_INPUT_IMAGE_PATH
Parameter to transmit a path to an image instead of a text.static String
PARAMETER_MAXTOKENS
Optional numerical parameter limiting the number of tokens (about 3/4 english word on average) to be generated.static String
PARAMETER_PROMPT
Parameter to transmit a prompt on which ChatGPT is to operate - that is, the instructions.static String
PARAMETER_RICHTEXT
Optional boolean parameter that indicates the inputText and response are in HTML, not Markdown.static String
PARAMETER_SOURCE
Parameter to transmit a text on which ChatGPT is to operate - not as instructions but as data.static 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.static String
PARAMETER_STREAMID
The ID of the stream.static String
PARAMETER_TEXTLENGTH
Description of intended response (generated text) length, optionally including maximum number of tokens, as e.g.static String
SESSIONKEY_STREAMING
Session 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 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 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 com.composum.ai.backend.base.service.chat.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.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 keySESSIONKEY_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 classorg.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.
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 classorg.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
-
-