Class FunctionManager
java.lang.Object
com.jackdaw.chatwithnpc.openaiapi.function.FunctionManager
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
private static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Path
private static final Map<String,
CustomFunction> private static final org.slf4j.Logger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncallFunction
(@NotNull ConversationHandler conversation, @NotNull String name, @NotNull Map<String, Object> args) Call a function by its name.static String
getRequestJson
(@NotNull String name) Get the JSON string of a function by its name.static void
registerFromJson
(String json) Register a function from a JSON string.static void
registerFunction
(@NotNull String name, @NotNull CustomFunction function) Register a function that can be used by the NPC.static void
sync()
Get the list of the functions, and register them.
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
folder
-
functionRegistry
-
-
Constructor Details
-
FunctionManager
public FunctionManager()
-
-
Method Details
-
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 functionfunction
- The function
-
registerFromJson
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 functionconversation
- The conversation handlerargs
- The arguments
-
getRequestJson
Get the JSON string of a function by its name.- Parameters:
name
- The name of the function- Returns:
- The JSON string
-