loffel/laravel-chatgpt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

loffel/laravel-chatgpt

Composer 安装命令:

composer require loffel/laravel-chatgpt

包简介

Super simple wrapper for openai-php/client with error handling. Specifically for ChatGPT conversations.

README 文档

README

Super simple wrapper for openai-php/client with error handling. Specifically for ChatGPT conversations.

Tests Coverage Latest Stable Version License Total Downloads

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

  1. Laravel v8+
  2. PHP 7.4+
  3. OpenAI API Key

Install with Composer

composer require nwilging/laravel-chatgpt

Configuration

Two things must be configured for this package to work:

  1. OpenAI API key
  2. OpenAI Tokenizer

.env setup

First, get an API key from OpenAI.

Add this key to your .env as:

OPENAI_API_KEY=sk_your-key

Tokenizer Setup

To publish the tokenizer files to storage/app/openai_tokenizer:

php artisan vendor:publish --provider=Nwilging\\LaravelChatGpt\\Providers\\ChatGptServiceProvider

This will add 3 files to the storage/app/openai_tokenizer directory:

  1. characters.json
  2. encoder.json
  3. 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:

!openai_tokenizer/*.json
!openai_tokenizer/*.bpe

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:

use Nwilging\LaravelChatGpt\Models\ChatCompletionMessage;

$message1 = new ChatCompletionMessage();
$message2 = new ChatCompletionMessage();

$message1->role = ChatCompletionMessage::ROLE_SYSTEM;
$message1->name = 'system';
$message1->content = 'Initial prompt provided by system.';

$message2->role = ChatCompletionMessage::ROLE_USER;
$message2->name = 'username';
$message2->content = 'The user\'s message';

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:

use Nwilging\LaravelChatGpt\Contracts\Services\ChatGptServiceContract;

$service = app(ChatGptServiceContract::class);

// Use the messages from above!
$messages = [$message1, $message2];

$model = 'gpt-3.5-turbo';

// Create a completion:
$result = $service->createChat($model, $messages);

// Create a completion that retains the initial prompt:
$result = $service->createChatRetainInitialPrompt($model, $messages);

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 ChatCompletionMessages, or just tokenize a basic string prompt.

Tokenizing Prompts:

use Nwilging\LaravelChatGpt\Helpers\Tokenizer;

$tokenizer = app(Tokenizer::class);
$prompt = 'this is a test prompt!';

$tokens = $tokenizer->tokenize($prompt);
dd($tokens);
/**
 * Output:
 * [
 *  "this" => 5661
 *  "Ġis" => 318
 *  "Ġa" => 257
 *  "Ġtest" => 1332
 *  "Ġprompt" => 6152
 *  "!" => 0
 * ]
 */

// Get token count:
$numberOfTokens = count($tokens);

Tokenizing ChatCompletionMessages 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:

<|im_start|>role name=username
message content
<|im_end|>

For user messages:

<|im_start|>user name=TheUserName
hello this is a message from a user!
<|im_end|>

For bot messages:

<|im_start|>bot name=TheBotUsername
response from chatgpt!
<|im_end|>

Finally, system messages are treated slightly differently:

<|im_start|>system
This is a system message
<|im_end|>

The resulting formatted messages are what will be tokenized.

loffel/laravel-chatgpt 适用场景与选型建议

loffel/laravel-chatgpt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 loffel/laravel-chatgpt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 loffel/laravel-chatgpt 我们能提供哪些服务?
定制开发 / 二次开发

基于 loffel/laravel-chatgpt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-04