PHP code example of hexgenerative / ai

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

    

hexgenerative / ai example snippets



HexaGenerative\HexaAI;

$client = new HexaAI('hgx-your-api-key');

$response = $client->chat->completions->create([
    'model' => 'hexa-pro',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello, who are you?']
    ]
]);

echo $response->getContent();

// By task type
$response = $client->chat->completions->create([
    'task' => 'coding',
    'messages' => [
        ['role' => 'user', 'content' => 'Write a PHP function']
    ]
]);

// By optimization
$response = $client->chat->completions->create([
    'optimize_for' => 'speed',
    'messages' => [
        ['role' => 'user', 'content' => 'Quick answer please']
    ]
]);

use HexaGenerative\HexaAI;
use HexaGenerative\HexaAIException;

try {
    $response = $client->chat->completions->create([
        'model' => 'hexa-pro',
        'messages' => [['role' => 'user', 'content' => 'Hello']]
    ]);
} catch (HexaAIException $e) {
    echo "Error: " . $e->getMessage();
    echo "Status: " . $e->getStatusCode();
}

$result = $client->agent->run([
    'task' => 'Research AI trends and summarize',
    'model' => 'hexa-ultra',
]);
print_r($result);

// Upload document
$client->rag->upload('Company Policy', 'Employees get 30 days leave...');

// Search
$results = $client->rag->search('leave policy');

$session = $client->context->create('You are a helpful assistant');
$sessionId = $session['data']['session_id'];

$client->context->add($sessionId, ['role' => 'user', 'content' => 'Hello!']);

$result = $client->code->execute('print(sum(range(100)))');
echo $result['data']['output'];

$tools = $client->tools->list();
echo "Found " . $tools['data']['count'] . " tools";