PHP code example of manik / neuro

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

    

manik / neuro example snippets


use Neuro::vector()->driver()->defaultCollection(); // resolves the fallback chain
Neuro::vector()->upsert('my_collection', $records); // explicit name
Neuro::vector()->upsert(null, $records);            // uses config fallback

$response = Neuro::chat()
    ->provider('openai')
    ->model('gpt-4o')
    ->message('Explain Laravel')
    ->options(['temperature' => 0.3])
    ->chat();

$response = Neuro::chat()
    ->message(['role' => 'system', 'content' => 'You are a helpful assistant.'])
    ->message('Explain Laravel')
    ->chat();

use Manik\Neuro\Facades\Neuro;

$response = Neuro::chat()
    ->provider('openai')
    ->model('gpt-4o')
    ->message('Explain Laravel to a beginner')
    ->chat();

// $response['content'] => string
// $response['role'] => 'assistant'

$stream = Neuro::chat()
    ->provider('openai')
    ->message('Write a poem about Laravel')
    ->stream();

foreach ($stream as $chunk) {
    echo $chunk['content'];
    ob_flush();
    flush();
}

$result = Neuro::chat()
    ->text('The text to embed')
    ->embed('openai');

// $result['embedding'] => array of floats
// $result['dimensions'] => int

$results = Neuro::chat()
    ->embedBatch(['text one', 'text two', 'text three'], 'openai');

$results = Neuro::vector()
    ->driver('qdrant')
    ->search('my_collection', $vector, ['top_k' => 10]);

$driver = Neuro::llm()->driver('openai');
$response = $driver->chat([['role' => 'user', 'content' => 'Hello!']]);

$response = Neuro::chat()
    ->provider('openai')
    ->model('gpt-4o')
    ->message('What is the weather in Paris?')
    ->tools([
        [
            'type' => 'function',
            'function' => [
                'name' => 'get_weather',
                'description' => 'Get the weather for a city',
                'parameters' => [
                    'type' => 'object',
                    'properties' => [
                        'city' => ['type' => 'string'],
                    ],
                ],
            ],
        ],
    ]);

$response = Neuro::chat()
    ->provider('ollama')
    ->model('llama3')
    ->message('Hello, how are you?')
    ->chat();

$vector = Neuro::embedding()
    ->driver('openai')
    ->embed('Your text here');

// Get a vector driver
$vector = Neuro::vector()->driver('qdrant');

// Create a collection
$vector->createCollection('knowledge', 1536);

// Upsert vectors
$vector->upsert('knowledge', [
    [
        'id' => '1',
        'vector' => [0.1, 0.2, ...],
        'payload' => ['text' => 'Some content', 'source' => 'docs'],
    ],
]);

// Search
$results = $vector->search('knowledge', [0.1, 0.2, ...], [
    'top_k' => 10,
    'filter' => ['source' => 'docs'],
]);

// Delete
$vector->delete('knowledge', '1');

$response = Neuro::rag()
    ->collection('knowledge_base')
    ->question('What is Laravel?')
    ->answer();

// $response['answer'] => string (the LLM's answer with context)
// $response['sources'] => array (the retrieved chunks)
// $response['tokens'] => array (token usage)

$pipeline = Neuro::rag()->pipeline();

$response = $pipeline
    ->collection('knowledge_base')
    ->question('What is Laravel?')
    ->topK(10)
    ->minScore(0.5)
    ->answer();

// Just search without LLM
$results = $pipeline->search();

$agent = Neuro::agent('openai')
    ->session('user-123')
    ->maxSteps(5)
    ->tool('get_time', function () {
        return now()->toDateTimeString();
    }, 'Get the current date and time')
    ->tool('calculate', function (float $a, string $op, float $b) {
        return match ($op) { '+' => $a + $b, '-' => $a - $b, '*' => $a * $b, '/' => $a / $b };
    }, 'Perform a calculation');

$result = $agent->run('What time is it?');
// $result['response'] => string
// $result['steps'] => int

