Skip to content

Commit d775be1

Browse files
committed
updated legacy complete api to chat api
1 parent c487d72 commit d775be1

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

Model/Config.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class Config
1212
public const XML_PATH_OPENAI_API_KEY = 'catalog_ai/settings/openai_key';
1313
public const XML_PATH_OPENAI_API_MODEL = 'catalog_ai/settings/openai_model';
1414
public const XML_PATH_OPENAI_API_MAX_TOKENS = 'catalog_ai/settings/openai_max_tokens';
15+
public const XML_PATH_OPENAI_API_ADVANCED_SYSTEM_PROMPT = 'catalog_ai/advanced/system_prompt';
16+
public const XML_PATH_OPENAI_API_ADVANCED_TEMPERATURE = 'catalog_ai/advanced/temperature';
17+
public const XML_PATH_OPENAI_API_ADVANCED_FREQUENCY_PENALTY = 'catalog_ai/advanced/frequency_penalty';
18+
public const XML_PATH_OPENAI_API_ADVANCED_PRESENCE_PENALTY = 'catalog_ai/advanced/presence_penalty';
1519

1620
public function __construct(
1721
private \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
@@ -68,4 +72,32 @@ public function canEnrich(Product $product)
6872
{
6973
return $this->isEnabled() && $this->getApiKey() && $product->isObjectNew();
7074
}
75+
76+
public function getSystemPrompt()
77+
{
78+
return $this->scopeConfig->getValue(
79+
self::XML_PATH_OPENAI_API_ADVANCED_SYSTEM_PROMPT
80+
);
81+
}
82+
83+
public function getTemperature()
84+
{
85+
return (float)$this->scopeConfig->getValue(
86+
self::XML_PATH_OPENAI_API_ADVANCED_TEMPERATURE
87+
);
88+
}
89+
90+
public function getFrequencyPenalty()
91+
{
92+
return (float)$this->scopeConfig->getValue(
93+
self::XML_PATH_OPENAI_API_ADVANCED_FREQUENCY_PENALTY
94+
);
95+
}
96+
97+
public function getPresencePenalty()
98+
{
99+
return (float)$this->scopeConfig->getValue(
100+
self::XML_PATH_OPENAI_API_ADVANCED_PRESENCE_PENALTY
101+
);
102+
}
71103
}

Model/Product/Enricher.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,27 @@ public function enrichAttribute($product, $attributeCode)
5151

5252
$prompt = $this->parsePrompt($prompt, $product);
5353

54-
$response = $this->client->completions()->create([
54+
$response = $this->client->chat()->create([
5555
'model' => $this->config->getApiModel(),
56-
'prompt' => $this->parsePrompt($prompt, $product),
56+
'temperature' => $this->config->getTemperature(),
57+
'frequency_penalty' => $this->config->getFrequencyPenalty(),
58+
'presence_penalty' => $this->config->getPresencePenalty(),
5759
'max_tokens' => $this->config->getApiMaxTokens(),
58-
'temperature' => 0.5
60+
'messages' => [
61+
[
62+
'role' => 'system',
63+
'content' => $this->config->getSystemPrompt()
64+
],
65+
[
66+
'role' => 'user',
67+
'content' => $this->parsePrompt($prompt, $product)
68+
]
69+
]
5970
]);
6071

6172
// @TODO: no exception?
6273
if($result = $response->choices[0]) {
63-
$product->setData($attributeCode, $result->text);
74+
$product->setData($attributeCode, $result->message?->content);
6475
}
6576
}
6677
}

etc/adminhtml/system.xml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<label>AI Data Enrichment</label>
77
<tab>catalog</tab>
88
<resource>Magento_Catalog::config_catalog_ai</resource>
9-
<group id="settings" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
9+
<group id="settings" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
1010
<label>Settings</label>
1111
<field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
1212
<label>Enabled</label>
@@ -26,7 +26,7 @@
2626
<label>OpenAI API Max Tokens</label>
2727
</field>
2828
</group>
29-
<group id="product" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
29+
<group id="product" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
3030
<label>Product Fields Auto-Generation</label>
3131
<comment>
3232
<![CDATA[Use {{product_attribute_code}} (e.g. {{name}} for product name}) as product attribute data placeholder
@@ -49,6 +49,21 @@
4949
<label>Meta Description</label>
5050
</field>
5151
</group>
52+
<group id="advanced" translate="label" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
53+
<label>Advanced Settings</label>
54+
<field id="system_prompt" translate="label comment" type="text" sortOrder="10" showInDefault="1" canRestore="1">
55+
<label>System Prompt</label>
56+
</field>
57+
<field id="temperature" translate="label comment" type="text" sortOrder="20" showInDefault="1" canRestore="1">
58+
<label>System Prompt</label>
59+
</field>
60+
<field id="frequency_penalty" translate="label comment" type="text" sortOrder="30" showInDefault="1" canRestore="1">
61+
<label>Frequency Penalty</label>
62+
</field>
63+
<field id="presence_penalty" translate="label comment" type="text" sortOrder="30" showInDefault="1" canRestore="1">
64+
<label>Presence Penalty</label>
65+
</field>
66+
</group>
5267
</section>
5368
</system>
5469
</config>

etc/config.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
<default>
44
<catalog_ai>
55
<settings>
6-
<openai_model>gpt-3.5-turbo-instruct</openai_model>
6+
<openai_model>gpt-3.5-turbo</openai_model>
77
<openai_max_tokens>1000</openai_max_tokens>
88
</settings>
99
<product>
1010
<short_description>white a very short product description for {{name}} to highlight reasoning for purchase, under 100 words</short_description>
1111
<description>white a detailed product description for {{name}} with features in bullet list, under 1000 words</description>
1212
</product>
13+
<advanced>
14+
<system_prompt>You are a helpful assistant.</system_prompt>
15+
<temperature>0</temperature>
16+
<frequency_penalty>0</frequency_penalty>
17+
<presence_penalty>0</presence_penalty>
18+
</advanced>
1319
</catalog_ai>
1420
</default>
1521
</config>

0 commit comments

Comments
 (0)