Getting Started with the Laravel AI SDK

Laravel AI SDK হলো AI-powered application feature তৈরি করার জন্য Laravel-এর first-party package, যেখানে provider-specific HTTP call ছড়িয়ে না দিয়ে একটি consistent Laravel-friendly API ব্যবহার করা যায়। Provider configure করা, agent define করা, model call করা, tool ব্যবহার করা, structured output নেওয়া এবং usage track করা—সবকিছু একই ecosystem-এর ভেতরে আসে।
গুরুত্বপূর্ণ mental shift হলো: SDK নিজে product feature নয়। এটি integration layer। Use case, authorization, validation, persistence, queue, log এবং user experience—এসব Laravel application-এর দায়িত্বেই থাকে। SDK সেই responsibility-গুলোকে AI provider-এর সঙ্গে connect করার Laravel-native পথ দেয়।
Laravel AI SDK কী দেয়
SDK-এর আগে অনেক Laravel application raw HTTP client, direct provider SDK অথবা ছোট wrapper service দিয়ে AI integrate করত। Prototype-এর জন্য কাজ করলেও product বড় হলে multiple provider, conversation context, structured output, tool, streaming, cost tracking, testing এবং failover manage করা কঠিন হয়ে যায়।
Provider abstraction
OpenAI, Anthropic, Gemini, Groq, Mistral, DeepSeek, xAI, Ollama, Azure OpenAI, OpenRouter এবং OpenAI-compatible endpoint-এর সঙ্গে consistent Laravel API দিয়ে কাজ করা যায়।
Agents
Agent instruction, context, tool, model configuration এবং optional output schema-কে dedicated PHP class-এর ভেতরে রাখে। Prompt controller জুড়ে ছড়িয়ে থাকে না।
Structured output
Application যদি prose নয়, data চায়, SDK schema-based output চাইতে পারে, যেটি Laravel ব্যবহারের আগে validate করতে পারে।
Tool access
Agent controlled tool ব্যবহার করতে পারে, কিন্তু actual operation Laravel code-এর মধ্য দিয়েই যায়, যেখানে authorization এবং validation থাকে।
Operational hooks
Usage data, raw response, event, testing helper, failover, streaming এবং queue feature-টিকে demo থেকে production path-এ নিয়ে যায়।
Package install এবং configuration publish করা
Laravel AI SDK setup সাধারণ Laravel package-এর মতোই শুরু হয়: package require করুন, configuration ও migration publish করুন, এরপর conversation storage ব্যবহার করলে migration run করুন।
composer require laravel/ai php artisan vendor:publish --provider="Laravel\Ai\AiServiceProvider" php artisan migrateMigration step গুরুত্বপূর্ণ, কারণ remembered conversation-এর মতো feature database table ব্যবহার করে। আপনার প্রথম feature stateless classification বা summarization হলেও configuration শুরুতেই publish করলে provider ও model choice visible থাকে।
Environment variable দিয়ে provider configure করা
Provider credential environment variable এবং configuration-এ থাকা উচিত; prompt, controller, job বা agent class-এর ভেতরে নয়। এতে deployment, rotation এবং provider switching সহজ হয়।
OPENAI_API_KEY=ANTHROPIC_API_KEY=GEMINI_API_KEY=GROQ_API_KEY=MISTRAL_API_KEY=OPENROUTER_API_KEY=OPENAI_COMPATIBLE_API_KEY=OPENAI_COMPATIBLE_URL=প্রথম production habit হিসেবে environment অনুযায়ী default provider ও model define করা ভালো। Local development cheap বা local model ব্যবহার করতে পারে। Staging stable low-cost model ব্যবহার করতে পারে। Production predictable behavior ও pricing-এর জন্য specific model pin করতে পারে।
প্রথম agent তৈরি করা
SDK-এর main application-facing concept হলো agent। Agent হলো নির্দিষ্ট responsibility-সহ একটি PHP class। প্রথম feature হিসেবে ভাবুন, SupportSummaryAgent একটি দীর্ঘ support message থেকে internal support team-এর জন্য short summary তৈরি করবে।
php artisan make:agent SupportSummaryAgent<?php namespace App\Ai\Agents; use Laravel\Ai\Attributes\MaxTokens;use Laravel\Ai\Attributes\Temperature;use Laravel\Ai\Contracts\Agent;use Laravel\Ai\Promptable; #[MaxTokens(600)]#[Temperature(0.2)]class SupportSummaryAgent implements Agent{ use Promptable; public function instructions(): string { return <<<'PROMPT'You summarize customer support messages for an internal Laravel support team. Write concise summaries.Keep facts from the original message.Do not invent account status, payment state, or technical causes.If the message is unclear, say what is missing.PROMPT; }}এখানে instruction শুধু text নয়; এটি feature boundary। Agent summarize করতে পারে এবং missing information identify করতে পারে, কিন্তু account status বা technical root cause invent করতে পারবে না। এই ধরনের language application contract-এর অংশ।
Application code থেকে agent call করা
Controller এমন জায়গা হওয়া উচিত নয় যেখানে prompt, provider decision, validation, persistence এবং response formatting সব একসঙ্গে মিশে যায়। HTTP layer thin রাখুন এবং use case-এর ownership service বা action-এ রাখুন।
<?php namespace App\Services; use App\Ai\Agents\SupportSummaryAgent;use App\Models\SupportTicket; class SummarizeTicket{ public function handle(SupportTicket $ticket): string { $prompt = <<<TEXTSummarize this support ticket for an internal support agent. Subject: {$ticket->subject}Message:{$ticket->message}TEXT; $response = (new SupportSummaryAgent)->prompt($prompt); return trim((string) $response->content); }}Prompt-এ task-এর জন্য যতটুকু data দরকার শুধু ততটুকুই পাঠান। Summary-এর জন্য billing history দরকার না হলে billing history পাঠাবেন না। User যে record access করতে পারে না, model-ও সেই record receive করা উচিত নয়।
Anonymous agent শুধু experiment-এর জন্য ব্যবহার করুন
SDK `agent()` helper দিয়ে anonymous agent support করে। Experiment, prototype অথবা one-off internal script-এর জন্য এটি useful। কিন্তু product feature-এর জন্য named class সাধারণত review, test, version এবং observe করা সহজ।
use function Laravel\Ai\agent; $response = agent( instructions: 'You explain Laravel concepts to PHP developers.',)->prompt('Explain service containers in three bullet points.');প্রথম দিন থেকেই usage track করা
AI feature নতুন operational dimension তৈরি করে: token usage। একটি feature correct হতে পারে, কিন্তু খুব expensive-ও হতে পারে। SDK response provider-reported usage information expose করে, যেমন input token, output token এবং total token।
$response = (new SupportSummaryAgent)->prompt($prompt); logger()->info('support_ticket_summary.generated', [ 'ticket_id' => $ticket->id, 'input_tokens' => $response->usage?->inputTokens, 'output_tokens' => $response->usage?->outputTokens, 'total_tokens' => $response->usage?->totalTokens(),]); return trim((string) $response->content);কমপক্ষে provider, model, prompt version, feature name, response time, token usage, validation result এবং failure reason log করুন। পরে এই log practical question-এর উত্তর দেবে: কোন customer বেশি cost করছে, কোন prompt version slow হয়েছে, কোন provider বেশি fail করছে, অথবা cheaper model যথেষ্ট কি না।
প্রথম feature boundary design করা
ভালো প্রথম Laravel AI SDK feature-এর boundary boring হওয়াই ভালো। এটি trusted application data নেয়, minimal context পাঠায়, limited result চায়, result validate বা review করে, এবং পরে debug করার জন্য metadata store করে।
- Authorization model call-এর আগে হবে, পরে নয়।
- Prompt সম্ভব হলে raw untrusted request payload নয়, application data ব্যবহার করবে।
- Output draft, classification, suggestion অথবা structured result হিসেবে treat হবে; final business decision হিসেবে নয়।
- Provider timeout বা invalid output হলে feature-এর fallback থাকবে।
- Model এবং prompt version log-এ visible থাকবে।
- Sensitive data prompt-এ যাওয়ার আগে minimize করা হবে।
যেমন ticket summary fail করলে “summary unavailable” দেখিয়ে support agent original ticket পড়তে পারে। কিন্তু payment decision fail-open করা যায় না। প্রথম feature এমন হওয়া উচিত, যা team-কে AI behavior শেখাবে কিন্তু core business state risk-এ ফেলবে না।
First agent-এর পরে কোথায় যাবেন
প্রথম feature কাজ করলেই next step “আরও AI” নয়। Next step হলো একই narrow use case-এর চারপাশে ভালো engineering: structured output, test, retry, provider failover, prompt versioning, queueing এবং user feedback।
এই episode আপনাকে first working shape দিল: SDK install করা, provider configure করা, agent তৈরি করা, Laravel code থেকে call করা এবং result observe করা। পরের episode-এ agent নিয়ে আরও গভীরে যাব: agent-এর ভেতরে কী থাকবে, Laravel service-এ কী থাকবে, tool কীভাবে loop-এ আসে এবং feature বড় হলে agent behavior কীভাবে understandable রাখা যায়।
এই article-এর লেখা পরিমার্জন, formatting এবং Bangla translation-এ ChatGPT সহায়তা করেছে।
