DEV Community

Amit Gupta
Amit Gupta

Posted on

Generating AI-Powered Content in Laravel 13 Using OpenAI

As AI becomes part of modern web applications, one feature I've been working on is automatically generating product content for an e-commerce application built with Laravel 13.

Instead of manually writing content for every product, I'm using the OpenAI API to generate everything in a single request.

The generated content includes:

  • Product Description
  • Short Product Description
  • SEO Meta Title
  • SEO Meta Description
  • SEO Meta Keywords

Why a Single API Request?

Initially, I considered making separate API calls for each field, but that quickly increases response time and API costs.

Instead, I generate everything in a single request.

This approach provides several advantages:

  • Fewer API calls
  • Faster response time
  • Lower token usage
  • More consistent content across all generated fields

Laravel Implementation

The implementation is straightforward using Laravel's HTTP Client.

$response = Http::withToken(config('services.openai.key'))
->post('https://api.openai.com/v1/chat/completions', [
'model' => env('OPENAI_MODEL'),
'response_format' => ['type' => 'json_object'],
'messages' => $messages,
]);

Using structured JSON responses makes it easy to validate and store each field independently.

Current Workflow

My current workflow looks like this:

  • User enters product information.
  • Laravel sends a single request to the OpenAI API.
  • The model returns structured JSON.
  • Each field is validated.
  • The generated content is saved for review.

Challenges

While this approach works well, there are still several areas I'm improving.

  • Response caching where appropriate
  • Queueing bulk generation jobs
  • Human review before publishing
  • Validation of AI-generated content
  • Better handling of inaccurate or misleading product details

One thing I've learned is that AI should help speed up content creation—not completely replace human review for important product information.

Looking for Suggestions

I'd love to hear how other developers are approaching similar problems.

  • Are you generating structured JSON or free-form text?
  • Do you use a second LLM for verification?
  • How are you handling large product catalogs?
  • Any techniques you've found useful for reducing API costs?

I'm always interested in learning how others are solving these challenges.

Laravel 13 AI-Powered E-commerce Series

I'm documenting this implementation step by step as part of my Laravel 13 AI-Powered E-commerce series on the Stack Developers YouTube channel, where I'm building the complete application from scratch, including AI features, OpenAI integration, Filament admin panel, and more.

If you've built something similar, I'd love to hear your experience or suggestions in the comments.

Top comments (0)