定制 sonichaos360/lugpt 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sonichaos360/lugpt

Composer 安装命令:

composer create-project sonichaos360/lugpt

包简介

A PHP library for interacting with OpenAI's Completions and ChatCompletions API.

README 文档

README

A PHP library for interacting with OpenAI's Completions and ChatCompletions API.

Table of Contents

Installation

Install the library using Composer:

composer require sonichaos360/lugpt

Usage

Initialization

Initialize the LuGPT class with your OpenAI API key:

require_once 'vendor/autoload.php';

use Sonichaos360\LuGPT\Completions;

$apiKey = 'your_openai_api_key';
$model = 'gpt-3.5-turbo';
$maxTokens = '1500';
$temp = '1';
$conversationsPath = '../conversations'; //Optional, necessary only if you will manage conversation history using createConversation()

$luGPT = new Completions($apiKey, $model, $maxTokens, $temp, $conversationsPath);

In this example, we are using the gpt-3.5-turbo model, which is optimized for chat. The chat-optimized models can be used with the chat() method. If you want to use the completion() method, you will need to use text-davinci-003 or another non-chat-optimized model. A list of all available OpenAI models can be found here.

Chat

Send a message using Chat Completions API:

$systemMessage = 'You are an assistant that translates English to French.';
$userMessage = 'Translate the following sentence: "Hello, how are you?"';
$response = $luGPT->Chat($systemMessage, $userMessage);

echo $response['choices'][0]['message']['content'];

Conversations

Create and manage conversations:

// Create a new conversation
$conversationId = $luGPT->createConversation();

// Send messages within the conversation
$systemMessage = 'You are an assistant that translates between any languages.';
$userMessage1 = 'Translate the following sentence from English to French: "Hello, how are you?"';
$response1 = $luGPT->Chat($systemMessage, $userMessage1, $conversationId);

echo $response1['choices'][0]['message']['content']." | ";

$userMessage2 = 'Now, please translate this: "I am fine, thank you."';
$response2 = $luGPT->Chat($systemMessage, $userMessage2, $conversationId);

echo $response2['choices'][0]['message']['content']." | ";

This example uses a conversationId, which allows the API to remember the context of previous messages and responses. The conversations will be created as separate JSON files stored in the $conversationsPath directory.

Completion

Send a prompt to the text-davinci-003 or other non chat completions optimized model:

$prompt = 'Translate the following English sentence to French: "Hello, how are you?"';
$response = $luGPT->Completion($prompt);

echo $response["choices"][0]["text"];

TODO

Here are some upcoming features and improvements we have planned for the LuGPT library. We encourage contributors to take on these tasks or suggest new ones by creating issues on GitHub:

  1. Refactor the saveTokens() function, which was deactivated due to strange behavior with non-English language characters.
  2. Improve error handling and provide more informative error messages.
  3. Implement unit tests for the library to ensure code quality and functionality.

Please consider contributing to the development of these features or suggesting new ones by creating an issue or submitting a pull request. Your input and collaboration are greatly appreciated.

Issues

If you encounter any problems or have suggestions for improvements, we welcome you to create an issue on GitHub. To do so, please follow these steps:

  1. Navigate to the Issues section of the LuGPT repository.
  2. Click on the "New issue" button.
  3. Provide a clear and descriptive title for the issue.
  4. In the description, include as much detail as possible, such as:
    • A summary of the problem or suggestion.
    • Steps to reproduce the issue, if applicable.
    • The expected behavior and the actual behavior.
    • Any error messages or logs, if available.
    • The PHP version and operating system you are using.
    • Attach any relevant screenshots or code snippets, if necessary.

When creating an issue, please keep the following recommendations in mind:

  • Make sure to search the existing issues before submitting a new one, to avoid duplicates.
  • Be respectful and courteous to other users and maintain a constructive discussion.
  • Stay on topic and keep the conversation relevant to the issue at hand.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Make your changes.
  4. Submit a pull request.

License

This library is released under the MIT License.

sonichaos360/lugpt 适用场景与选型建议

sonichaos360/lugpt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「ai」 「chatbot」 「openai」 「gpt」 「gpt-3.5-turbo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 sonichaos360/lugpt 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-07