xr-ai-accelerator

XrAiModelManager

The XrAiModelManager class is a MonoBehaviour component that manages AI model configurations, API keys, and workflow-specific properties. It provides a centralized system for storing and retrieving model settings across different AI providers and workflows.

Class Declaration

public class XrAiModelManager : MonoBehaviour

Data Structures

XrAiProperty

Represents a key-value pair for configuration properties.

[System.Serializable]
public class XrAiProperty
{
    public string key;
    public string value;
}

XrAiSection

Groups related properties under a section name.

[System.Serializable]
public class XrAiSection
{
    public string sectionName;
    public List<XrAiProperty> properties = new List<XrAiProperty>();
}

XrAiModelData

Container for all configuration sections.

[System.Serializable]
public class XrAiModelData
{
    public List<XrAiSection> sections = new List<XrAiSection>();
}

Constants

File Paths

private const string CONFIG_FILE_PATH = "XrAiModelConfig";
private const string API_KEYS_FILE_PATH = "XrAiApiKeys";

Workflow Types

public const string WORKFLOW_IMAGE_TO_IMAGE = "ImageToImage";
public const string WORKFLOW_IMAGE_TO_TEXT = "ImageToText";
public const string WORKFLOW_TEXT_TO_IMAGE = "TextToImage";
public const string WORKFLOW_SPEECH_TO_TEXT = "SpeechToText";
public const string WORKFLOW_IMAGE_TO_3D = "ImageTo3d";
public const string WORKFLOW_OBJECT_DETECTOR = "ObjectDetector";
public const string WORKFLOW_TEXT_TO_SPEECH = "TextToSpeech";

Properties

ModelData

Gets the main configuration data, loading from file if necessary.

public XrAiModelData ModelData { get; }

ApiKeysData

Gets the API keys data, separated from main configuration for security.

public XrAiModelData ApiKeysData { get; }

Methods

LoadFromFile

Loads configuration data from Resources folder files.

public void LoadFromFile()

SaveToFile

Saves configuration data to Resources folder files.

public void SaveToFile()

GetGlobalProperties

Retrieves all global properties for a section, including API keys.

public Dictionary<string, string> GetGlobalProperties(string sectionName)

GetWorkflowProperties

Retrieves workflow-specific properties for a section.

public Dictionary<string, string> GetWorkflowProperties(string sectionName, string workflowName)

GetGlobalProperty

Retrieves a single global property value.

public string GetGlobalProperty(string sectionName, string key, string defaultValue = "")

GetWorkflowProperty

Retrieves a single workflow-specific property value.

public string GetWorkflowProperty(string sectionName, string workflowName, string key, string defaultValue = "")

Usage Examples

Basic Setup

public class AIConfigurationManager : MonoBehaviour
{
    private XrAiModelManager modelManager;
    
    void Start()
    {
        modelManager = GetComponent<XrAiModelManager>();
        ConfigureAIModels();
    }
    
    private void ConfigureAIModels()
    {
        // Get OpenAI configuration
        var openAIConfig = modelManager.GetGlobalProperties("OpenAI");
        string apiKey = openAIConfig.GetValueOrDefault("apiKey", "");
        
        if (string.IsNullOrEmpty(apiKey))
        {
            Debug.LogWarning("OpenAI API key not configured");
        }
        
        // Get workflow-specific settings
        string imageToTextModel = modelManager.GetWorkflowProperty(
            "OpenAI", 
            XrAiModelManager.WORKFLOW_IMAGE_TO_TEXT, 
            "model", 
            "gpt-4-vision-preview"
        );
    }
}

Dynamic Configuration

public class RuntimeConfiguration : MonoBehaviour
{
    private XrAiModelManager modelManager;
    
    public void SetupProviderConfiguration(string provider, Dictionary<string, string> config)
    {
        modelManager = GetComponent<XrAiModelManager>();
        
        // Update configuration at runtime
        foreach (var setting in config)
        {
            SetGlobalProperty(provider, setting.Key, setting.Value);
        }
        
        // Save changes
        modelManager.SaveToFile();
    }
    
