Package com.composum.ai.composum.bundle
Class AIServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- org.apache.sling.api.servlets.SlingSafeMethodsServlet
-
- org.apache.sling.api.servlets.SlingAllMethodsServlet
-
- com.composum.sling.core.servlet.AbstractServiceServlet
-
- com.composum.ai.composum.bundle.AIServlet
-
- All Implemented Interfaces:
com.composum.sling.core.service.RestrictedService
,Serializable
,javax.servlet.Servlet
,javax.servlet.ServletConfig
public class AIServlet extends com.composum.sling.core.servlet.AbstractServiceServlet
Servlet providing the various services from the backend as servlet, which are useable for the authors.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AIServlet.AbstractGPTServletOperation
class
AIServlet.CreateOperation
Implements the content creation operation.class
AIServlet.DescriptionOperation
Servlet representation ofGPTContentCreationService.generateDescription(String, int, GPTConfiguration)
with arguments text and maxwords.static class
AIServlet.Extension
class
AIServlet.KeywordsOperation
Servlet representation ofGPTContentCreationService.generateKeywords(String, GPTConfiguration)
with argument text.static class
AIServlet.Operation
class
AIServlet.PromptOnTextOperation
Servlet representation ofGPTContentCreationService.executePromptOnText(String, String, GPTChatRequest)
with arguments prompt, text and maxwords.class
AIServlet.PromptOperation
Servlet representation ofGPTContentCreationService.executePrompt(String, GPTChatRequest)
with arguments prompt and maxwords.class
AIServlet.StreamResponseOperation
Returns an event stream that was prepared by a previous operation with parameterPARAMETER_STREAMING
set.class
AIServlet.TranslateOperation
Servlet representation ofGPTTranslationService
, specificallyGPTTranslationService.singleTranslation(String, String, String, GPTConfiguration)
and the streaming version, with arguments text, sourceLanguage, targetLanguage, "richText", pagePath .
-
Field Summary
Fields Modifier and Type Field Description protected org.osgi.framework.BundleContext
bundleContext
protected com.composum.ai.backend.slingbase.AIConfigurationService
configurationService
protected com.composum.ai.backend.base.service.chat.GPTContentCreationService
contentCreationService
protected com.google.gson.Gson
gson
protected com.composum.ai.backend.slingbase.ApproximateMarkdownService
markdownService
protected com.composum.sling.core.servlet.ServletOperationSet<AIServlet.Extension,AIServlet.Operation>
operations
static String
PARAMETER_CHAT
Parameter to transmit additional chat afterPARAMETER_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_MAXWORDS
Optional numerical parameter limiting the number of words to be generated.static String
PARAMETER_PATH
The path to a resource, given as parameter.static String
PARAMETER_PROMPT
Parameter to transmit a prompt on which ChatGPT is to operate - that is, the instructions.static String
PARAMETER_PROPERTY
Property name, given as parameter.static String
PARAMETER_RICHTEXT
static String
PARAMETER_STREAMID
Transmits the ID of the stream toAIServlet.StreamResponseOperation
.static String
PARAMETER_STREAMING
If set to true for operations that support it, the actual response can be streamed withAIServlet.StreamResponseOperation
in a followup GET request.static String
PARAMETER_TEXT
Parameter to transmit a text on which ChatGPT is to operate - not as instructions but as data.static String
RESULTKEY
Key forStatus.data(String)
- toplevel key in the servlet result.static String
RESULTKEY_DESCRIPTION
Key in the result that transmits the generated description.static String
RESULTKEY_FINISHREASON
Used to transmit whether the response was complete (finishreasonGPTFinishReason.STOP
or length restrictionGPTFinishReason.LENGTH
).static String
RESULTKEY_KEYWORDS
Key in the result that transmits the generated list of keywords.static String
RESULTKEY_STREAMID
Alternative toRESULTKEY_TEXT
when the response will be streamed.static String
RESULTKEY_TEXT
Key in the result that transmits the generated text.static String
RESULTKEY_TRANSLATION
Key in the result that transmits a list of translations (currently only one, but might be extended later.)static String
SERVICE_KEY
static String
SESSIONKEY_STREAMING
Session contains a map at this key that maps the streamids to the streaming handle.protected com.composum.ai.backend.base.service.chat.GPTTranslationService
translationService
-
Fields inherited from class com.composum.sling.core.servlet.AbstractServiceServlet
DATE_FORMAT, PARAM_BEFORE, PARAM_CMD, PARAM_FILE, PARAM_FILTER, PARAM_ID, PARAM_INDEX, PARAM_JCR_CONTENT, PARAM_LABEL, PARAM_MIME_TYPE, PARAM_NAME, PARAM_PATH, PARAM_QUERY, PARAM_RESOURCE_TYPE, PARAM_TITLE, PARAM_TYPE, PARAM_URL, PARAM_VALUE, PARAM_VERSION
-
-
Constructor Summary
Constructors Constructor Description AIServlet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activate(org.osgi.framework.BundleContext bundleContext)
protected com.composum.sling.core.servlet.ServletOperationSet<AIServlet.Extension,AIServlet.Operation>
getOperations()
void
init()
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 requests into session, to be retrieved withretrieveStream(String, SlingHttpServletRequest)
during aAIServlet.StreamResponseOperation
.-
Methods inherited from class com.composum.sling.core.servlet.AbstractServiceServlet
deactivate, doDelete, doGet, doPost, doPut, getJsonObject, getJsonObject, getJsonObject, getPath, getResource, getRestrictions, getServiceKey, isEnabled, jsonAnswerItemExists, jsonValue, methodDeletePermission, methodGetPermission, methodPostPermission, methodPutPermission, setNoCacheHeaders, tryToUseRawSuffix
-
Methods inherited from class org.apache.sling.api.servlets.SlingAllMethodsServlet
getAllowedRequestMethods, isMethodValid, mayService
-
Methods inherited from class org.apache.sling.api.servlets.SlingSafeMethodsServlet
doGeneric, doHead, doOptions, doTrace, getServletInfo, handleMethodNotImplemented, service, service
-
-
-
-
Field Detail
-
SERVICE_KEY
public static final String SERVICE_KEY
- See Also:
- Constant Field Values
-
PARAMETER_TEXT
public static final String PARAMETER_TEXT
Parameter to transmit a text on which ChatGPT is to operate - not as instructions but as data.- 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
-
PARAMETER_PROMPT
public static final String PARAMETER_PROMPT
Parameter to transmit a prompt on which ChatGPT is to operate - that is, the instructions. If there is aPARAMETER_CHAT
given, this is the first prompt *before* the chat - the last message of the chat is the last prompt.- See Also:
- Constant Field Values
-
PARAMETER_CHAT
public static final String PARAMETER_CHAT
Parameter to transmit additional chat afterPARAMETER_PROMPT
. The last message of the chat is the last prompt. Format: array of serializedGPTChatMessage
. E.g.[{"role":"assistant","content":"Answer 1"},{"role":"user","content":"Another question"}]
.- See Also:
- Constant Field Values
-
PARAMETER_MAXWORDS
public static final String PARAMETER_MAXWORDS
Optional numerical parameter limiting the number of words to be generated. That might lead to cutoff or actual wordcount, depending on the operation, and is usually only quite approximate.- 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.- See Also:
- Constant Field Values
-
PARAMETER_PATH
public static final String PARAMETER_PATH
The path to a resource, given as parameter.- See Also:
- Constant Field Values
-
PARAMETER_PROPERTY
public static final String PARAMETER_PROPERTY
Property name, given as parameter.- See Also:
- Constant Field Values
-
PARAMETER_RICHTEXT
public static final String PARAMETER_RICHTEXT
- See Also:
- Constant Field Values
-
RESULTKEY
public static final String RESULTKEY
Key forStatus.data(String)
- toplevel key in the servlet result.- See Also:
- Constant Field Values
-
RESULTKEY_DESCRIPTION
public static final String RESULTKEY_DESCRIPTION
Key in the result that transmits the generated description.- See Also:
- Constant Field Values
-
RESULTKEY_KEYWORDS
public static final String RESULTKEY_KEYWORDS
Key in the result that transmits the generated list of keywords.- See Also:
- Constant Field Values
-
RESULTKEY_TEXT
public static final String RESULTKEY_TEXT
Key in the result that transmits the generated text.- See Also:
- Constant Field Values
-
RESULTKEY_TRANSLATION
public static final String RESULTKEY_TRANSLATION
Key in the result that transmits a list of translations (currently only one, but might be extended later.)- See Also:
- Constant Field Values
-
RESULTKEY_FINISHREASON
public static final String RESULTKEY_FINISHREASON
Used to transmit whether the response was complete (finishreasonGPTFinishReason.STOP
or length restrictionGPTFinishReason.LENGTH
). Lowercase String.- See Also:
- Constant Field Values
-
PARAMETER_STREAMING
public static final String PARAMETER_STREAMING
If set to true for operations that support it, the actual response can be streamed withAIServlet.StreamResponseOperation
in a followup GET request.- See Also:
- Constant Field Values
-
PARAMETER_STREAMID
public static final String PARAMETER_STREAMID
Transmits the ID of the stream toAIServlet.StreamResponseOperation
.- See Also:
- Constant Field Values
-
RESULTKEY_STREAMID
public static final String RESULTKEY_STREAMID
Alternative toRESULTKEY_TEXT
when the response will be streamed.- 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
-
SESSIONKEY_STREAMING
public static final String SESSIONKEY_STREAMING
Session contains a map at this key that maps the streamids to the streaming handle.
-
translationService
protected com.composum.ai.backend.base.service.chat.GPTTranslationService translationService
-
contentCreationService
protected com.composum.ai.backend.base.service.chat.GPTContentCreationService contentCreationService
-
markdownService
protected com.composum.ai.backend.slingbase.ApproximateMarkdownService markdownService
-
configurationService
protected com.composum.ai.backend.slingbase.AIConfigurationService configurationService
-
bundleContext
protected org.osgi.framework.BundleContext bundleContext
-
gson
protected com.google.gson.Gson gson
-
operations
protected final com.composum.sling.core.servlet.ServletOperationSet<AIServlet.Extension,AIServlet.Operation> operations
-
-
Method Detail
-
getOperations
protected com.composum.sling.core.servlet.ServletOperationSet<AIServlet.Extension,AIServlet.Operation> getOperations()
- Specified by:
getOperations
in classcom.composum.sling.core.servlet.AbstractServiceServlet
-
init
public void init() throws javax.servlet.ServletException
- Overrides:
init
in classjavax.servlet.GenericServlet
- Throws:
javax.servlet.ServletException
-
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 requests into session, to be retrieved withretrieveStream(String, SlingHttpServletRequest)
during aAIServlet.StreamResponseOperation
.
-
retrieveStream
protected EventStream retrieveStream(String streamId, org.apache.sling.api.SlingHttpServletRequest request)
-
-