This library is designed to facilitate the creation, management and execution of AI models within smart contracts on the Evire blockchain. It provides tools for deploying models, managing data inputs and outputs, and ensuring secure, efficient execution of AI processes in a decentralized environment.
pragmasolidity ^0.8.0;// Interface for an Oracle to fetch external datainterface IOracle {functionrequestData(uint256 requestId,stringcalldata data) external;functiongetData(uint256 requestId) externalviewreturns (stringmemory);}// Library for AI model managementlibraryAIModelLibrary {structModel {string modelName;address owner;uint256 version;bool active; }structModelExecution {uint256 executionId;string inputData;string outputData;uint256 timestamp;bool executed; }mapping(uint256=> Model) private models;mapping(uint256=> ModelExecution) private executions;mapping(address=>uint256[]) private ownerModels;eventModelRegistered(uint256 modelId, string modelName, address owner);eventModelExecuted(uint256 executionId, uint256 modelId, string inputData, string outputData, uint256 timestamp);uint256private modelCounter =1;uint256private executionCounter =1;// Register a new AI modelfunctionregisterModel(stringcalldata modelName) externalreturns (uint256) {uint256 modelId = modelCounter++; models[modelId] =Model({ modelName: modelName, owner: msg.sender, version:1, active:true }); ownerModels[msg.sender].push(modelId);emitModelRegistered(modelId, modelName, msg.sender);return modelId; }// Execute a model with input datafunctionexecuteModel(uint256 modelId,stringcalldata inputData,IOracle oracle) externalreturns (uint256) {require(models[modelId].active,"Model is not active");uint256 executionId = executionCounter++; executions[executionId] =ModelExecution({ executionId: executionId, inputData: inputData, outputData:"", timestamp: block.timestamp, executed:false });// Request data from oracle for model execution oracle.requestData(executionId, inputData);return executionId; }// Callback function to receive execution result from OraclefunctionreceiveExecutionResult(uint256 executionId,stringcalldata outputData) external {require(executions[executionId].executed ==false,"Execution already completed"); executions[executionId].outputData = outputData; executions[executionId].executed =true;emitModelExecuted(executionId, executionId, executions[executionId].inputData, outputData, block.timestamp); }// Get details of a modelfunctiongetModelDetails(uint256 modelId) externalviewreturns (Modelmemory) {return models[modelId]; }// Get execution details of a modelfunctiongetExecutionDetails(uint256 executionId) externalviewreturns (ModelExecutionmemory) {return executions[executionId]; }// Get models owned by an addressfunctiongetModelsByOwner(address owner) externalviewreturns (uint256[] memory) {return ownerModels[owner]; }// Deactivate a modelfunctiondeactivateModel(uint256 modelId) external {require(models[modelId].owner == msg.sender,"Only owner can deactivate the model"); models[modelId].active =false; }// Update model versionfunctionupdateModelVersion(uint256 modelId) external {require(models[modelId].owner == msg.sender,"Only owner can update the model version"); models[modelId].version++; }}
Key Features
Model Registration, by allowing developers to register new AI models and storing details such as model name, owner, version, and activation status.
Model Execution, facilitating the execution of AI models by interacting with decentralized oracles to fetch necessary data inputs.
Execution Tracking, recording and tracking each model execution, storing input data, output data, and timestamps to maintain a clear history.
Owner Management, associating models with their respective owners and providing functions to retrieve models by owner.
Model Lifecycle Management, including functions for deactivating models and updating model versions to ensure models can be managed and maintained effectively.