PHP code example of modfin / bellman

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

    

modfin / bellman example snippets


use Bellman\Client;
use Bellman\Models\Anthropic;
use Bellman\Models\OpenAI;

$bellman = new Client(getenv('BELLMAN_URL'), 'my-app', getenv('BELLMAN_TOKEN'));
// or, with a combined key: Client::fromKey(getenv('BELLMAN_URL'), getenv('BELLMAN_KEY'))

$res = $bellman->generator()
    ->model(OpenAI::GenModel_gpt4o_mini())
    ->prompt('What company made you?');

echo $res->text(); // OpenAI

$llm = $bellman->generator()
    ->model(Anthropic::GenModel_5_5_sonnet_latest())
    ->system('You are a helpful assistant. Answer briefly.')
    ->temperature(0.2)
    ->maxTokens(1000);

$a = $llm->prompt('Why is the sky blue?');
$b = $llm->temperature(1.0)->prompt('Write a haiku about Stockholm.'); // $llm is unchanged

$res = $llm
    ->output([
        'type' => 'object',
        'properties' => [
            'name' => ['type' => 'string'],
            'founded' => ['type' => 'integer'],
        ],
        '

use Bellman\Prompt\Mime;
use Bellman\Prompt\Prompt;

$res = $llm->prompt(
    Prompt::asUserWithData(Mime::APPLICATION_PDF, file_get_contents('report.pdf')),
    'Summarize this report in three bullet points.',
);

$history = [Prompt::asUser('Pick a random Nordic capital.')];
$res = $llm->prompt(...$history);

$history = [...$history, ...$res->turn, Prompt::asUser('What is its population?')];
$res = $llm->prompt(...$history);

use Bellman\Embed\Type;
use Bellman\Models\VoyageAI;

$model = VoyageAI::EmbedModel_voyage_3_5();

$vector = $bellman->embed($model->withType(Type::QUERY), 'search query')->single();
$vectors = $bellman->embed($model->withType(Type::DOCUMENT), ['first text', 'second text'])->embeddings;

// Contextualized chunk embeddings
$chunks = $bellman->embedDocument(VoyageAI::EmbedModel_voyage_context_4(), $documentChunks)->embeddings;

OpenAI::GenModel_gpt4o_mini();
VertexAI::EmbedModel_text_005();
OpenAI::genModels();              // all OpenAI generative models
Catalog::gen('OpenAI/gpt-4o');    // look up by "provider/name", null if unknown

use Bellman\Gen\Model;

new Model('VertexAI', 'gemini-2.5-flash', config: ['region' => 'us-central1']);
Model::fromFqn('OpenAI/some-new-model');

$res->metadata->inputTokens;
$res->metadata->outputTokens;
$res->metadata->totalTokens;

$bellman = new Client($url, 'my-app', $token, http: $myPsr18Client);

// config/services.php
'bellman' => [
    'url' => env('BELLMAN_URL'),
    'name' => env('BELLMAN_KEY_NAME', 'my-app'),
    'token' => env('BELLMAN_TOKEN'),
],

// AppServiceProvider::register()
$this->app->singleton(\Bellman\Client::class, fn () => new \Bellman\Client(
    config('services.bellman.url'),
    config('services.bellman.name'),
    config('services.bellman.token'),
));