use Manik\Neuro\Facades\Neuro;

$driver = Neuro::llm()->driver('openai');
$driver->tools($messages, [
    [
        'type' => 'function',
        'function' => [
            'name' => 'search_web',
            'description' => 'Search the web for information',
            'parameters' => [
                'type' => 'object',
                '

// Using the memory manager
Neuro::memory()->driver('session')->add('session-1', [
    'role' => 'user',
    'content' => 'Hello!',
]);

$history = Neuro::memory()->driver('session')->get('session-1');
// Returns array of messages, limited by config

Neuro::memory()->driver('session')->clear('session-1');

// Requires running the migration
Neuro::memory()->driver('persistent')->add('user-456', [
    'role' => 'user',
    'content' => 'Remember my name is John',
]);

$history = Neuro::memory()->driver('persistent')->get('user-456');

$result = Neuro::image()
    ->driver('openai')
    ->generate('A serene mountain landscape at sunset', [
        'size' => '1024x1024',
        'quality' => 'hd',
    ]);

// $result['url'] => string
// $result['revised_prompt'] => string

$result = Neuro::image()
    ->driver('openai')
    ->edit('/path/to/image.png', 'Add a rainbow to the sky');

$result = Neuro::image()
    ->driver('openai')
    ->variations('/path/to/image.png', ['n' => 3]);

$audioContent = Neuro::speech()
    ->driver('openai')
    ->synthesize('Hello, welcome to Laravel AI!', [
        'voice' => 'alloy',
        'model' => 'tts-1',
    ]);

// Save to file
Storage::put('audio/welcome.mp3', $audioContent);

$transcription = Neuro::speech()
    ->driver('openai')
    ->transcribe('/path/to/audio.mp3');

// $transcription['text'] => string

Neuro::rag()->ingestion()
    ->ingestFromPath(storage_path('docs/laravel-intro.md'), 'knowledge_base');

Neuro::rag()->ingestion()
    ->ingestRaw('# Laravel\nLaravel is a PHP framework...', 'knowledge_base', [
        'source' => 'manual',
        'author' => 'John',
    ]);

use Manik\Neuro\RAG\Chunking\SemanticChunking;

Neuro::rag()->ingestion()
    ->setChunkStrategy(new SemanticChunking)
    ->ingestRaw($markdownContent, 'docs');

use Manik\Neuro\Facades\Neuro;

// Enable fake mode
Neuro::fake();

// All chat calls now return fake responses
$response = Neuro::chat()
    ->message('This will not hit the API')
    ->chat();

// $response['content'] === 'fake response'

Neuro::fake();

$result = Neuro::chat()
    ->text('Test text')
    ->embed('openai');

// Returns zeroed-out embedding vector with 1536 dimensions

use Manik\Neuro\Events\MessageReceived;

Event::listen(MessageReceived::class, function (MessageReceived $event) {
    Log::info('LLM call completed', [
        'provider' => $event->provider,
        'model' => $event->model,
        'latency' => $event->latency,
    ]);
});

// config/neuro.php
'observability' => [
    'track_cost' => env('AI_TRACK_COST', false),
    'track_tokens' => env('AI_TRACK_TOKENS', false),
    'track_latency' => env('AI_TRACK_LATENCY', false),
    'store' => env('AI_OBSERVABILITY_STORE', 'log'),
],

// Custom LLM driver
Neuro::llm()->extend('my-provider', function ($app) {
    return new MyCustomDriver(config('ai.llm.my-provider'));
});

// Custom embedding driver
Neuro::embedding()->extend('my-embedder', function ($app) {
    return new MyEmbedder(config('ai.embedding.my-embedder'));
});

// Custom vector driver
Neuro::vector()->extend('my-vector-db', function ($app) {
    return new MyVectorDB(config('ai.vector.my-vector-db'));
});

$response = Neuro::chat()
    ->provider('my-provider')
    ->message('Hello')
    ->chat();
bash
php artisan vendor:publish --tag=neuro-config
bash
php artisan migrate