Class FunctionManager

java.lang.Object
com.jackdaw.chatwithnpc.openaiapi.function.FunctionManager

public class FunctionManager extends Object
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • folder

      private static final Path folder
    • functionRegistry

      private static final Map<String,CustomFunction> functionRegistry
  • Constructor Details

    • FunctionManager

      public FunctionManager()
  • Method Details

    • getRegistryList

      @NotNull public static @NotNull ArrayList<String> getRegistryList()
    • registerFunction

      public static void registerFunction(@NotNull @NotNull String name, @NotNull @NotNull CustomFunction function)
      Register a function that can be used by the NPC. It will be called by the OpenAI Assistant.
      Parameters:
      name - The name of the function
      function - The function
    • registerFromJson

      public static void registerFromJson(String json)
      Register a function from a JSON string. It will be called by the OpenAI Assistant and then call the function in Minecraft World data package.

      The JSON string should be in the following format:

           {
               "type": "function",
               "function": {
                   "name": "functionName",
                   "description": "This function does something",
                   "parameters": {
                       "type": "object",
                       "properties": {
                           "param1": {
                               "type": "integer",
                               "description": "This is the first parameter",
                               "enum": [1, 2, 3]
                           },
                           "param2": {
                               "type": "integer",
                               "description": "This is the second parameter"
                           }
                       },
                       "required": ["param1", "param2"]
                   }
               }
               "call": "NameSpace:function"
           }
       
      Note: The parameters must be integer as it will be stored in scoreboard.

      The parameters are stored in the nearby players' scoreboard with format "npc_UUID_paramName". The UUID is the UUID of the NPC.

      The "call" field is optional, it is the function stored in Minecraft World data package with format "NameSpace:function". If it is not null, the function will be called as the Server in NPC's World with its position and with permission level 2.

      This function will not be executed if "call" is null. It will only add the integer parameters to the scoreboard.

      Parameters:
      json - The JSON string
    • sync

      public static void sync()
      Get the list of the functions, and register them.
    • callFunction

      public static Map<String,String> callFunction(@NotNull @NotNull ConversationHandler conversation, @NotNull @NotNull String name, @NotNull @NotNull Map<String,Object> args)
      Call a function by its name. It will be executed by the OpenAI Assistant and work on the conversation.
      Parameters:
      name - The name of the function
      conversation - The conversation handler
      args - The arguments
    • getRequestJson

      public static String getRequestJson(@NotNull @NotNull String name)
      Get the JSON string of a function by its name.
      Parameters:
      name - The name of the function
      Returns:
      The JSON string