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 onGPTChatCompletionService
this implements generating keywords.
-
-
Field Summary
Fields Modifier and Type Field Description protected GPTChatCompletionService
chatCompletionService
protected static 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.static String
PLACEHOLDER_TEXT
static String
PLACEHOLDER_WORDCOUNTLIMIT
static String
TEMPLATE_CHATPROMPTONTEXT
static String
TEMPLATE_MAKEDESCRIPTION
static String
TEMPLATE_MAKEKEYWORDS
Template forGPTChatMessagesTemplate
to generate keywords from a text.static String
TEMPLATE_PROMPTONTEXT
-
Constructor Summary
Constructors Constructor Description GPTContentCreationServiceImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
executePrompt(String prompt, GPTChatRequest additionalParameters)
Executes a given prompt from the user using ChatGPT.String
executePromptOnText(String prompt, String text, GPTChatRequest additionalParameters)
Executes a given prompt from the user using ChatGPT, using the given text as context.void
executePromptOnTextStreaming(String prompt, String text, GPTChatRequest additionalParameters, GPTCompletionCallback callback)
Executes a given prompt from the user using ChatGPT, using the given text as context.void
executePromptStreaming(String prompt, GPTChatRequest additionalParameters, GPTCompletionCallback callback)
Executes a given prompt from the user using ChatGPT.String
generateDescription(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 GPTChatRequest
makeExecuteOnTextRequest(String prompt, String text, GPTChatRequest additionalParameters)
protected GPTChatRequest
makeExecutePromptRequest(String prompt, GPTChatRequest additionalParameters)
-
-
-
Field Detail
-
TEMPLATE_MAKEKEYWORDS
public static final String TEMPLATE_MAKEKEYWORDS
Template forGPTChatMessagesTemplate
to 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:GPTContentCreationService
Generates a list of keywords from the given text.- Specified by:
generateKeywords
in 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:GPTContentCreationService
Generates a description from the given text.- Specified by:
generateDescription
in 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:GPTContentCreationService
Executes a given prompt from the user using ChatGPT.- Specified by:
executePrompt
in 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:GPTContentCreationService
Executes a given prompt from the user using ChatGPT.- Specified by:
executePromptStreaming
in 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:GPTContentCreationService
Executes a given prompt from the user using ChatGPT, using the given text as context.- Specified by:
executePromptOnText
in 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:GPTContentCreationService
Executes a given prompt from the user using ChatGPT, using the given text as context.- Specified by:
executePromptOnTextStreaming
in 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
-
-