PHP code example of adrenallen / ai-agents-laravel

1. Go to this page and download the library: Download adrenallen/ai-agents-laravel 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/ */

    

adrenallen / ai-agents-laravel example snippets


class TextingAgent extends BaseAgent {

    use \Adrenallen\AiAgentsLaravel\AgentTraits\SMSTrait; // Access to send SMS via Twilio, all handled automatically

    public string $prePrompt = "You are a helpful assistant";   // Pre-prompt
}

/**
* @aiagent-description Adds two numbers together
* @param int $a
* @param int $b
* @return int
*/
public function add(int $a, int $b): int {
    return $a + $b;
}

$chat = new \Adrenallen\AiAgentsLaravel\ChatModels\ChatGPT();
// or
$chat = new \Adrenallen\AiAgentsLaravel\ChatModels\AzureOpenAI();
// or
$chat = new \Adrenallen\AiAgentsLaravel\ChatModels\AnthropicClaude();

$agent = new \Adrenallen\AiAgentsLaravel\Agents\TestingAgent($chat); // Ensures the agent gets a pre-prompt at creation
$agent->ask("Hello, is this thing on?"); // Yes, I'm here. How can I assist you today?
$agent->lastCallMetadata;
/*
return $agent->lastCallMetadata;
= [
    "id" => "chatcmpl-8123ABC",
    "created" => 1705545737,
    "model" => "gpt-4",
    "systemFingerprint" => "fp_l33t123",
    "usage" => OpenAI\Responses\Chat\CreateResponseUsage {#5004
      +promptTokens: 365,
      +completionTokens: 17,
      +totalTokens: 382,
    },
  ]
*/

class TestingAgent extends BaseAgent {

    use \Adrenallen\AiAgentsLaravel\AgentTraits\SMSTrait; // Access to send SMS via Twilio
    use \Adrenallen\AiAgentsLaravel\AgentTraits\MathTrait; // Access to math functions
    use \Adrenallen\AiAgentsLaravel\AgentTraits\DateTrait;  // Access to date functions
    use \Adrenallen\AiAgentsLaravel\AgentTraits\WeatherTrait; // Access to openweathermap API

    public string $prePrompt = "You are a helpful assistant";   // Pre-prompt
}

    /**
     * @param int $a
     * @param int $b
     * @return int
     * @aiagent-description Adds two numbers together
     */
    public function add(int $a, int $b): int {
        return $a + $b;
    }