1. Go to this page and download the library: Download milpa/ai-gateway library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
milpa / ai-gateway example snippets
use Milpa\AiGateway\AgentOrchestrator;
use Milpa\AiGateway\LlmService;
use Milpa\AiGateway\McpClientService;
use Milpa\ToolRuntime\ToolRegistry;
use Psr\Log\NullLogger;
$registry = new ToolRegistry(new NullLogger());
$registry->register(
'get_time',
'Get the current time',
[],
fn () => ['time' => '12:00 PM'],
);
$mcpClient = new McpClientService($registry);
$llm = new LlmService(apiKey: getenv('OPENAI_API_KEY'), model: 'gpt-4o', provider: 'openai');
// LlmService talks HTTP through PSR-18 (Psr\Http\Client\ClientInterface), defaulting to a
// Guzzle client when none is injected — see "Bringing your own HTTP client" below.
$orchestrator = new AgentOrchestrator($llm, $mcpClient);
echo $orchestrator->run('What time is it?');
// -> asks the model, the model requests `get_time`, AgentOrchestrator executes it through
// the registry, feeds the result back, and returns the model's final answer.
use Milpa\AiGateway\RunEnd;
try {
$answer = $orchestrator->run('Inspect the app.');
} finally {
$end = $orchestrator->termination();
// $end?->reason === RunEnd::FinalAnswer means the final-answer branch returned.
// It does not mean the requested work is complete or approved.
}
use Milpa\ToolRuntime\Gate\ToolCallGate;
use Milpa\ToolRuntime\Gate\ToolCallRecorder;
interface ToolCallGate
{
// The reason this call does not proceed, or null if it does.
public function refuse(string $tool, array $arguments): ?string;
}
interface ToolCallRecorder
{
// Told after the call, with the rendered result and whether it succeeded.
public function recorded(string $tool, array $arguments, string $result, bool $ok): void;
}
interface OptionTable
{
public function remove(string $option, string $code, ?string $message = null): void;
public function removed(): array; // the PROJECTION: what the model sees
public function wasRemoved(string $option): bool; // the FACT: did an authority declare it gone?
}
use Milpa\AiGateway\ProviderWindow;
$window = new ProviderWindow('http://localhost:11438');
$window->tokens(); // 32768, or null when the provider did not say
$window = new ProviderWindow('http://provider.test', fn (string $url): ?string => $recordedBody);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.