Class AIEditor

java.lang.Object
io.keikai.doc.api.editor.Editor
io.keikai.doc.ai.api.editor.AIEditor

public class AIEditor extends io.keikai.doc.api.editor.Editor
An AI editor that can chat with a language model to generate responses.
Author:
jumperchen
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The type of prompt to generate.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Accepts the AI chat, if any.
    void
    Adds a prompt template.
    void
    Reloads the chat with the default prompt template to continue writing.
    void
    Discards the AI chat, if any.
    static AIEditor
    getInstance(io.keikai.doc.api.DocumentModel model, dev.langchain4j.model.chat.StreamingChatLanguageModel chatModel)
    Returns an AI editor for the specified document model.
    Returns the prompt template for the specified key.
    protected void
    Initializes the prompt templates.
    void
    reloadChat(Function<AIPromptTemplate,AIPromptTemplate> promptTemplateFunction, Consumer<String> responseConsumer)
    Reloads the chat with the specified prompt template.
    Removes the prompt template for the specified key.

    Methods inherited from class io.keikai.doc.api.editor.Editor

    clearRevisionHistory, execute, getComponents, getComponentsByAttr, getCurrentRevisionIndex, getDirectComponents, getDirectParagraphs, getDirectTables, getInReadLock, getInstance, getLists, getModel, getParagraphs, getParagraphsByAttr, getRevisionHistory, getSections, getSelection, getTables, getTablesByAttr, loadJSON, loadJSON, loadJSON, loadJSON, loadRevision, redo, removeSelection, runInWriteLock, runInWriteLock, toJSON, undo

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • initPromptTemplate

      protected void initPromptTemplate()
      Initializes the prompt templates.
    • addPromptTemplate

      public void addPromptTemplate(String key, AIPromptTemplate template)
      Adds a prompt template.
      Parameters:
      key - Enum.name()
      template - the prompt template
    • getPromptTemplate

      public AIPromptTemplate getPromptTemplate(String key)
      Returns the prompt template for the specified key.
      Parameters:
      key - Enum.name()
    • removePromptTemplate

      public AIPromptTemplate removePromptTemplate(String key)
      Removes the prompt template for the specified key.
      Parameters:
      key - Enum.name()
    • getInstance

      public static AIEditor getInstance(io.keikai.doc.api.DocumentModel model, dev.langchain4j.model.chat.StreamingChatLanguageModel chatModel)
      Returns an AI editor for the specified document model.
      Parameters:
      model - the document model to edit or query
      chatModel - the chat model to use
      Returns:
      an editor for the specified document model
    • acceptChat

      public void acceptChat()
      Accepts the AI chat, if any.
    • discardChat

      public void discardChat()
      Discards the AI chat, if any.
    • reloadChat

      public void reloadChat(Function<AIPromptTemplate,AIPromptTemplate> promptTemplateFunction, Consumer<String> responseConsumer)
      Reloads the chat with the specified prompt template.

      To modify the prompt template, use the promptTemplateFunction function to modify the prompt template.
      For example,

       
       reloadChat(template -> {
       	template.setUserMessage(template.getUserMessage().replace("{prompt}",
       	"Continue writing AFTER <Block> ONLY ONE SENTENCE. DONT REPEAT THE TEXT."));
       	return template;
       }, response -> {
           execute(Commands.text(response));
       });
       

      Parameters:
      promptTemplateFunction - a function to modify the prompt template
      responseConsumer - a consumer to consume the response
    • continueWrite

      public void continueWrite()
      Reloads the chat with the default prompt template to continue writing.
      See Also: