PHP code example of dij-digital / langfuse-php

1. Go to this page and download the library: Download dij-digital/langfuse-php 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/ */

    

dij-digital / langfuse-php example snippets


use DIJ\Langfuse\PHP\Langfuse;
use DIJ\Langfuse\PHP\Transporters\HttpTransporter;
use GuzzleHttp\Client;

$langfuse = new Langfuse(
    transporter: new HttpTransporter(new Client([
        'base_uri' => 'https://cloud.langfuse.com',
        'auth' => ['PUBLIC_KEY', 'SECRET_KEY'],
    ])),
    environment: 'production', // optional, defaults to 'default'
);

// Get and compile prompts
$langfuse->prompt()->text(promptName: 'promptName')->compile(params: ['key' => 'value']);
$langfuse->prompt()->chat(promptName: 'chatName')->compile(params: ['key' => 'value']);

// List all prompts (returns a Generator that auto-paginates)
foreach ($langfuse->prompt()->list() as $prompt) {
    echo $prompt->name;
}

// Create a prompt
$langfuse->prompt()->create(promptName: 'promptName', prompt: 'text', type: PromptType::TEXT);

// Update prompt labels
$langfuse->prompt()->update(promptName: 'promptName', version: 1, labels: ['production']);

$ingestion = $langfuse->ingestion();

$trace = $ingestion->trace(
    name: 'my-workflow',
    userId: 'user-456',
    input: 'user question',
);

// Update the trace (sends immediately)
$trace->update(
    output: 'final answer',
    metadata: ['duration_ms' => 1234],
);

// Create a span from the trace
$span = $trace->span(name: 'web-search-batch');

// Nest a child span under the parent span
$childSpan = $span->span(name: 'single-search');

// Update spans when work is done
$childSpan->update(output: ['results' => 3], endTime: date('c'));
$span->update(output: ['total' => 3], endTime: date('c'));

// Generation on a trace
$gen = $trace->generation(
    input: ['messages' => [['role' => 'user', 'content' => 'Hello']]],
    output: 'Hi there!',
    name: 'llm-call',
    model: 'gpt-4o',
    modelParameters: ['temperature' => 0.7],
    promptName: 'my-prompt',
    promptVersion: 1,
);

// Generation nested under a span
$gen = $span->generation(
    input: 'summarize this',
    output: 'summary text',
    name: 'summarize-call',
    model: 'gpt-4o',
);

// Update a generation after the LLM responds
$gen->update(
    output: 'updated response',
    metadata: ['tokens' => 150],
);

$ingestion = $langfuse->ingestion();

$trace = $ingestion->trace(
    name: 'handle-request',
    userId: 'user-789',
    input: 'What is the weather?',
);

$span = $trace->span(name: 'search-batch');

    $child = $span->span(name: 'weather-api-call');
    $child->update(output: ['temp' => 22], endTime: date('c'));

    $span->generation(
        input: 'Summarize weather data',
        output: 'It is 22 degrees and sunny.',
        name: 'summarize',
        model: 'gpt-4o',
    );

$span->update(output: ['answer' => 'It is 22 degrees.'], endTime: date('c'));
$trace->update(output: 'It is 22 degrees and sunny.');

use DIJ\Langfuse\PHP;
use DIJ\Langfuse\PHP\Enums\ScoreDataType;

// Create a score
$score = $langfuse->score()->create(
    traceId: 'trace-id-123',
    name: 'accuracy',
    value: 0.95,
    dataType: ScoreDataType::NUMERIC,
    comment: 'High accuracy score'
);

// Get a specific score (using v2 API)
$score = $langfuse->score()->get('score-id-123');

// List scores with filters (using v2 API)
$scores = $langfuse->score()->list(
    traceId: 'trace-id-123',
    dataType: ScoreDataType::NUMERIC,
    limit: 10
);

// Delete a score
$langfuse->score()->delete('score-id-123');
bash
composer