The py/gaas_gpt_function.py module provides the FunctionEngine class, which acts as a Python proxy to execute SEMOSS FUNCTION engines. This enables Python-based Generative AI Agent Services (GAAS) or other Python scripts to invoke predefined functions or tools that are managed by the SEMOSS backend.
- Purpose: The
FunctionEngineclass allows Python code to trigger the execution of a specific SEMOSSFUNCTIONengine. These backendFUNCTIONengines can encapsulate a wide range of operations, such as calling external APIs, running specific Python or R scripts, or even executing complex SEMOSS Pixel recipes. This class does not define the functions themselves but provides the means to call them. - Inheritance: It extends
gaas_server_proxy.ServerProxy, which is responsible for the underlying communication with the SEMOSS Java backend.
The constructor __init__(self, engine_id: str, insight_id: Optional[str] = None):
engine_id(str): Required. The ID of the target SEMOSSFUNCTIONengine (anIFunctionEngineinstance) that is configured in the Java backend. This ID specifies which function or tool will be executed.insight_id(Optional[str]): The ID of the current insight. This can be used for context by the backendFUNCTIONengine, such as accessing insight-specific variables or resources if the function is designed to do so.- The constructor asserts that
engine_idis provided and prints an initialization message.
execute(self, parameterMap: dict, insight_id: Optional[str] = None) -> Any:- Purpose: This is the primary method to call the configured SEMOSS
FUNCTIONengine. - Inputs:
parameterMap(dict): A Python dictionary where keys are the parameter names expected by the backendFUNCTIONengine, and values are the arguments for those parameters. This map is serialized to JSON when constructing the Pixel command.insight_id(Optional[str]): Overrides the instance'sinsight_idif provided, passing this specific insight context to the backend.
- Core Logic:
- Generates a unique
epocID for the transaction usingsuper().get_next_epoc(). - Constructs a Pixel script string:
ExecuteFunctionEngine(engine = "<engine_id>", map=[<json_serialized_parameterMap>]);- The
engine_idis the ID of theFUNCTIONengine to be executed. - The
parameterMapis converted into a JSON string.
- The
- Calls
super().callReactor(...)to send this Pixel script to the SEMOSS Java backend for execution. ThecallReactormethod (fromServerProxy) handles the communication.
- Generates a unique
- Outputs:
- The method returns the
"output"field from the firstpixelReturnstructure returned by thecallReactormethod. The nature and format of this output are determined by the specific backendFUNCTIONengine that was executed. It could be a simple value, a JSON string, a list, a dictionary, or any data type that the backend function returns and can be serialized. - If the Pixel execution does not return the expected structure, it might return
Noneor the rawpixelReturnobject.
- The method returns the
- Purpose: This is the primary method to call the configured SEMOSS
- The
FunctionEnginePython class is a client or proxy to aFUNCTIONengine (an implementation ofprerna.engine.api.IFunctionEngine) that is already defined and configured within the SEMOSS Java backend. - The actual logic of the function (e.g., calling an external API, running a script, performing a calculation) resides in the Java implementation of that backend
FUNCTIONengine. - The
parameterMapprovided to theexecutemethod is passed to the backendIFunctionEngine'sexecute(Map<String, Object> parameterValues)method.
- The constructor asserts that
engine_idis provided. - Errors related to the communication with the SEMOSS backend would be handled by the
ServerProxysuperclass. - Errors originating from the execution of the
FUNCTIONengine on the backend (e.g., issues within the function's logic, API call failures) would typically be propagated back through thepixelReturnstructure, potentially as an error message within the output or by raising an exception if thecallReactormethod is designed to do so for certain error types.
# Assuming gaas_server_proxy is configured and SEMOSS backend is running
# And a FUNCTION engine with ID "my_custom_api_caller" is configured in SEMOSS
function_engine_id = "my_custom_api_caller"
insight_context_id = "some_active_insight_id" # Optional
# Initialize the FunctionEngine client
function_tool = FunctionEngine(engine_id=function_engine_id, insight_id=insight_context_id)
# Define parameters for the backend FUNCTION engine
# This depends on what parameters "my_custom_api_caller" expects
params_for_function = {
"api_endpoint_param": "users/123",
"http_method": "GET",
"query_params": {"include_details": True}
}
try:
# Execute the function
result = function_tool.execute(parameterMap=params_for_function)
if result is not None:
print("Function execution successful. Result:")
print(result)
else:
print("Function execution might have failed or returned no specific output.")
except Exception as e:
print(f"Error executing function engine: {e}")This FunctionEngine class provides a straightforward way for Python-based GAAS components to leverage the extensible functionality offered by SEMOSS FUNCTION engines, enabling agents to perform a wide variety of pre-defined actions and tools managed by the core platform.