Git Product home page Git Product logo

chatgpt-library's Introduction

OpenAI Api integration

A PHP class that simplifies interactions with the OpenAI API. Includes functions to generate text, generate images and convert text to speech.

Text Generation

Generates a text response based on the given prompt using the specified parameters.

API Documentation: https://platform.openai.com/docs/guides/text-generation

Usage

$prompt The prompt for generating the text response. Either a string or an array of prompts.

$model The gpt-3.5-turbo model to use for text generation. Available models: gpt-4, gpt-3.5-turbo.

$temperature The temperature parameter for controlling randomness (default: 0.7).

$maxTokens The maximum number of tokens in the generated text (default: 1000).

Result: An array containing 'data' and 'error' keys, representing the generated text and any errors.

<?php 
echo $ai->textGeneration('Who is Tristan White?')->choices[0]->message->content; 
echo $ai->textGeneration('Who is Tristan White?','gpt-3.5-turbo')->choices[0]->message->content; 
?>

Example

<?php 
echo "<p>".$ai->textGeneration('Who is tristan white?')->choices[0]->message->content."</p>"; 
$chats = [
    ["role" => "system", "content" => "You are a helpful assistant." ], 
    ["role" => "user", "content" => "Who won the world series in 2020?" ], 
    ["role" => "assistant", "content" => "The Los Angeles Dodgers won the World Series in 2020." ], 
    ["role" => "user", "content" => "Where was it played?" ] 
];
echo "<p>".$ai->textGeneration($chats)->choices[0]->message->content."</p>"; 
?>

Image Generation

Generates an image based on the given prompt using the specified parameters.

API Documentation: https://platform.openai.com/docs/guides/images/usage?context=node

Usage

$prompt The prompt for generating image.

$size The size of the generated image. Available size: 1024x1024, 1024x1792 or 1792x1024 pixels.

$model The dall-e-3 model to use for image generation. Available model: dall-e-3.

Result: An array containing 'data' and 'error' keys, representing the generated text and any errors.

<?php 
$ai->imageGeneration('Draw Tristan White as a magnificent robot'); 
$ai->imageGeneration('Draw Tristan White as a magnificent robot','1024x1792');
?>

Example

<?php 
$image = $ai->imageGeneration('Draw Tristan White as a magnificent robot'); 
echo '<img src="'.$image->data[0]->url.'" alt="'.$image->data[0]->revised_prompt.'">';
?>

Text to Speech

Generates an audio file based on the given prompt using the specified parameters.

API Documentation: https://platform.openai.com/docs/guides/text-to-speech

Usage

$prompt The prompt for generating the text to audio.

$output The absolute path to the about of the audio file. E.g. /path/to/my/audio.mp3

$voice The voice of the output. Available voices: alloy, echo, fable, onyx, nova, and shimmer.

$model The tts-1 model to use for text to audio translation. Available model: tts-1, tts-1-hd.

Result: file containing the audio data. The default response format is "mp3"

<?php 
$ai->textToSpeech('Who is Tristan White?','/path/to/my/audio.mp3');
$ai->textToSpeech('Who is Tristan White?','/path/to/my/audio.mp3','onyx');
?>

Example

<?php
$path = "audio.mp3";
$ai->textToSpeech('Who is Tristan White?',$path);
echo '<audio autoplay><source src="'.$path.'" type="audio/mpeg"></audio>';
?>

chatgpt-library's People

Contributors

triss90 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.