Example: Model Execution and Management Library

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.

pragma solidity ^0.8.0;

// Interface for an Oracle to fetch external data
interface IOracle {
    function requestData(uint256 requestId, string calldata data) external;
    function getData(uint256 requestId) external view returns (string memory);
}

// Library for AI model management
library AIModelLibrary {
    struct Model {
        string modelName;
        address owner;
        uint256 version;
        bool active;
    }

    struct ModelExecution {
        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;

    event ModelRegistered(uint256 modelId, string modelName, address owner);
    event ModelExecuted(uint256 executionId, uint256 modelId, string inputData, string outputData, uint256 timestamp);

    uint256 private modelCounter = 1;
    uint256 private executionCounter = 1;

    // Register a new AI model
    function registerModel(string calldata modelName) external returns (uint256) {
        uint256 modelId = modelCounter++;
        models[modelId] = Model({
            modelName: modelName,
            owner: msg.sender,
            version: 1,
            active: true
        });
        ownerModels[msg.sender].push(modelId);

        emit ModelRegistered(modelId, modelName, msg.sender);
        return modelId;
    }

    // Execute a model with input data
    function executeModel(uint256 modelId, string calldata inputData, IOracle oracle) external returns (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 Oracle
    function receiveExecutionResult(uint256 executionId, string calldata outputData) external {
        require(executions[executionId].executed == false, "Execution already completed");

        executions[executionId].outputData = outputData;
        executions[executionId].executed = true;

        emit ModelExecuted(executionId, executionId, executions[executionId].inputData, outputData, block.timestamp);
    }

    // Get details of a model
    function getModelDetails(uint256 modelId) external view returns (Model memory) {
        return models[modelId];
    }

    // Get execution details of a model
    function getExecutionDetails(uint256 executionId) external view returns (ModelExecution memory) {
        return executions[executionId];
    }

    // Get models owned by an address
    function getModelsByOwner(address owner) external view returns (uint256[] memory) {
        return ownerModels[owner];
    }

    // Deactivate a model
    function deactivateModel(uint256 modelId) external {
        require(models[modelId].owner == msg.sender, "Only owner can deactivate the model");
        models[modelId].active = false;
    }

    // Update model version
    function updateModelVersion(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.

Last updated