PHP code example of mftsoft / agent-runner

1. Go to this page and download the library: Download mftsoft/agent-runner 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/ */

    

mftsoft / agent-runner example snippets


use MftSoft\AgentRunner\AgentRunner;

$runner = new AgentRunner();

$resposta = $runner->run('Qual é a capital do Brasil?');

echo $resposta;

$runner = new AgentRunner([
    'apiKey'        => 'sua-chave',
    'url'           => 'https://api.openai.com/v1/',
    'model'         => 'gpt-4o-mini',
    'systemPrompt'  => 'Você é um assistente especializado em finanças.',
    'temperature'   => 0.7,
    'maxIterations' => 10,
    'skillsPath'    => '/caminho/para/skills',
]);

use MftSoft\AgentRunner\AgentRunner;
use MftSoft\AgentRunner\ToolFactory;
use LLPhant\Chat\FunctionInfo\Parameter;

// Classe de serviço (ex: do AdiantFramework)
$calculadora = new CalculadoraService();

$runner = new AgentRunner();

$runner->addTool(
    ToolFactory::build(
        $calculadora,
        'calcular',
        'Realiza cálculos matemáticos a partir de uma expressão',
        new Parameter('expressao', 'string', 'A expressão matemática a calcular')
    )
);

$resposta = $runner->run('Quanto é 15% de 3.800?');

class CalculadoraService
{
    public function calcular(string $expressao): string
    {
        // implementação
    }
}

$runner = new AgentRunner(['skillsPath' => '/var/app/skills']);

// Envia uma mensagem e retorna a resposta final do agente
$runner->run(string $prompt): string

// Adiciona uma tool ao agente
$runner->addTool(FunctionInfo $tool): void

// Substitui o system prompt
$runner->setSystemPrompt(string $prompt): void

// Define o histórico de mensagens
$runner->setMessages(array|MessageCollection $messages): void

// Retorna o histórico de mensagens
$runner->getMessages(): array

// Limpa o histórico de mensagens
$runner->reset(): void

// Retorna a instância do chat (LLPhant)
$runner->getChat(): OpenAIChat

// Constrói um FunctionInfo a partir de um objeto de serviço
ToolFactory::build(
    object $service,
    string $method,
    string $description,
    Parameter ...$parameters
): FunctionInfo

// Define o caminho base das skills
SkillFactory::setSkillsPath(string $path): void

// Retorna o caminho base das skills
SkillFactory::getSkillsPath(): string

// Retorna a lista de skills disponíveis em formato Markdown
SkillFactory::listSkills(): string

// Constrói o FunctionInfo da tool readSkill (usado internamente pelo AgentRunner)
SkillFactory::build(): FunctionInfo

$runner = new AgentRunner([
    'url'   => 'http://localhost:11434/v1/',
    'model' => 'llama3',
]);