sonichaos360 / lugpt
A PHP库,用于与OpenAI的Completions和ChatCompletions API交互。
1.0.1
2023-05-13 22:09 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^9.0
README
A PHP库,用于与OpenAI的Completions和ChatCompletions API交互。
目录
安装
使用Composer安装库
composer require sonichaos360/lugpt
用法
初始化
使用您的OpenAI API密钥初始化LuGPT
类
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);
在这个例子中,我们使用的是gpt-3.5-turbo
模型,该模型针对聊天进行了优化。聊天优化的模型可以与chat()
方法一起使用。如果您想使用completion()
方法,则需要使用text-davinci-003
或其他非聊天优化的模型。所有可用的OpenAI模型列表可以在这里找到。
聊天
使用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'];
对话
创建和管理对话
// 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']." | ";
此示例使用一个conversationId
,这允许API记住先前消息和响应的上下文。对话将作为单独的JSON文件存储在$conversationsPath
目录中。
完成
向text-davinci-003
或其他非聊天完成优化的模型发送提示
$prompt = 'Translate the following English sentence to French: "Hello, how are you?"'; $response = $luGPT->Completion($prompt); echo $response["choices"][0]["text"];
TODO
以下是我们在LuGPT库中计划的一些即将到来的功能和改进。我们鼓励贡献者承担这些任务或通过在GitHub上创建问题来提出新的任务。
- 重构由于与非英文语言字符的奇怪行为而被停用的
saveTokens()
函数。 - 改进错误处理并提供更详尽的信息。
- 为库实现单元测试以确保代码质量和功能。
请考虑通过创建问题或提交拉取请求来为这些功能的开发做出贡献或提出新的建议。您的输入和协作非常受重视。
问题
如果您遇到任何问题或对改进有建议,请在GitHub上创建问题。为此,请按照以下步骤操作
- 导航到LuGPT存储库的问题部分。
- 点击“新建问题”按钮。
- 为问题提供清晰和描述性的标题。
- 在描述中,尽可能详细地提供信息,例如
- 问题的总结或建议。
- 如有适用,请提供重现问题的步骤。
- 预期的行为和实际行为。
- 如有可用,提供任何错误消息或日志。
- 您正在使用的PHP版本和操作系统。
- 如有必要,请附加相关的屏幕截图或代码片段。
在创建问题时,请记住以下建议
- 在提交新问题之前,请确保搜索现有问题,以避免重复。
- 对其他用户表示尊重和礼貌,并保持建设性的讨论。
- 保持主题相关,并确保对话与当前问题相关。
贡献
欢迎贡献!请按照以下步骤操作
- 分支存储库。
- 为您的更改创建一个新分支。
- 进行更改。
- 提交拉取请求。
许可
此库在MIT许可证下发布。