Jump24 - Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Laravel

openai-php/laravel image

Laravel stats

Downloads
428.6K
Stars
1,522
Open Issues
6
Forks
102

View on GitHub →

OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API

OpenAI PHP for Laravel

OpenAI PHP for Laravel is a powerful, community-maintained PHP API client tailored for Laravel users, enabling seamless integration with the OpenAI API. This tool is ideal for developers looking to utilize advanced AI features in their Laravel applications.

Key Features

  • Easy Installation: Quickly install via Composer and publish the configuration with Laravel's artisan commands.
  • Framework Integration: Directly integrates with Laravel, leveraging facades for simplified API usage.
  • Environment Configuration: Configure your OpenAI credentials and settings in the Laravel environment or config files.
  • Testing Support: Includes facilities to fake API responses and assert against expected API calls, which is great for development and testing environments.

Installation

  1. Install with Composer:

    composer require openai-php/laravel
  2. Publish Configuration File:

    php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"

    This step creates a config/openai.php file which you can customize.

  3. Set API Key in .env:

    OPENAI_API_KEY=sk-...

Usage Example

Utilize the OpenAI facade to interact with the OpenAI API:

use OpenAI\Laravel\Facades\OpenAI;
 
$result = OpenAI::completions()->create([
'model' => 'text-davinci-003',
'prompt' => 'PHP is',
]);
 
echo $result['choices'][0]['text']; // Outputs AI-generated text

Configuration Details

  • API Key & Organization: Essential for API requests, settable in .env or directly in the configuration file.
  • Request Timeout: Customizable timeout for API requests, default is 30 seconds.

Testing Facilities

The package provides a fake() method to mock API responses for testing:

use OpenAI\Laravel\Facades\OpenAI;
use OpenAI\Responses\Completions\CreateResponse;
 
OpenAI::fake([
CreateResponse::fake(['choices' => [['text' => 'awesome!']]]),
]);
 
$completion = OpenAI::completions()->create(['model' => 'text-davinci-003', 'prompt' => 'PHP is ']);
expect($completion['choices'][0]['text'])->toBe('awesome!');

This package is a must-have for developers looking to leverage AI in their Laravel applications, providing robust tools for both development and production environments.

openai-php photo

Makes OpenAI development in PHP more productive and enjoyable.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Openai Php Laravel Related Articles

Prompt Deck: Manage AI Prompts as Versioned Files in Laravel image

Prompt Deck: Manage AI Prompts as Versioned Files in Laravel

Read article
Lens for Laravel Brings WCAG Auditing to Your Local Dev Workflow image

Lens for Laravel Brings WCAG Auditing to Your Local Dev Workflow

Read article
Laravel Related Content: Semantic Relationships Using pgvector image

Laravel Related Content: Semantic Relationships Using pgvector

Read article
The Neuron AI Framework for PHP and Laravel image

The Neuron AI Framework for PHP and Laravel

Read article
PhpStorm 2025.3 brings bundled Laravel support, PHP 8.5, and new AI agents image

PhpStorm 2025.3 brings bundled Laravel support, PHP 8.5, and new AI agents

Read article
The Practical Guide to Laravel + Nova on OpenAI Codex Web image

The Practical Guide to Laravel + Nova on OpenAI Codex Web

Read article
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing