| Layer | Role |
|---|---|
Agent (MonoBehaviour) | Handles Unity-side orchestration: captures input, triggers inference, and dispatches results. |
Provider (ScriptableObject) | Defines the backend — how and where inference happens (Cloud, Local, On-Device). Defines input/output structure. |
| Interface | Purpose |
|---|---|
IChatTask | Defines text or multimodal LLM chat tasks. |
IObjectDetectionTask | Handles object detection (boxes, labels, confidence). |
ISpeechToTextTask | Converts audio input to transcribed text. |
ITextToSpeechTask | Converts text into playable speech audio. |
RunTaskAsync() or
RunChatAsync()) that accepts structured input and returns a strongly typed
result.Llama API Provider implementing IChatTask
[CreateAssetMenu(menuName = "Meta/AI/Providers/Cloud/Llama API Provider")]
public class LlamaApiProvider : AIProviderBase, IChatTask
{
[Header("Llama API Settings")]
[SerializeField] private string endpointUrl = "https://api.llama.com/v1/chat/completions";
[SerializeField] private string apiKey;
[SerializeField] private string model = "llama-4-maverick";
[SerializeField] private bool supportsVision = true;
[SerializeField] private bool inlineRemoteImages = true;
[SerializeField] private bool resolveRemoteRedirects = false;
public override string ProviderName => "Llama API (Maverick 4)";
public async Task<string> RunChatAsync(string prompt, CancellationToken token)
{
// Construct the Maverick 4 request payload
var body = new
{
model = model,
messages = new[]
{
new { role = "user", content = prompt }
},
temperature = 0.6f,
max_tokens = 512,
top_p = 0.9f,
stream = false
};
var json = JsonUtility.ToJson(body);
// Prepare headers for the API call
var headers = new Dictionary<string, string>
{
{ "Authorization", $"Bearer {apiKey}" }
};
// Send the request using the shared HttpTransport
var response = await HttpTransport.PostJsonAsync(endpointUrl, json, token, headers);
if (string.IsNullOrEmpty(response))
{
HandleError("Empty response from Llama API Maverick 4");
return string.Empty;
}
// Parse the response and extract the message content
var result = JsonUtility.FromJson<LlamaResponse>(response);
return result?.choices != null && result.choices.Length > 0
? result.choices[0].message.content
: string.Empty;
}
[System.Serializable]
private class LlamaResponse
{
public Choice[] choices;
}
[System.Serializable]
private class Choice
{
public Message message;
}
[System.Serializable]
private class Message
{
public string role;
public string content;
}
}
AIProviderBaseLlamaApiProvider — inherit from
AIProviderBase, which provides:| Responsibility | Description |
|---|---|
API Key Management | Stores and secures tokens as serialized fields. |
Endpoint Configuration | Defines your API URL and model ID (for example, llama-4-maverick). |
Network Handling | Uses HttpTransport for all JSON or binary POST requests with built-in retry and cancellation logic. |
Error Handling | Centralized HandleError() and OnError events for reliable debugging and logging. |
CreateAssetMenu attribute: Meta/AI/Providers/<YourCategory>IChatTask, IObjectDetectionTask,
and so on).HttpTransport handles standard HTTP workflows, but you can extend
it for real-time streaming or gRPC/WebSocket backends.MyWebSocketTransport.cs.HttpTransport.public class ContextualLlmAgent : LlmAgent
{
public string Context = "You are a helpful assistant.";
public override async Task SendPrompt(string userInput)
{
var fullPrompt = Context + "\n" + userInput;
await base.SendPrompt(fullPrompt);
}
}