PHP code example of haro / aitracer

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

    

haro / aitracer example snippets




use AITracer\AITracer;

// 初期化
$tracer = new AITracer([
    'api_key' => 'at-xxxxx',  // または環境変数 AITRACER_API_KEY
    'project' => 'my-project', // または環境変数 AITRACER_PROJECT
]);

// 手動ログ記録
$tracer->log([
    'model' => 'gpt-4',
    'input' => ['messages' => [['role' => 'user', 'content' => 'Hello']]],
    'output' => ['content' => 'Hi there!'],
    'input_tokens' => 10,
    'output_tokens' => 5,
    'latency_ms' => 500,
]);



use AITracer\AITracer;
use OpenAI;

$tracer = new AITracer([
    'api_key' => 'at-xxxxx',
    'project' => 'my-project',
]);

// OpenAIクライアントを作成
$openai = OpenAI::client('sk-...');

// AITracerでラップ(自動ログ記録が有効になる)
$traced = $tracer->wrapOpenAI($openai);

// 通常通りAPI呼び出し(自動的にログ記録される)
$response = $traced->chat()->completions()->create([
    'model' => 'gpt-4',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello, how are you?'],
    ],
]);



use AITracer\AITracer;

$tracer = new AITracer([
    'api_key' => 'at-xxxxx',
    'project' => 'my-project',
]);

// Anthropicクライアントをラップ
$traced = $tracer->wrapAnthropic($anthropic);

$response = $traced->messages()->create([
    'model' => 'claude-3-opus-20240229',
    'max_tokens' => 1024,
    'messages' => [
        ['role' => 'user', 'content' => 'Hello, Claude!'],
    ],
]);



// トレースを開始
$trace = $tracer->trace('conversation-123', 'Customer Support Chat')
    ->setMetadata(['customer_id' => 'cust_001'])
    ->addTags(['support', 'premium']);

// このトレース内のすべてのAPI呼び出しは同じtrace_idでグループ化される
$response1 = $traced->chat()->completions()->create([...]);
$response2 = $traced->chat()->completions()->create([...]);

// トレースを終了
$tracer->endTrace();



$tracer = new AITracer([
    'api_key' => 'at-xxxxx',
    'project' => 'my-project',
    'pii_detection' => true,
    'pii_action' => 'mask',  // mask, redact, hash, none
    'pii_types' => ['email', 'phone', 'credit_card', 'ssn'],
]);

// ログ内のPIIは自動的にマスクされる
// "Contact [email protected]" → "Contact [email]"



$tracer = new AITracer([
    'api_key' => 'at-xxxxx',
    'project' => 'my-project',
    'sync' => true,  // 即座に送信
]);



// 未送信のログをすべて送信
$tracer->flush();

// シャットダウン(フラッシュを含む)
$tracer->shutdown();

$tracer->log([
    'model' => 'gpt-4',                    // モデル名
    'provider' => 'openai',                // プロバイダー(自動検出可)
    'input' => [...],                      // 入力データ
    'output' => [...],                     // 出力データ
    'input_tokens' => 100,                 // 入力トークン数
    'output_tokens' => 50,                 // 出力トークン数
    'latency_ms' => 1234,                  // レイテンシ(ミリ秒)
    'status' => 'success',                 // success または error
    'error_message' => null,               // エラーメッセージ
    'metadata' => ['key' => 'value'],      // カスタムメタデータ
    'tags' => ['production', 'chat'],      // タグ
    'trace_id' => 'xxx',                   // トレースID
    'session_id' => 'session-123',         // セッションID
    'user_id' => 'user-456',               // ユーザーID
]);
bash
composer