asciito/simple-generators

A very simple package to handle text and image generation from AI tools

v0.4.1 2024-03-15 21:26 UTC

This package is auto-updated.

Last update: 2024-05-15 21:53:31 UTC


README

A very simple package to handle text and image generation from AI tools

Badge of the supported PHP versions Badge for licence Badge of current version Badge to show if the test passed

Description

The package is straightforward, enables you to generate either text or an image, based on a text prompt, utilizing different AI providers.

Get Started

Requires PHP ^8.2


First, install simple-generators via Composer:

composer require asciito/simple-generators

And, that's all, you can start using the package without any problem.

Usage

Our generator system (text & image) operates through Clients. These are classes implementing the \Asciito\SimpleGenerators\Providers\Contracts\Client interface. You have the freedom to choose between different clients or create your own.

  • Available Clients: At the moment, only OpenAI is available.
  • Creating Your Own Client: You have the option to develop your own clients. Please refer to the section Create your own client for details on how to accomplish this.

In this documentation, our primary focus is on the OpenAI client.

Generate Text with: Factory::text(string $client = null, array $options = [])

This section will guide you through the usage of the text generator to create textual content.

use Asciito\SimpleGenerators\Factory;

$generator = Factory::text(options: ['api' => 'this-is-a-key-for-open-ai']);

echo $generator->prompt('Give me a motivational phrase');

// for example: "Believe in yourself and all your dreams will come true".

Generate Image with: Factory::image(string $client = null, array $options = [])

This section will guide you through the usage of the image generator to crate images.

use Asciito\SimpleGenerators\Factory;

$generator = Factory::image(options: ['api' => 'this-is-a-key-for-open-ai']);

echo $generator->prompt('A cat floating in space riding a horse with a France flag');

// For example, the URL might be: "https://oaidalleapiprodscus.blob.core.windows.net/private/rA23htPg4..."

Note: If you omit the first parameter (in both static methods), the generator provider will fall back to OpenAI client, so be sure to give the correct options for the OpenAI client.

Create your own client

This is a work in progress, because right now, there's no way to pass your class to the text or image generators

Put in simple words, you just need to implement the contract from \Asciito\SimpleGenerator\Providers\Contracts\Client, this interface have 3 methods that should to be implemented.

enerators\Clients\Contracts;

interface Client
{
    public function __construct(array $options = []);
    
    public function generateText(string $prompt): string;

    public function generateImage(string $prompt, string $size = null): string;
}

Method __construct(array $options = [])

The __construct method accepts a key-value array with the options that your client might use at some point; the implementation is up to you.

Method generateText(string $prompt)

The generateText method accepts a string which is the text prompt that would be used to make the request to your AI provider. It should return a string representing the response from your provider.

Method generateImage(string $prompt, string $size = null)

The generateImage method accepts a string which is the text prompt you should use to make the request to your AI provider. However, you should ultimately return a string that represents the response from your provider.

Providers

List of every AI providers, and what options do they need to work.

Open AI

The Open AI provider just requires one option, the api option, which will be used to call the API and do its magic.

$options = ['api' => 'this-is-your-api-key'];

This option should be passed to the Factory::text or Factory::image every time you create a new generator.

License

This project is licensed under the MIT License - see the LICENSE.md file for details