Simple AI Integration
We have provided a simple example of how to use the AI integration in your application. This example is a simple text generation request to the OpenAI API.
It's a Livewire component that will send a request to the OpenAI API and dump the response to the browser.
Response Handling
The component will send a request to the OpenAI API and dump the response to the browser using the Laravel AI SDK.
You can use instructions to guide the AI model to behave in a certain way. For example, you can tell the AI model to behave like a Laravel developer who explains concepts simply.
use function Laravel\Ai\agent;
$response = agent(
instructions: 'You are an expert Laravel developer who explains concepts simply.',
)->prompt($this->prompt, model: $this->model);
Usage Tracking
Token usage is stored using helpers in app/Helpers/ai_helpers.php and the AiUsage
model (Learn more about AI Usage):
ai_save_usage($userId, $model, $promptTokens, $outputTokens);
// Calculates cost with ai_cost_for_model_tokens() and stores a row in ai_usages
Related Routes
| Method | Path | Route name |
|---|---|---|
| GET | /ai-simple | ai.simple |
Read Next
- AI Chat with streaming for streamed responses.
- AI Usage for cost and reporting details.