lanos/laravel-open-ai-conversations 问题修复 & 功能扩展

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

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

lanos/laravel-open-ai-conversations

最新稳定版本:v1.2

Composer 安装命令:

composer require lanos/laravel-open-ai-conversations

包简介

Adds persistent conversation functionality to the existing Laravel OpenAI Package

README 文档

README

Laravel GPT Conversations

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.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-22