Class GenericOaiPromptServiceHandler

java.lang.Object
de.xima.fc.prompt.ms.impl.service.generic_oai.service.logic.GenericOaiPromptServiceHandler
All Implemented Interfaces:
IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>, PromptServiceHandlerMixin_JsonConfig<GenericOaiConnectionConfig, GenericOaiQueryConfig>

The IPromptServiceHandler implementation for the Generic OAI prompt service.
Since:
8.5.0
  • Method Details

    • computeQueryInput

      Description copied from interface: IPromptServiceHandler
      Given a prompt query, computes the input parameters required to execute the query. The engine will provide the parameters to the executePromptQuery method. The input parameter descriptors are also used for documentation purposes or to generate a user interface for the prompt query workflow action.
      Specified by:
      computeQueryInput in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Parameters:
      params - Parameters to the computation, such as the prompt query.
      Returns:
      The list of descriptors for the input of the query.
    • computeQueryOutput

      Description copied from interface: IPromptServiceHandler
      Given a prompt query, computes the output parameters that will be returned by the executePromptQuery method. The output parameter descriptors are e.g. used for documentation purposes; or by the prompt query workflow action to determine which output parameters are available after executing the prompt query.
      Specified by:
      computeQueryOutput in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Parameters:
      params - Parameters to the computation, such as the prompt query.
      Returns:
      The list of descriptors for the output of the query.
    • executePromptQuery

      Description copied from interface: IPromptServiceHandler
      Executes a prompt query using the given parameters, and returns the result of the query.

      This method is given a value for each parameter as defined by IPromptServiceHandler.computeQueryInput(IPromptComputeQueryInputParams). The prompt engine ensures all required parameters are present. Optional input parameter may be absent.

      This method must return a value for each output parameter as defined by IPromptServiceHandler.computeQueryOutput(IPromptComputeQueryOutputParams). The prompt engine will check that required parameters are present. Optional output parameters may be absent.

      Specified by:
      executePromptQuery in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Parameters:
      params - The parameters for the execution, such as the connection and query configuration, and the values for each input parameter.
      Returns:
      The result of the prompt query execution, with the values for each output parameter.
      Throws:
      PromptExecutionException - If the execution fails due to some reason, e.g. due to a network error or when the quota limit was exceeded. You should throw one of the subclasses of this exception to indicate the appropriate reason of the failure. ExecutionFailedException is the generic fallback if no more specific exception is available.
    • getConnectionConfigClass

      public Class<GenericOaiConnectionConfig> getConnectionConfigClass()
      Description copied from interface: IPromptServiceHandler
      Gets the class of the connection configuration type parameter.

      The default implementation attempts to read the type of the ConnectionConfig type parameter via reflection. If that does not work, or you need to optimize, override this method and return the type directly.

      Specified by:
      getConnectionConfigClass in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
    • getConnectionConfigVersion

      public String getConnectionConfigVersion()
      Description copied from interface: PromptServiceHandlerMixin_JsonConfig
      Gets the version of the connection configuration class. This is saved in the serialized data. Can be used to update the data if the class definition has undergone changes. If you need to update the serialized data, override the updateConnectionConfig method. Defaults to the serialVersionUID static field of the class, if it exists, or "1.0" otherwise.
      Specified by:
      getConnectionConfigVersion in interface PromptServiceHandlerMixin_JsonConfig<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The version of the connection configuration class, as a string.
    • getConnectionViewBeanClass

      public Class<GenericOaiConnectionConfigBean> getConnectionViewBeanClass()
      Description copied from interface: IPromptServiceHandler
      Returns the class of the bean that should be used when editing the properties of a prompt connection. May be null if you do not require any bean or custom logic. When you only wish to access the properties of your configuration, you do have to use a custom bean - the model is available via the expression language variable model. See IPromptServiceHandler.getConnectionViewXhtml() for further details.

      The default returns null, which uses no extra bean. An extra bean may not be required for simple UIs if you only need to access the properties model.

      Specified by:
      getConnectionViewBeanClass in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The class of the bean to use for editing a prompt connection's configuration.
      See Also:
    • getConnectionViewXhtml

      public URL getConnectionViewXhtml()
      Description copied from interface: IPromptServiceHandler
      This method must return the path to the XHTML page for the custom user interface of a prompt connection. Usually the XHTML file is part of the JAR resources of the module or plugin. In this case, you should return a URL to a JAR file resource (jar:file:/...) like so:
      @Override
      public URL getConnectionViewXhtml() {
        return getClass().getResource("/path/to/view.xhtml");
      }
      

      The contents of this XHTML page is included in the backend menu for prompt connections, already inside a form (i.e. do not add an additional h:form as that would result in an error). You should wrap your custom UI in a naming container to ensure unique IDs that do not clash with other prompt services. Implementations may (but need not) use the components provided by the http://www.xima.de/taglib/xfc/prompt tag library. The following is a recommended template on which you may base your UI:

      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:xp="http://www.xima.de/taglib/xfc/prompt">
      
        <xp:ui id="CHANGE_THIS_TO_A_UNIQUE_ID">
      
          <!-- Custom section with editors specific to your workflow element -->
          <xp:fieldset legend="#{msg['MyAwesomePromptService.connection.fs.base']}">
            <!-- A simple input field for the API key -->
            <xp:apiKey id="apiKey" value="#{model.apiKey}" required="true"/>
         </xp:fieldset>
      
        </xp:ui>
      </ui:composition>
      

      The XHTML page may access the following expression language variables:

      • model: The deserialized configuration of the connection.
      • msg: A virtual ResourceBundle for the localized messages, backed by the message localizer. Empty when that method returns null. For example, if the resource bundle contains the key prompt.label, you can access the localized message via the EL expression msg['prompt.label']
      • bean: An instance of the bean class returned by getConnectionViewBeanClass. Will be null if that method does not define a bean class.
      The XHTML page may use the following additional search expressions (e.g. for the process and update attributes of a <p:ajax />):
      • @promptUi: Resolves to the closest <xp:ui /> component from the http://www.xima.de/taglib/xfc/prompt tag library.
      • @promptFieldset: Resolves to the closest <xp:promptFieldset /> component from the http://www.xima.de/taglib/xfc/prompt tag library.

      To access the values of an enum or the constants defined by a class, consider using:

      <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:o="http://omnifaces.org/ui">
        <o:importConstants type="my.fully.classified.path.MyEnum" var="MyEnum" loader="#{model}"/>
      </ui:composition>
      
      The loader argument is only required for plugins and ensures that the correct class loader is used that knows about the plugin class. Without the loader attribute, the above is equivalent to the PrimeFaces tag handler <p:importConstants />.
      Specified by:
      getConnectionViewXhtml in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      Path to the XHTML view, must noe be null. If you do return null, it will be treated as an error and an appropriate message is displayed to the user informing them that the properties panel could not be loaded.
    • getMessageLocalizer

      public IMessageLocalizer getMessageLocalizer()
      Description copied from interface: IPromptServiceHandler
      Retrieves the message localizer for custom messages, such as localized messages for the configuration UI.
      Specified by:
      getMessageLocalizer in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The resource bundle to use for localized messages.
    • getQueryConfigClass

      public Class<GenericOaiQueryConfig> getQueryConfigClass()
      Description copied from interface: IPromptServiceHandler
      Gets the class of the query configuration type parameter.

      The default implementation attempts to read the type of the QueryConfig type parameter via reflection. If that does not work, or you need to optimize, override this method and return the type directly.

      Specified by:
      getQueryConfigClass in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
    • getQueryConfigVersion

      public String getQueryConfigVersion()
      Description copied from interface: PromptServiceHandlerMixin_JsonConfig
      Gets the version of the query configuration class. This is saved in the serialized data. Can be used to update the data if the class definition has undergone changes. If you need to update the serialized data, override the updateQueryConfig method. Defaults to the serialVersionUID static field of the class, if it exists, or "1.0" otherwise.
      Specified by:
      getQueryConfigVersion in interface PromptServiceHandlerMixin_JsonConfig<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The version of the query configuration class, as a string.
    • getQueryViewBeanClass

      public Class<GenericOaiQueryConfigBean> getQueryViewBeanClass()
      Description copied from interface: IPromptServiceHandler
      Returns the class of the bean that should be used when editing the properties of a prompt query. May be null if you do not require any bean or custom logic. When you only wish to access the properties of your configuration, you do have to use a custom bean - the model is available via the expression language variable model. See IPromptServiceHandler.getQueryViewXhtml() for further details.

      The default returns null, which uses no extra bean. An extra bean may not be required for simple UIs if you only need to access the properties model.

      Specified by:
      getQueryViewBeanClass in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The class of the bean to use for editing a prompt query's configuration.
      See Also:
    • getQueryViewXhtml

      public URL getQueryViewXhtml()
      Description copied from interface: IPromptServiceHandler
      This method must return the path to the XHTML page for the custom user interface of a prompt query. Usually the XHTML file is part of the JAR resources of the module or plugin. In this case, you should return a URL to a JAR file resource (jar:file:/...) like so:
      @Override
      public URL getQueryViewXhtml() {
        return getClass().getResource("/path/to/view.xhtml");
      }
      

      The contents of this XHTML page is included in the backend menu for prompt queries, already inside a form (i.e. do not add an additional h:form as that would result in an error). You should wrap your custom UI in a naming container to ensure unique IDs that do not clash with other prompt services. Implementations may (but need not) use the components provided by the http://www.xima.de/taglib/xfc/prompt tag library. The following is a recommended template on which you may base your UI:

      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:xp="http://www.xima.de/taglib/xfc/prompt">
      
        <xp:ui id="CHANGE_THIS_TO_A_UNIQUE_ID">
      
          <!-- Custom section for the UI of your prompt queries -->
          <xp:fieldset legend="#{msg['MyAwesomePromptService.prompt.fs.base']}>
            <!-- A simple select field for the type of the task -->
              <xp:task id="task" value="#{model.task}" options="#{bean.taskTypeOptions}">
                <p:ajax global="false" listener="#{bean.onPromptQueryParamFieldChanged}"
                  process="@this,@promptUi:taskConfig"
                  update="@promptUi:taskConfig" />
              </xp:task>
          </xp:fieldset>
        </xp:ui>
      
      </xp:composition>
      

      The XHTML page may access the following expression language variables:

      • model: The deserialized configuration of the query.
      • msg: A virtual ResourceBundle for the localized messages, backed by the message localizer. Empty when that method returns null. For example, if the resource bundle contains the key prompt.label, you can access the localized message via the EL expression msg['prompt.label']
      • bean: An instance of the bean class returned by getQueryViewBeanClass. Will be null if that method does not define a bean class.
      The XHTML page may use the following additional search expressions (e.g. for the process and update attributes of a <p:ajax />):
      • @promptUi: Resolves to the closest <xp:ui /> component from the http://www.xima.de/taglib/xfc/prompt tag library.
      • @promptFieldset: Resolves to the closest <xp:promptFieldset /> component from the http://www.xima.de/taglib/xfc/prompt tag library.

      To access the values of an enum or the constants defined by a class, consider using:

      <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:o="http://omnifaces.org/ui">
        <o:importConstants type="my.fully.classified.path.MyEnum" var="MyEnum" loader="#{model}"/>
      </ui:composition>
      
      The loader argument is only required for plugins and ensures that the correct class loader is used that knows about the plugin class. Without the loader attribute, the above is equivalent to the PrimeFaces tag handler <p:importConstants />.
      Specified by:
      getQueryViewXhtml in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      Path to the XHTML view, must noe be null. If you do return null, it will be treated as an error and an appropriate message is displayed to the user informing them that the properties panel could not be loaded.
    • getType

      public String getType()
      Description copied from interface: IPromptServiceHandler
      The technical type of this prompt service. Must be unique across all prompt services, including built-in and plugin services. For plugins, we recommend using a prefix to avoid clashes with built-in services and other plugin services, such as the group and artifact ID of the plugin.
      Specified by:
      getType in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The type of the prompt service.
    • getTypeDisplayDescriptionKey

      public String getTypeDisplayDescriptionKey()
      Description copied from interface: IPromptServiceHandler
      Gets the key for the display description of this type of prompt service, such as "A prompt service that uses OpenAI's GPT-3 model to generate text responses." This might be shown in the prompt service selection UI, to provide further details for IPromptServiceHandler.getTypeDisplayNameKey().

      The returned key will be localized via IPromptServiceHandler.getMessageLocalizer().

      Specified by:
      getTypeDisplayDescriptionKey in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The display description of the prompt service, localized for the given locale.
    • getTypeDisplayNameKey

      public String getTypeDisplayNameKey()
      Description copied from interface: IPromptServiceHandler
      Gets the display name of this type of prompt service, such as "OpenAI GPT-3" or "Stable Diffusion". Might e.g. be shown when the user select a prompt service type.

      The returned key will be localized via IPromptServiceHandler.getMessageLocalizer().

      Specified by:
      getTypeDisplayNameKey in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The display name of the prompt service, localized for the given locale.
    • getTypeIcon

      public IGuiIcon getTypeIcon()
      Description copied from interface: IPromptServiceHandler
      Gets the icon that should be used to represent this prompt service, such as in the backend menu. The default implementation returns a default neutral icon.
      Specified by:
      getTypeIcon in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Returns:
      The icon to use for this prompt service. Must not be null.
    • testConnection

      Description copied from interface: IPromptServiceHandler
      Tests the given prompt service connection to see if it is valid and can be used to execute queries.
      Specified by:
      testConnection in interface IPromptServiceHandler<GenericOaiConnectionConfig, GenericOaiQueryConfig>
      Parameters:
      params - The parameters for the test connection, with the connection configuration and any additional data needed.
      Returns:
      The result of the test connection, with the success status and any error messages.