Skip to content

Commit cc9d7c5

Browse files
authored
Merge pull request #13 from SamueleMartini/main
New API key system with project id parameter
2 parents 7198c2d + 73dd892 commit cc9d7c5

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

Model/Config.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class Config
1010
{
1111
public const XML_PATH_ENRICH_ENABLED = 'catalog_ai/settings/active';
1212
public const XML_PATH_USE_ASYNC = 'catalog_ai/settings/async';
13+
public const XML_PATH_OPENAI_ORGANIZATION_ID = 'catalog_ai/settings/openai_organization_id';
1314
public const XML_PATH_OPENAI_API_KEY = 'catalog_ai/settings/openai_key';
15+
public const XML_PATH_OPENAI_PROJECT_ID = 'catalog_ai/settings/openai_project_id';
1416
public const XML_PATH_OPENAI_API_MODEL = 'catalog_ai/settings/openai_model';
1517
public const XML_PATH_OPENAI_API_MAX_TOKENS = 'catalog_ai/settings/openai_max_tokens';
1618
public const XML_PATH_OPENAI_API_ADVANCED_SYSTEM_PROMPT = 'catalog_ai/advanced/system_prompt';
@@ -41,6 +43,21 @@ public function getApiKey(): mixed
4143
self::XML_PATH_OPENAI_API_KEY
4244
);
4345
}
46+
47+
public function getOrganizationID(): mixed
48+
{
49+
return $this->scopeConfig->getValue(
50+
self::XML_PATH_OPENAI_ORGANIZATION_ID
51+
);
52+
}
53+
54+
public function getProjectId(): mixed
55+
{
56+
return $this->scopeConfig->getValue(
57+
self::XML_PATH_OPENAI_PROJECT_ID
58+
);
59+
}
60+
4461
public function getApiModel(): mixed
4562
{
4663
return $this->scopeConfig->getValue(

Model/Config/Source/OpenAIModel.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace MageOS\CatalogDataAI\Model\Config\Source;
4+
5+
use Magento\Framework\Data\OptionSourceInterface;
6+
use OpenAI;
7+
use OpenAI\Client as OpenAIClient;
8+
use MageOS\CatalogDataAI\Model\Config as ModuleConfig;
9+
use Exception;
10+
11+
class OpenAIModel implements OptionSourceInterface
12+
{
13+
/**
14+
* @var OpenAIClient|null
15+
*/
16+
protected ?OpenAIClient $openAIclient = null;
17+
/**
18+
* @var OpenAI
19+
*/
20+
protected OpenAI $openAI;
21+
/**
22+
* @var ModuleConfig
23+
*/
24+
protected ModuleConfig $moduleConfig;
25+
26+
/**
27+
* @param OpenAI $openAI
28+
* @param ModuleConfig $moduleConfig
29+
*/
30+
public function __construct(
31+
OpenAI $openAI,
32+
ModuleConfig $moduleConfig
33+
) {
34+
$this->openAI = $openAI;
35+
$this->moduleConfig = $moduleConfig;
36+
}
37+
38+
/**
39+
* @return void
40+
*/
41+
protected function initClient()
42+
{
43+
$apiKey = $this->moduleConfig->getApiKey();
44+
$organization = $this->moduleConfig->getOrganizationID();
45+
$projectId = $this->moduleConfig->getProjectId();
46+
47+
$this->openAIclient = $this->openAI::client($apiKey, $organization, !empty($projectId) ? $projectId : null);
48+
}
49+
50+
/**
51+
* @return array[]
52+
*/
53+
public function toOptionArray(): array
54+
{
55+
$optionArray = [['value' => '', 'label' => __('-- Please Select --')]];
56+
57+
if (!empty($this->moduleConfig->getOrganizationID()) && !empty($this->moduleConfig->getApiKey())) {
58+
try {
59+
if (empty($this->openAIclient)) {
60+
$this->initClient();
61+
}
62+
63+
$models = $this->openAIclient->models()->list()->toArray();
64+
65+
foreach ($models['data'] as $model) {
66+
$optionArray[] = [
67+
'value' => $model['id'],
68+
'label' => $model['id']
69+
];
70+
}
71+
} catch (Exception $e) {
72+
return $optionArray;
73+
}
74+
75+
}
76+
77+
return $optionArray;
78+
}
79+
}

Model/Product/Enricher.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public function __construct(
1818
private readonly Config $config
1919
) {
2020
$this->client = $this->clientFactory
21+
->withOrganization($this->config->getOrganizationId())
2122
->withApiKey($this->config->getApiKey())
23+
->withProject($this->config->getProjectId())
2224
->make();
2325
}
2426

etc/adminhtml/system.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@
1616
<label>Asynchronous enrichment</label>
1717
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1818
</field>
19+
<field id="openai_organization_id" translate="label comment" sortOrder="20" showInDefault="1" canRestore="1">
20+
<label>OpenAI Organization ID</label>
21+
</field>
1922
<field id="openai_key" translate="label comment" type="password" sortOrder="30" showInDefault="1" canRestore="1">
2023
<label>OpenAI API key</label>
2124
</field>
22-
<field id="openai_model" translate="label comment" type="text" sortOrder="40" showInDefault="1" canRestore="1">
25+
<field id="openai_project_id" translate="label" sortOrder="35" showInDefault="1" canRestore="1">
26+
<label>Open AI Project ID</label>
27+
</field>
28+
<field id="openai_model" translate="label comment" type="select" sortOrder="40" showInDefault="1" canRestore="1">
2329
<label>OpenAI API Model</label>
30+
<source_model>MageOS\CatalogDataAI\Model\Config\Source\OpenAIModel</source_model>
31+
<comment>Insert api key and organization ID and save to see options. Refer to the documentation to understand which model you should select: https://platform.openai.com/docs/models/overview</comment>
2432
</field>
2533
<field id="openai_max_tokens" translate="label comment" type="text" sortOrder="40" showInDefault="1" canRestore="1">
2634
<label>OpenAI API Max Tokens</label>

0 commit comments

Comments
 (0)