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 Prism's provided methods.
You can use system prompts to guide the AI model to behave in a certain way. For example, you can use a system prompt to tell the AI model to behave like a Laravel developer who explains concepts simply.
$response = Prism::text()
->using('openai', $this->model)
// Adjust this as needed to guide the AI model to behave in a certain way.
->withSystemPrompt('You are an expert Laravel developer who explains concepts simply.')
->withPrompt($this->prompt)
->asText();
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.