Download the PHP package loffel/laravel-chatgpt without Composer
On this page you can find all versions of the php package loffel/laravel-chatgpt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download loffel/laravel-chatgpt
More information about loffel/laravel-chatgpt
Files in loffel/laravel-chatgpt
Package laravel-chatgpt
Short Description Super simple wrapper for openai-php/client with error handling. Specifically for ChatGPT conversations.
License MIT
Informations about the package laravel-chatgpt
Laravel ChatGPT
Super simple wrapper for openai-php/client with error handling. Specifically for ChatGPT conversations.
About
This package is a very simple wrapper for interacting with OpenAI Chat Completions (ChatGPT). A common problem with larger conversations is "too many tokens", which happens when a prompt is sent to the API that contains a number of tokens greater than the specified model's token limit.
This package will attempt to prune messages from the conversation starting from the beginning, so that the most recent conversation context still exists in the prompt. Additionally, if an "initial prompt" or other "system" level instruction message is required, this message will be locked to the top of the message stack so that it is always the first message.
Installation
Pre Requisites
- Laravel v8+
- PHP 7.4+
- OpenAI API Key
Install with Composer
Configuration
Two things must be configured for this package to work:
- OpenAI API key
- OpenAI Tokenizer
.env setup
First, get an API key from OpenAI.
Add this key to your .env
as:
Tokenizer Setup
To publish the tokenizer files to storage/app/openai_tokenizer
:
This will add 3 files to the storage/app/openai_tokenizer
directory:
characters.json
encoder.json
vocab.bpe
These files must be present for the tokenizer to work! It is best to commit these files
to your codebase since they are relatively small. You may also need to add the following to
your storage/app/.gitignore
:
Usage
You may use this package to execute chat completions while automatically pruning message payloads that are too large for the given OpenAI model. Additionally you may use each component separately, for example if you wish to tokenize a prompt.
The ChatCompletionMessage
Model
This is a helper model that must be used to generate chat completions. Since the chat completion API supports message objects, this class exists to help build lists of those message objects.
Example:
These messages may be sent in an array to the ChatGptService
.
Automatic Chat Completions
Send any number of messages to the ChatGptService
and automatically generate a chat
completion based on the conversation context, automatically pruning messages from the
top of the stack in the event of a token exceeded exception.
Example:
In the above example, createChat
will prune messages from the top of the stack
when the payload is too large, disregarding the initial prompt.
If the initial prompt helps define parameters for the entire conversation, you should
retain it in the payload. Use createChatRetainInitialPrompt
to do this.
Tokenizer
The Tokenizer is very similar to OpenAI's tokenizer and can be used to extract tokens from a prompt. This can be used to determine number of tokens in a prompt, etc.
The tokenizer has the ability to tokenize an array of ChatCompletionMessage
s, or just
tokenize a basic string prompt.
Tokenizing Prompts:
Tokenizing ChatCompletionMessage
s is slightly more complicated. The tokenizer will wrap
each message in ChatGPT directives denoting messages and their attributes. This differs
from simple prompt tokenization since messages themselves are more complex than simply
text -- e.g. they include a role, username, and the message content.
For bot and user messages, the format is as follows:
For user messages:
For bot messages:
Finally, system
messages are treated slightly differently:
The resulting formatted messages are what will be tokenized.
All versions of laravel-chatgpt with dependencies
laravel/framework Version >=8
guzzlehttp/guzzle Version ^7.4
openai-php/client Version ^0.7.3