-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b4269a
commit 6967e4f
Showing
9 changed files
with
606 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OPENAI_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
return (new PhpCsFixer\Config()) | ||
->setRules([ | ||
'@PSR12' => true, | ||
'array_indentation' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
'combine_consecutive_unsets' => true, | ||
'class_attributes_separation' => ['elements' => ['method' => 'one', ]], | ||
'multiline_whitespace_before_semicolons' => false, | ||
'single_quote' => true, | ||
|
||
'binary_operator_spaces' => [ | ||
'operators' => [ | ||
] | ||
], | ||
'braces' => [ | ||
'allow_single_line_closure' => true, | ||
], | ||
'concat_space' => ['spacing' => 'one'], | ||
'declare_equal_normalize' => true, | ||
'function_typehint_space' => true, | ||
'single_line_comment_style' => ['comment_types' => ['hash']], | ||
'include' => true, | ||
'lowercase_cast' => true, | ||
'no_extra_blank_lines' => [ | ||
'tokens' => [ | ||
'curly_brace_block', | ||
'extra', | ||
'throw', | ||
'use', | ||
] | ||
], | ||
'no_multiline_whitespace_around_double_arrow' => true, | ||
'no_spaces_around_offset' => true, | ||
'no_whitespace_before_comma_in_array' => true, | ||
'no_whitespace_in_blank_line' => true, | ||
'object_operator_without_whitespace' => true, | ||
'ternary_operator_spaces' => true, | ||
'trim_array_spaces' => true, | ||
'unary_operator_spaces' => true, | ||
'whitespace_after_comma_in_array' => true, | ||
'space_after_semicolon' => true, | ||
]) | ||
->setLineEnding("\n") | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# LuGPT | ||
|
||
A PHP library for interacting with OpenAI's Completions and ChatCompletions API. | ||
|
||
## Table of Contents | ||
|
||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Initialization](#initialization) | ||
- [Chat](#chat) | ||
- [Completion](#completion) | ||
- [Conversations](#conversations) | ||
- [Contributing](#contributing) | ||
- [License](#license) | ||
|
||
## Installation | ||
|
||
Install the library using [Composer](https://getcomposer.org/): | ||
|
||
```bash | ||
composer require sonichaos360/lugpt | ||
``` | ||
|
||
## Usage | ||
|
||
### Initialization | ||
|
||
Initialize the `LuGPT` class with your OpenAI API key: | ||
|
||
```php | ||
require_once 'vendor/autoload.php'; | ||
|
||
use Sonichaos360\LuGPT\LuGPT; | ||
|
||
$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 LuGPT($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](https://platform.openai.com/docs/models). | ||
|
||
|
||
### Chat | ||
|
||
Send a message using Chat Completions API: | ||
|
||
```php | ||
$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: | ||
|
||
```php | ||
// 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: | ||
|
||
```php | ||
$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](https://github.com/sonichaos360/lugpt/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](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "sonichaos360/lugpt", | ||
"description": "A PHP library for interacting with OpenAI's Completions and ChatCompletions API.", | ||
"keywords": ["openai", "gpt", "gpt-3.5-turbo", "chatbot", "ai"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Luciano Joan Vergara", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6", | ||
"ext-curl": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Sonichaos360\\LuGPT\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Sonichaos360\\LuGPT\\Tests\\": "tests/" | ||
} | ||
}, | ||
"config": { | ||
"platform": { | ||
"php": "7.4" | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
|
||
|
||
// var_dump(json_decode(file_get_contents('../conversations/645756bcd4697.json'))); | ||
// exit; | ||
|
||
require '../src/LuGPT/LuGPT.php'; | ||
$ENV = parse_ini_file('../.env'); | ||
|
||
//Initiate LuGPT | ||
$luGPT = new Sonichaos360\LuGPT\LuGPT( | ||
$ENV['OPENAI_API_KEY'], //ApiKey | ||
'gpt-3.5-turbo', //model | ||
'1500', //tokens | ||
'1', //temp | ||
'../conversations', //conversationPath | ||
'../chat.log' //Log Path | ||
); | ||
|
||
// 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 Spanish: "Hello, how are you?"'; | ||
$response1 = $luGPT->Chat($systemMessage, $userMessage1, $conversationId); | ||
|
||
var_dump($response1); | ||
|
||
|
||
$userMessage2 = 'Now, please translate this: "I am fine, thank you."'; | ||
$response2 = $luGPT->Chat($systemMessage, $userMessage2, $conversationId); | ||
|
||
var_dump($response2); | ||
|
||
// echo $response2['choices'][0]['message']['content']." | "; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
require '../src/LuGPT/LuGPT.php'; | ||
$ENV = parse_ini_file('../.env'); | ||
|
||
//Initiate LuGPT | ||
$LuGPT = new Sonichaos360\LuGPT\LuGPT( | ||
$ENV['OPENAI_API_KEY'], //ApiKey | ||
'text-davinci-003', //model | ||
'1500', //tokens | ||
'0.5', //temp | ||
); | ||
|
||
//Define system Message | ||
$prompt = "This is a list of the best programming language ordered by creation date: "; | ||
|
||
//Make a Chat Completion request | ||
$result = $LuGPT->Completion($prompt); | ||
|
||
var_dump($result); |
Oops, something went wrong.