Giter VIP home page Giter VIP logo

laravel-chatgpt's Introduction

๐Ÿˆ Laravel Chat for ChatGPT

Packagist Version GitHub License Packagist Downloads

Documentation

This libray need openai php client, so don't forget to add in your .env this variables:

# .env

OPENAI_API_KEY=
OPENAI_API_ORGANIZATION=
OPENAI_MODEL=gpt-4

Installation:

To install the Laravel Chat for ChatGPT library, run the following command:

composer require ogrre/laravel-chatgpt

After the installation, publish the vendor files by executing the command:

php artisan vendor:publish --provider="Ogrre\ChatGPT\ChatServiceProvider"

By default, the service provider will be automatically registered in the app.php file. However, if needed, you can manually add the service provider in the config/app.php file:

# config/app.php

'providers' => [
    // ...
    Ogrre\ChatGPT\ChatServiceProvider,
];

Finally, run the migration command to create the necessary database tables:

php artisan migrate

Usage instructions

To associate a chat with a model, the model must use the hasChat trait. For example, in the User model:

class User extends Authenticatable
{
    use HasFactory, HasChat;
    // ...
}

Once the model is set up with the hasChat trait, you can create a new chat using the following command:

$optional_title = "Translate this word";
$optional_role = "You're a helpful assistant";

$user_chat = $user->newChat($optional_title, $optional_role);

After creating a new chat or retrieving an existing one, you have two options to interact with it:

Using the chat object directly:

$chat->gpt("say hello in French");

Using the model directly (in this example, the User model):

$user->chatgpt("say hello in French", $chat);

The Chat model is a standard Laravel Model, so you can use all the usual functions, including delete and others.

Additionally, the library provides a ChatResource for displaying chats in an API, allowing for easy integration with API endpoints.

Contributing

Contributions are welcome and will be fully credited. I accept contributions via Pull Requests on Github.

Support me

Buy Me A Coffee

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.