Class GPTContentCreationServiceImpl
- java.lang.Object
-
- com.composum.ai.backend.base.service.chat.impl.GPTContentCreationServiceImpl
-
- All Implemented Interfaces:
GPTContentCreationService
public class GPTContentCreationServiceImpl extends Object implements GPTContentCreationService
Building onGPTChatCompletionServicethis implements generating keywords.
-
-
Field Summary
Fields Modifier and Type Field Description protected GPTChatCompletionServicechatCompletionServiceprotected static intMAXTOKENSTo respect limits of ChatGPT we replace in texts longer than this many words we replace the middle with [...] TODO: use an intelligent algorithm to determine this limit, but that's pretty hard for executePromptOnText.static StringPLACEHOLDER_TEXTstatic StringPLACEHOLDER_WORDCOUNTLIMITstatic StringTEMPLATE_CHATPROMPTONTEXTstatic StringTEMPLATE_MAKEDESCRIPTIONstatic StringTEMPLATE_MAKEKEYWORDSTemplate forGPTChatMessagesTemplateto generate keywords from a text.static StringTEMPLATE_PROMPTONTEXT
-
Constructor Summary
Constructors Constructor Description GPTContentCreationServiceImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringexecutePrompt(String prompt, GPTChatRequest additionalParameters)Executes a given prompt from the user using ChatGPT.StringexecutePromptOnText(String prompt, String text, GPTChatRequest additionalParameters)Executes a given prompt from the user using ChatGPT, using the given text as context.voidexecutePromptOnTextStreaming(String prompt, String text, GPTChatRequest additionalParameters, GPTCompletionCallback callback)Executes a given prompt from the user using ChatGPT, using the given text as context.voidexecutePromptStreaming(String prompt, GPTChatRequest additionalParameters, GPTCompletionCallback callback)Executes a given prompt from the user using ChatGPT.StringgenerateDescription(String text, int maxwords, GPTConfiguration configuration)Generates a description from the given text.List<String>generateKeywords(String text, GPTConfiguration configuration)Generates a list of keywords from the given text.protected GPTChatRequestmakeExecuteOnTextRequest(String prompt, String text, GPTChatRequest additionalParameters)protected GPTChatRequestmakeExecutePromptRequest(String prompt, GPTChatRequest additionalParameters)
-
-
-
Field Detail
-
TEMPLATE_MAKEKEYWORDS
public static final String TEMPLATE_MAKEKEYWORDS
Template forGPTChatMessagesTemplateto generate keywords from a text. Has placeholder ${text} .- See Also:
- Constant Field Values
-
TEMPLATE_MAKEDESCRIPTION
public static final String TEMPLATE_MAKEDESCRIPTION
- See Also:
- Constant Field Values
-
TEMPLATE_PROMPTONTEXT
public static final String TEMPLATE_PROMPTONTEXT
- See Also:
- Constant Field Values
-
TEMPLATE_CHATPROMPTONTEXT
public static final String TEMPLATE_CHATPROMPTONTEXT
- See Also:
- Constant Field Values
-
PLACEHOLDER_TEXT
public static final String PLACEHOLDER_TEXT
- See Also:
- Constant Field Values
-
PLACEHOLDER_WORDCOUNTLIMIT
public static final String PLACEHOLDER_WORDCOUNTLIMIT
- See Also:
- Constant Field Values
-
MAXTOKENS
protected static final int MAXTOKENS
To respect limits of ChatGPT we replace in texts longer than this many words we replace the middle with [...] TODO: use an intelligent algorithm to determine this limit, but that's pretty hard for executePromptOnText. 3000 would collide with the 1000 token default for maxtokens, so we use 2800.- See Also:
- Constant Field Values
-
chatCompletionService
protected GPTChatCompletionService chatCompletionService
-
-
Method Detail
-
generateKeywords
@Nonnull public List<String> generateKeywords(@Nullable String text, @Nullable GPTConfiguration configuration)
Description copied from interface:GPTContentCreationServiceGenerates a list of keywords from the given text.- Specified by:
generateKeywordsin interfaceGPTContentCreationService- Parameters:
text- The text to generate keywords from.- Returns:
- A list of generated keywords, possibly empty.
-
generateDescription
@Nonnull public String generateDescription(@Nullable String text, int maxwords, @Nullable GPTConfiguration configuration)
Description copied from interface:GPTContentCreationServiceGenerates a description from the given text.- Specified by:
generateDescriptionin interfaceGPTContentCreationService- Parameters:
text- The text to generate a description from.maxwords- approximate maximum number of words in the description, if > 0- Returns:
- A generated description, possibly empty.
-
executePrompt
@Nonnull public String executePrompt(@Nullable String prompt, @Nullable GPTChatRequest additionalParameters)
Description copied from interface:GPTContentCreationServiceExecutes a given prompt from the user using ChatGPT.- Specified by:
executePromptin interfaceGPTContentCreationService- Parameters:
prompt- the prompt text from the user
-
makeExecutePromptRequest
protected GPTChatRequest makeExecutePromptRequest(String prompt, @Nullable GPTChatRequest additionalParameters)
-
executePromptStreaming
public void executePromptStreaming(@Nonnull String prompt, @Nullable GPTChatRequest additionalParameters, @Nonnull GPTCompletionCallback callback) throws GPTException
Description copied from interface:GPTContentCreationServiceExecutes a given prompt from the user using ChatGPT.- Specified by:
executePromptStreamingin interfaceGPTContentCreationService- Parameters:
prompt- the prompt text from the usercallback- the callback to use for streaming the result- Throws:
GPTException
-
executePromptOnText
@Nonnull public String executePromptOnText(@Nullable String prompt, @Nullable String text, @Nullable GPTChatRequest additionalParameters)
Description copied from interface:GPTContentCreationServiceExecutes a given prompt from the user using ChatGPT, using the given text as context.- Specified by:
executePromptOnTextin interfaceGPTContentCreationService- Parameters:
prompt- the prompt text from the usertext- the text to use as context for the prompt
-
makeExecuteOnTextRequest
protected GPTChatRequest makeExecuteOnTextRequest(String prompt, String text, @Nullable GPTChatRequest additionalParameters)
-
executePromptOnTextStreaming
public void executePromptOnTextStreaming(@Nonnull String prompt, @Nonnull String text, @Nullable GPTChatRequest additionalParameters, @Nonnull GPTCompletionCallback callback) throws GPTException
Description copied from interface:GPTContentCreationServiceExecutes a given prompt from the user using ChatGPT, using the given text as context.- Specified by:
executePromptOnTextStreamingin interfaceGPTContentCreationService- Parameters:
prompt- the prompt text from the usertext- the text to use as context for the promptcallback- the callback to use for streaming the result- Throws:
GPTException
-
-