    private void SetGlobalProperty(string section, string key, string value)
    {
        // This would require extending the manager to support runtime updates
        // or directly modifying the ModelData structure
    }
}

Workflow-Specific Configuration

public class WorkflowConfigurationExample : MonoBehaviour
{
    private XrAiModelManager modelManager;
    
    public async Task<string> ProcessImageWithGroq(Texture2D image)
    {
        modelManager = GetComponent<XrAiModelManager>();
        
        // Get Groq configuration for Image-to-Text workflow
        var groqConfig = modelManager.GetGlobalProperties("Groq");
        var workflowConfig = modelManager.GetWorkflowProperties("Groq", XrAiModelManager.WORKFLOW_IMAGE_TO_TEXT);
        
        // Load the model with configuration
        var imageToText = XrAiFactory.LoadImageToText("Groq", groqConfig);
        
        // Execute with workflow-specific options
        byte[] imageData = XrAiImageHelper.EncodeTexture(image, "image/jpeg");
        var result = await imageToText.Execute(imageData, "image/jpeg", workflowConfig);
        
        return result.IsSuccess ? result.Data : null;
    }
}

Configuration File Structure

Main Configuration (XrAiModelConfig.txt)

{
  "sections": [
    {
      "sectionName": "Groq",
      "properties": []
    },
    {
      "sectionName": "Groq.ImageToText",
      "properties": [
        {
          "key": "model",
          "value": "llama-vision-free"
        },
        {
          "key": "prompt",
          "value": "Describe what you see in this image."
        }
      ]
    },
    {
      "sectionName": "OpenAI.SpeechToText",
      "properties": [
        {
          "key": "model",
          "value": "whisper-1"
        }
      ]
    }
  ]
}

API Keys Configuration (XrAiApiKeys.txt)

{
  "sections": [
    {
      "sectionName": "Groq",
      "properties": [
        {
          "key": "apiKey",
          "value": "your-groq-api-key"
        }
      ]
    },
    {
      "sectionName": "OpenAI",
      "properties": [
        {
          "key": "apiKey",
          "value": "your-openai-api-key"
        }
      ]
    }
  ]
}

Predefined Sections and Properties

The manager automatically initializes configuration for known providers:

Supported Providers

Auto-Initialized Properties

Each provider section automatically includes:

Security Considerations

API Key Separation

File Location

Integration with AI Factory

public class FactoryIntegration : MonoBehaviour
{
    private XrAiModelManager modelManager;
    
    public IXrAiImageToText CreateConfiguredImageToText(string provider)
    {
        modelManager = GetComponent<XrAiModelManager>();
        
        // Get provider configuration
        var config = modelManager.GetGlobalProperties(provider);
        
        // Load model with configuration
        return XrAiFactory.LoadImageToText(provider, config);
    }
    
    public async Task<XrAiResult<string>> ExecuteWithWorkflowConfig(
        IXrAiImageToText model, 
        byte[] imageData, 
        string provider)
    {
        // Get workflow-specific options
        var workflowOptions = modelManager.GetWorkflowProperties(
            provider, 
            XrAiModelManager.WORKFLOW_IMAGE_TO_TEXT
        );
        
        return await model.Execute(imageData, "image/jpeg", workflowOptions);
    }
}

Editor Integration

The manager supports Unity Editor integration:

#if UNITY_EDITOR
// Auto-refresh assets when saving configuration
UnityEditor.AssetDatabase.Refresh();
#endif

Best Practices

  1. Separate Concerns: Keep API keys separate from workflow configuration
  2. Default Values: Always provide sensible defaults for configuration properties
  3. Validation: Validate configuration before using with AI models
  4. Environment-Specific: Consider different configurations for development/production
  5. Documentation: Document all configuration properties and their purposes

Implementation Notes