yllw-digital/
laravel-chatgpt-mock-api
g4t.io

ChatGPT Mock API Generator for Laravel

138 stars by Paul Redmond

Generate smart API mocks in Laravel using ChatGPT prompts

README from yllw-digital/laravel-chatgpt-mock-api Open on GitHub →

Laravel ChatGPT Mock API Generator

Generate smart API mocks in Laravel using ChatGPT prompts.

Installation

composer require --dev yllw-digital/laravel-chatgpt-mock-api

You'll need to then publish the config file and migrations:

php artisan vendor:publish --provider="YellowDigital\LaravelChatgptMockApi\LaravelChatgptMockApiServiceProvider"

Run migrate to create the cache table:

php artisan migrate

Finally you'll need to add your OpenAI API key to your .env file:

OPENAI_API_KEY=sk-...

Usage

In your routes/api.php file, add the following:

use YellowDigital\LaravelChatgptMockApi\Facades\ChatGPTMockApi;

Route::get("/mock-response", function() {
    return ChatGPTMockApi::generate(
        prompt: "European countries and their national food",
        keys: [
            "id",
            "name",
            "food",
            "food_description",
        ],
        count: 3,
    );
});

This will generate a response like the following:

[
    {
        "id": 1,
        "name": "Italy",
        "food": "Pizza",
        "food_description": "A delicious round dough topped with tomato sauce, cheese and a variety of toppings."
    },
    {
        "id": 2,
        "name": "France",
        "food": "Croissants",
        "food_description": "A flaky, buttery pastry that is commonly eaten for breakfast or as a snack."
    },
    {
        "id": 3,
        "name": "Spain",
        "food": "Paella",
        "food_description": "A rice dish that originated in Valencia and is typically made with saffron, chicken, and shellfish."
    }
]

Caching

By default, the package will cache the generated responses in your DB so that you don't hit the OpenAI API limit. You can disable this by setting like so:

ChatGPTMockApi::generate(
    prompt: "European countries and their national food",
    keys: [
        "id",
        "name",
        "food",
        "food_description",
    ],
    count: 3,
    cache: false,
);

Changing ChatGPT model

By default, the package will use the gpt-3.5-turbo model. You can change this by setting like so:

ChatGPTMockApi::generate(
    prompt: "European countries and their national food",
    keys: [
        "id",
        "name",
        "food",
        "food_description",
    ],
    count: 3,
    model: "gpt-3.5-turbo-0301",
);

License

This package is open-sourced software licensed under the MIT license.

Credits

More from the ecosystem

marcreichel/
laya-php
g4t.io
3 53

LayaPHP: Self-Hosted Text Classification for PHP and Laravel

Classify text in PHP without an LLM bill: typed decisions in 100+ languages, self-hosted. Laravel-ready SDK for Laya, a Jev AI alternative.

ai classification decision-engine
marcreichel/laya-php via Laravel News
Josh-Dovey/
postcodes-laravel
g4t.io
5 9

Postcodes for Laravel: GB Postcode Lookup and Geography Data

Postcodes for Laravel adds typed GB postcode lookups, validation, geography data, distance searches, and test fakes through the GB Postcodes API.

Josh-Dovey/postcodes-laravel via Laravel News
RedberryProducts/
mailbox-for-laravel
g4t.io
4 115

Mailbox for Laravel: Preview and Test Rendered Email

Mailbox for Laravel captures outgoing mail in a local dashboard and lets you test rendered HTML, recipients, and attachments with fluent assertions.

RedberryProducts/mailbox-for-laravel via Laravel News