-
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
0 parents
commit 3dd3190
Showing
12 changed files
with
669 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,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Jajuma\PotChatGpt\Block; | ||
|
||
use Jajuma\PotChatGpt\Helper\Config as HelperConfig; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\View\Element\Template\Context; | ||
|
||
class QuickAction extends \Jajuma\PowerToys\Block\PowerToys\QuickAction | ||
{ | ||
/** | ||
* @var HelperConfig | ||
*/ | ||
protected HelperConfig $helperConfig; | ||
|
||
/** | ||
* @param Context $context | ||
* @param HelperConfig $helperConfig | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
Template\Context $context, | ||
HelperConfig $helperConfig, | ||
array $data = [] | ||
) { | ||
parent::__construct($context, $data); | ||
$this->helperConfig = $helperConfig; | ||
} | ||
|
||
/** | ||
* Is enable | ||
* | ||
* @return bool | ||
*/ | ||
public function isEnable(): bool | ||
{ | ||
return $this->helperConfig->isEnable(); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Jajuma\PotChatGpt\Helper; | ||
|
||
use Magento\Framework\App\Helper\AbstractHelper; | ||
|
||
class Config extends AbstractHelper | ||
{ | ||
public const XML_PATH_ENABLE = 'power_toys/pot_chat_gpt/is_enabled'; | ||
|
||
public const XML_PATH_API_KEY = 'power_toys/pot_chat_gpt/api_key'; | ||
|
||
public const XML_PATH_BASE_URL = 'power_toys/pot_chat_gpt/base_url'; | ||
|
||
public const XML_PATH_MODEL = 'power_toys/pot_chat_gpt/model'; | ||
|
||
/** | ||
* Is enable | ||
* | ||
* @return bool | ||
*/ | ||
public function isEnable(): bool | ||
{ | ||
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLE); | ||
} | ||
|
||
/** | ||
* Get api key | ||
* | ||
* @return mixed | ||
*/ | ||
public function getApiKey() | ||
{ | ||
return $this->scopeConfig->getValue(self::XML_PATH_API_KEY); | ||
} | ||
|
||
/** | ||
* Get base url | ||
* | ||
* @return mixed | ||
*/ | ||
public function getBaseUrl() | ||
{ | ||
return $this->scopeConfig->getValue(self::XML_PATH_BASE_URL); | ||
} | ||
|
||
/** | ||
* Get model | ||
* | ||
* @return mixed | ||
*/ | ||
public function getModel() | ||
{ | ||
return $this->scopeConfig->getValue(self::XML_PATH_MODEL); | ||
} | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 JaJuMa | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,104 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Jajuma\PotChatGpt\Magewire; | ||
|
||
use Magewirephp\Magewire\Component; | ||
use Jajuma\PotChatGpt\Helper\Config as HelperConfig; | ||
use Magento\Framework\Serialize\Serializer\Json as JsonSerialize; | ||
|
||
class QuickAction extends Component | ||
{ | ||
protected $loader = false; | ||
|
||
protected $listeners = ['requestApi']; | ||
|
||
/** | ||
* @var HelperConfig | ||
*/ | ||
protected HelperConfig $helperConfig; | ||
|
||
/** | ||
* @var JsonSerialize | ||
*/ | ||
protected JsonSerialize $jsonSerialize; | ||
|
||
/** | ||
* @param HelperConfig $helperConfig | ||
* @param JsonSerialize $jsonSerialize | ||
*/ | ||
public function __construct( | ||
HelperConfig $helperConfig, | ||
JsonSerialize $jsonSerialize | ||
) { | ||
$this->helperConfig = $helperConfig; | ||
$this->jsonSerialize = $jsonSerialize; | ||
} | ||
|
||
/** | ||
* Request api | ||
* | ||
* @return void | ||
*/ | ||
public function requestApi($request) | ||
{ | ||
$requestMessage = ''; | ||
if (is_array($request)) { | ||
if (!empty($request['request_message'])) { | ||
$requestMessage = $request['request_message']; | ||
} | ||
} else { | ||
$requestMessage = $request; | ||
} | ||
|
||
if (empty($requestMessage)) { | ||
$message = 'Chat GPT returned an error or timed out. Please try again later!'; | ||
$this->dispatchBrowserEvent('finish-request-chat-gpt-api', ['message' => nl2br($message)]); | ||
return; | ||
} | ||
|
||
$apiKey = $this->helperConfig->getApiKey(); | ||
$baseUrl = $this->helperConfig->getBaseUrl(); | ||
$model = $this->helperConfig->getModel(); | ||
|
||
$data = [ | ||
'model' => $model, | ||
'messages' => [ | ||
[ | ||
'role' => 'user', | ||
'content' => $requestMessage | ||
] | ||
], | ||
'temperature' => 0 | ||
]; | ||
|
||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $baseUrl); | ||
curl_setopt($curl, CURLOPT_POST, true); | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->jsonSerialize->serialize($data)); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | ||
'Content-Type: application/json', | ||
'Authorization: Bearer ' . $apiKey | ||
)); | ||
|
||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
|
||
if (!$response) { | ||
$message = 'Chat GPT returned an error or timed out. Please try again later!'; | ||
$this->dispatchBrowserEvent('finish-request-chat-gpt-api', ['message' => nl2br($message)]); | ||
return; | ||
} | ||
|
||
$result = json_decode($response, true); | ||
if (!empty($result['error'])) { | ||
$message = $result['error']['message']; | ||
$this->dispatchBrowserEvent('finish-request-chat-gpt-api', ['message' => $message]); | ||
return; | ||
} | ||
|
||
$message = $result['choices'][0]['message']['content']; | ||
$this->dispatchBrowserEvent('finish-request-chat-gpt-api', ['message' => nl2br($message)]); | ||
} | ||
} |
Oops, something went wrong.