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.
public class XrAiModelManager : MonoBehaviour
Represents a key-value pair for configuration properties.
[System.Serializable]
public class XrAiProperty
{
public string key;
public string value;
}
Groups related properties under a section name.
[System.Serializable]
public class XrAiSection
{
public string sectionName;
public List<XrAiProperty> properties = new List<XrAiProperty>();
}
Container for all configuration sections.
[System.Serializable]
public class XrAiModelData
{
public List<XrAiSection> sections = new List<XrAiSection>();
}
private const string CONFIG_FILE_PATH = "XrAiModelConfig";
private const string API_KEYS_FILE_PATH = "XrAiApiKeys";
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";
Gets the main configuration data, loading from file if necessary.
public XrAiModelData ModelData { get; }
Gets the API keys data, separated from main configuration for security.
public XrAiModelData ApiKeysData { get; }
Loads configuration data from Resources folder files.
public void LoadFromFile()
Saves configuration data to Resources folder files.
public void SaveToFile()
Retrieves all global properties for a section, including API keys.
public Dictionary<string, string> GetGlobalProperties(string sectionName)
Retrieves workflow-specific properties for a section.
public Dictionary<string, string> GetWorkflowProperties(string sectionName, string workflowName)
Retrieves a single global property value.
public string GetGlobalProperty(string sectionName, string key, string defaultValue = "")
Retrieves a single workflow-specific property value.
public string GetWorkflowProperty(string sectionName, string workflowName, string key, string defaultValue = "")
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"
);
}
}
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
}
}
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;
}
}
{
"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"
}
]
}
]
}
{
"sections": [
{
"sectionName": "Groq",
"properties": [
{
"key": "apiKey",
"value": "your-groq-api-key"
}
]
},
{
"sectionName": "OpenAI",
"properties": [
{
"key": "apiKey",
"value": "your-openai-api-key"
}
]
}
]
}
The manager automatically initializes configuration for known providers:
Each provider section automatically includes:
XrAiApiKeys.txt
)Resources
folderpublic 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);
}
}
The manager supports Unity Editor integration:
#if UNITY_EDITOR
// Auto-refresh assets when saving configuration
UnityEditor.AssetDatabase.Refresh();
#endif