lanos/laravel-open-ai-conversations

Adds persistent conversation functionality to the existing Laravel OpenAI Package

v1.2 2023-08-25 04:26 UTC

This package is auto-updated.

Last update: 2024-04-25 06:05:32 UTC


README

Intro

This package is built on top of the openai-php/laravel package to allow you to build conversation sessions where context is preserved using a database.

This package is just an extra layer on top of the amazing package developed by:

The openai-php/laravel is required and should be installed as part of this install.

Setup

Requires PHP 8.1+

Really simple. Install our package (if you don't have the openai-php/laravel installed, composer should try to install it)

composer require lanos/laravel-open-ai-conversations

Then you need to just run the migrations

php artisan migrate

If you haven't set up the OpenAI-PHP Laravel package you can publish their config like so:

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

Then add the environment variables as needed.

OPENAI_API_KEY=sk-...

You can also override some of the default config variables in the .env for my plugin, such as setting the default model for a conversation.

OPENAI_DEFAULT_MODEL=gpt-4-32k
OPENAI_TOKEN_LIMIT=4096

Examples

Once you have fully configured both plugins you simply create a conversation using the eloquent interface.

The defaults are filled in for you, but you can override upon creation.

Once you have created the conversation you can ask questions. The plugin will automatically append all previous responses so that the model has consciousness of previous messages in the conversation. It also automatically ensures no token limits are hit by "forgetting" older messages as needed.

<?php
  
  // CREATES THE CONVERSATION
  $conversation = Conversation::create();
  
  // ASK A NEW QUESTION
  $capitalCity = $conversation->askQuestion('What is the capital city of England?');
  
  // ASK A FOLLOW UP QUESTION
  $population = $conversation->askQuestion('And what is the population of that city?');
  
  // GETS ALL OF THE QUESTIONS AND ANSWERS UP UNTIL NOW
  $messages = $conversation->messages()->get();
  

Forgotten messages

Due to token limits, when necessary the plugin will soft delete older messages, similar to how chat GPT does it. The difference is with this, it will do it less often, as the token limits are higher on the API depending on what model you use. Be wary that requests can become expensive.

You can use the withTrashed function on eloquent to get all the forgotten messages.

  // GETS ALL OF THE QUESTIONS AND ANSWERS UP UNTIL NOW INCLUJDING FOROGTTEN ONES
  $messages = $conversation->messages()->withTrashed()->get();
  
  // GETS ONLY FORGOTTEN MESSAGES
  $messages = $conversation->messages()->onlyTrashed()->get();

License

Please refer to the license.md in this repository.