PHP code example of dayemsiddiqui / langfuse-sdk

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

    

dayemsiddiqui / langfuse-sdk example snippets


use dayemsiddiqui\Langfuse\Langfuse;

$langfuse = new Langfuse();

// Get a raw prompt
echo $langfuse->getPrompt('welcome')->raw();


// Compile a prompt with variables
echo $langfuse->getPrompt('greeting')->compile(['name' => 'Alice']);

$prompt = $langfuse->getPrompt('welcome')->raw();
// "Hello {{name}}"

$compiled = $langfuse->getPrompt('greeting')->compile(['name' => 'Alice']);
// "Hello Alice!"

try {
    $langfuse->getPrompt('profile')->compile(['name' => 'John']);
} catch (\dayemsiddiqui\Langfuse\Exceptions\MissingPromptVariablesException $e) {
    echo $e->getMessage();
}

use dayemsiddiqui\Langfuse\Testing\LangfuseFake;
use dayemsiddiqui\Langfuse\Langfuse;

LangfuseFake::fake([
    'greeting' => 'Hello {{name}}!',
    'farewell' => 'Goodbye {{name}}!'
]);

$langfuse = app(Langfuse::class);

// This will use the fake prompt, no HTTP requests!
echo $langfuse->getPrompt('greeting')->compile(['name' => 'Test']); // "Hello Test!"

LangfuseFake::getFake()->assertPromptRequested('greeting');
LangfuseFake::getFake()->assertPromptRequestedTimes('greeting', 1);
LangfuseFake::getFake()->assertNoPromptsRequested();

LangfuseFake::fake()
    ->addPrompt('foo', 'Foo {{bar}}')
    ->addPrompt('baz', 'Baz {{qux}}')
    ->throwOnMissing();

use dayemsiddiqui\Langfuse\Testing\WithLangfuseFake;

uses(WithLangfuseFake::class);

it('fakes prompts', function () {
    $fake = $this->getLangfuseFake();
    $fake->addPrompt('test', 'Test prompt');
    $langfuse = app(Langfuse::class);
    expect($langfuse->getPrompt('test')->raw())->toBe('Test prompt');
});