PHP code example of descom / ai-core

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

    

descom / ai-core example snippets


use Descom\Ai\Bedrock\Converse\Agent;

final class SupportAgent extends Agent
{
    protected string $modelId = 'anthropic.claude-3-haiku-20240307-v1:0';

    public function systemPrompt(): ?string
    {
        return 'You are a customer-support assistant.';
    }

    public function tools(): array
    {
        return [];
    }
}

$agent = new SupportAgent();

$response = $agent->ask('How do I reset my password?');
$response = $agent->sendImage('/path/to/screenshot.png', 'What error is shown?');
$response = $agent->sendDocument('/path/to/invoice.pdf', 'Summarize this invoice.');

echo $response->output()->message->text();
echo $response->usage()->totalTokens;
echo $response->metrics()->latencyMs;

use Descom\Ai\Bedrock\Converse\BedrockClientConverse;
use Descom\Ai\Bedrock\Messages\Contents\Contents;
use Descom\Ai\Bedrock\Messages\Contents\TextContent;
use Descom\Ai\Bedrock\Messages\Message;
use Descom\Ai\Bedrock\Messages\Messages;
use Descom\Ai\Bedrock\Messages\Role;

$client = new BedrockClientConverse(new SupportAgent());

$messages = new Messages([
    new Message(Role::USER, new Contents([new TextContent('Hi!')])),
    new Message(Role::ASSISTANT, new Contents([new TextContent('Hello, how can I help?')])),
    new Message(Role::USER, new Contents([new TextContent('I forgot my password.')])),
]);

$response = $client->request($messages);

use Descom\Ai\Bedrock\Messages\Contents\ImageContent;
use Descom\Ai\Bedrock\Messages\Contents\Sources\BinarySource;

$contents = new Contents([
    new TextContent('What is in this picture?'),
    new ImageContent('png', new BinarySource(file_get_contents('photo.png'))),
]);

$messages = new Messages([new Message(Role::USER, $contents)]);

$response = $client->request($messages);

use Descom\Ai\Tools\ToolsContract;

final class GetWeatherTool implements ToolsContract
{
    public function __construct(public object|array $arguments) {}

    public function run(object|array $parameters): bool|string|array
    {
        return "Sunny, 25C in {$parameters['location']}";
    }
}

public function tools(): array
{
    return [[
        'name' => 'get_weather',
        'description' => 'Get current weather for a location',
        'parameters' => [
            'type' => 'object',
            'properties' => [
                'location' => ['type' => 'string', 'description' => 'City name'],
            ],
            '

$response->output()->message->text();      // assistant text
$response->output()->message->toolUses();  // ToolUseContent[]
$response->stopReason()->value;            // 'end_turn', 'tool_use', ...
$response->usage()->inputTokens;
$response->usage()->outputTokens;
$response->usage()->totalTokens;
$response->metrics()->latencyMs;

use Descom\Ai\Bedrock\Converse\Exceptions\BedrockRequestException;

try {
    $response = $agent->ask('Hello');
} catch (BedrockRequestException $e) {
    Log::error('Bedrock call failed', [
        'message' => $e->getMessage(),
        'payload' => $e->payload,
    ]);
    throw $e;
}