PHP Luminova: AI Agents Utility Class
Select, register, and use Luminova AI agents through a shared proxy.
The Luminova AI helper (Luminova\AI\Agent) class is the central manager that wraps any Luminova\Interface\AIAgentInterface implementation and forwards calls to the active provider. It supports both instance-based and static usage patterns, lazy agent instantiation from App\Config\AI, and a runtime registry so multiple AI engines can be used side-by-side within the same application.
Configuration
The Agent proxy reads its default agent from Application AI Configuration. Set App\Config\AI::$handler to 'OpenAI', 'Ollama', or 'Anthropic' to control which provider is used when no explicit agent is supplied. Configure its base URL and model for the selected provider as well.
// /app/Config/AI.php
// Inside App\Config\AI
public string $handler = 'Anthropic';
public string $apiKey = 'your-key-here';Instantiation
Default Agent from Config
use Luminova\AI\Agent;
$ai = new Agent();
$reply = $ai->message('Tell me a joke!');Inject an Explicit Agent
use Luminova\AI\Agent;
use Luminova\AI\Agents\Ollama;
$ai = new Agent(new Ollama('http://localhost:11434/api/'));
$reply = $ai->message('Explain quantum computing in plain language.');Singleton Access
Returns a shared singleton, creating it on the first call using the app's configured default agent.
$reply = Agent::create()->message('Write a haiku about the ocean.');
print_r($reply);Swapping Agents on the Singleton
Passing an agent to create() after the first call replaces the active agent without destroying the singleton.
use Luminova\AI\Agents\Ollama;
Agent::create(new Ollama())->message('Hello from Ollama!');Static Method Forwarding
Any method defined on an AIAgentInterface can be called statically on AI. The configured default agent is resolved automatically.
// Equivalent to: (new Agent())->message('...')
Agent::message('Write a short story about Mars.');Agent::chat([
['role' => 'system', 'content' => 'You are a friendly chef.'],
['role' => 'user', 'content' => 'What should I cook tonight?'],
]);Named Agent Access
Built-in Agents
The Agent class ships with three pre-registered agent keys: openai, ollama, and anthropic. Use with() for a registered agent. For a provider with its own credentials, register an explicitly configured instance. A registered agent is reused on later lookups.
use Luminova\AI\Agent;
use Luminova\AI\Agents\Anthropic;
use Luminova\AI\Agents\Ollama;
use Luminova\AI\Agents\OpenAI;
Agent::register('ollama', new Ollama('http://localhost:11434/api/'));
Agent::register('openai', new OpenAI(apiKey: (string) env('OPENAI_KEY')));
Agent::register('anthropic', new Anthropic(apiKey: (string) env('ANTHROPIC_KEY')));
$ollama = Agent::with('ollama');
$openai = Agent::with('openai');
$anthropic = Agent::with('anthropic');Agent Registry
Custom agent can be registered in application or controller class onCreate method:
// /app/Application.php
use Luminova\AI\Agent;
use App\AI\DeepSeek;
// In your application or controller's onCreate() hook:
Agent::register('deepseek', new DeepSeek());
// Or register the class for construction on first use:
Agent::register('deepseek', DeepSeek::class);Retrieve a Registered Agent
Once registered, the agent can be called like any built-in agent:
$agent = Agent::with('deepseek');
$reply = $agent->message('Hello!');Call Agent::with() without a name to retrieve the configured default. A registered name can also be used as a static call, for example Agent::Deepseek().
List All Registered Agents
foreach (Agent::agents() as $name => $agent) {
$type = is_string($agent) ? $agent : get_class($agent);
echo $name . ': ' . $type . PHP_EOL;
}Switching Agents at Runtime
Replace the active agent on an existing Agent instance without creating a new one.
use Luminova\AI\Agent;
use Luminova\AI\Agents\Ollama;
use Luminova\AI\Agents\Anthropic;
$ai = new Agent(); // Uses app default
$ai->setAgent(new Ollama('http://localhost:11434/api/'));
$reply = $ai->message('Now powered by Ollama!');
$ai->setAgent(new Anthropic(apiKey: env('ANTHROPIC_API_KEY')));
$reply = $ai->message('Now powered by Claude!');Inspect the Active Agent
$agent = Agent::create()->getAgent();
echo get_class($agent); // e.g. Luminova\AI\Agents\OpenAISetting the Default Model
use Luminova\AI\Agent;
$ai = new Agent();
$ai->setModel('gpt-4.1-mini');
$reply = $ai->message('Hello!');Computing Vector Similarity
Compute the cosine similarity between two equal-length embedding vectors. The vectors must have the same length and matching numeric indexes. A zero-length or zero-magnitude vector yields 0.0. For nonzero vectors, the cosine measures direction, where 1 means identical direction and 0 means orthogonal.
use Luminova\AI\Agent;
use Luminova\AI\Agents\OpenAI;
$agent = new OpenAI(apiKey: (string) env('OPENAI_KEY'));
$vectors = $agent->embed(['cat', 'kitten']);
$score = Agent::compareCosineVector($vectors[0], $vectors[1]);
echo $score; // e.g. ~0.94Practical Examples
Chat with the Default Agent
use Luminova\AI\Agent;
$reply = Agent::chat([
['role' => 'user', 'content' => 'What is the speed of light?'],
]);
print_r($reply);Embed and Compare Texts
use Luminova\AI\Agent;
$vectors = Agent::embed(['PHP is great', 'PHP is awesome']);
$score = Agent::compareCosineVector($vectors[0], $vectors[1]);
echo round($score, 4); // ~0.98Generate an Image
use Luminova\AI\Agent;
$images = Agent::image('A sunset over a tropical beach, watercolor style.');
echo $images[0]['url'] ?? '';Web Search
use Luminova\AI\Agent;
$results = Agent::webSearch('Latest Luminova framework release');
print_r($results);Register and Use a Custom Provider
use Luminova\AI\Agent;
use App\AI\MyChatAgent;
Agent::register('mychat', new MyChatAgent());
$reply = Agent::Mychat()->message('Hello from my custom provider!');Class Definition
- Full namespace:
Luminova\AI\Agent - This class is final.
Methods
__construct
Create a new AI manager instance.
If no agent is supplied, the default agent configured inApp\Config\AI::$handler is resolved automatically. The registry reuses an instance that has already been resolved.
public function __construct(?AIAgentInterface $agent = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
$agent | Luminova\Interface\AIAgentInterface|null | Optional custom agent instance. |
Examples:
Using the application default agent:
use Luminova\AI\Agent;
$ai = new Agent();
$reply = $ai->message('Tell me a joke!');Injecting an Ollama agent explicitly:
use Luminova\AI\Agent;
use Luminova\AI\Agents\Ollama;
$ai = new Agent(new Ollama('http://localhost:11434/api/'));
$reply = $ai->message('Explain quantum computing in plain language.');create
Get (or create) the shared AI singleton instance.
On first call, a new instance is created using the application'sdefault agent. Subsequent calls return the same instance.
Passing a $agent on any call after the first will replace the active agentwithout destroying the singleton.
public static function create(?AIAgentInterface $agent = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$agent | Luminova\Interface\AIAgentInterface|null | Optional agent to set or replace. |
Return:
self - Return singleton object of AI class.
Examples:
using default application AI agent:
use Luminova\AI\Agent;
$reply = Agent::create()->message('Write a haiku about the ocean.');Swapping agents on the singleton:
use Luminova\AI\Agent;
use Luminova\AI\Agents\Ollama;
Agent::create(new Ollama())->message('Hello from Ollama!');getAgent
Get the currently active AI agent.
public function getAgent(): AIAgentInterfaceReturn:
Luminova\Interface\AIAgentInterface - Return instance of agent class.
Example:
use Luminova\AI\Agent;
$agent = Agent::create()->getAgent();
echo get_class($agent); // Luminova\AI\Agents\OpenAIsetAgent
Replace the active AI agent.
Allows switching engines at runtime without creating a new Agent instance.
public function setAgent(AIAgentInterface $agent): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$agent | Luminova\Interface\AIAgentInterface | The new agent to use. |
Return:
self - Returns the proxy instance.
Example:
Change the current AI agent:
use Luminova\AI\Agent;
use Luminova\AI\Agents\Ollama;
$ai = new Agent();
$ai->setAgent(new Ollama('http://localhost:11434/api/'));
$reply = $ai->message('Now powered by Ollama!');setModel
Set the default model for all subsequent AI requests.
public function setModel(object|string $model): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$model | object|string | Model identifier. For example, gpt-4.1-mini. |
Return:
self - Returns the proxy instance.
Example:
Change the current AI agent:
// Using literal string
$ai->setModel('gpt-4.1-mini');
$reply = $ai->message('What is the weather today like?');register
Register a named agent in the global agent registry. Registering the same key again replaces its previous entry.
Accepts either a pre-built instance or a fully-qualified class name.Once registered, the agent can be retrieved via static method callsor Agent::with().
public static function register(string $name, AIAgentInterface|string $agent): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | Case-insensitive registry key (e.g. 'openai'). |
$agent | AIAgentInterface|class-string | Agent instance or class name. |
Examples:
Registering a live instance
use Luminova\AI\Agent;
Agent::register('myclient', new MyCustomProvider());Registering a class name (instantiated on first use):
use Luminova\AI\Agent;
Agent::register('myclient', MyCustomProvider::class);
Agent::with('myclient')->message('Hello!');
Agent::with('myclient')->message('Hello from Myclient!');with
Retrieve a agent instance from the registry by name.
If no name is given, the application's default agent is returned.If the agent entry is a class name string, it is instantiated on first access and the instance is cached for subsequent calls.
public static function with(?string $name = null): AIAgentInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string|null | Registry key (case-insensitive), or null for the default. |
Return:
Luminova\Interface\AIAgentInterface - Return agent class object.
Throws:
- Luminova\Exceptions\RuntimeException - If the requested agent is not registered.
Examples:
Get agent object:
use Luminova\AI\Agent;
$openai = Agent::with('openai');
$ollama = Agent::with('ollama');
$openai->message('Hello from OpenAI!');
$ollama->message('Hello from Ollama!');agents
Return all registered agent entries.
public static function agents(): arrayValues are either class-name strings (not yet instantiated) or liveAIAgentInterface objects.
Return:
array<string,class-string<Luminova\Interface\AIAgentInterface>|Luminova\Interface\AIAgentInterface> - Return array of all registered AI agent.
Example:
Get array of agents:
use Luminova\AI\Agent;
foreach (Agent::agents() as $name => $agent) {
echo $name . ': ' . (is_string($agent) ? $agent : get_class($agent));
}compareCosineVector
Compute the cosine similarity between two equal-length embedding vectors.
The vectors must have the same length and matching numeric indexes. A zero-length or zero-magnitude vector yields 0.0. For nonzero vectors, the cosine measures direction, where 1 means identical direction,0 means orthogonal, and -1 means opposite direction. Useful for comparing embeddings produced by AIAgentInterface::embed().
public static function compareCosineVector(array $a, array $b): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$a | float[] | First embedding vector. |
$b | float[] | Second embedding vector (must be the same length as $a). |
Return:
float - Cosine similarity score.
Example:
Compare embeddings vectors:
use Luminova\AI\Agent;
$vectors = $ai->embed(['cat', 'kitten']);
$score = Agent::compareCosineVector($vectors[0], $vectors[1]);
// $score → ~0.94 (very similar)