Skip to content

Commit 5045316

Browse files
committed
Compatability changes and option to switch model
1 parent f8011e6 commit 5045316

File tree

7 files changed

+140
-28
lines changed

7 files changed

+140
-28
lines changed

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
"support": {
77
"email": "support@diff.ch"
88
},
9+
"authors": [
10+
{
11+
"name": "diff. Kommunikation AG",
12+
"homepage": "https://www.diff.ch/"
13+
}
14+
],
915
"require": {
10-
"php": ">=8.0.2",
11-
"craftcms/cms": "^4.0.0"
16+
"php": ">=8.2.0",
17+
"craftcms/cms": "^5.0.0"
1218
},
1319
"require-dev": {
1420
"craftcms/ecs": "dev-main"

src/models/SettingsModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class SettingsModel extends Model
4242
*/
4343
public array $enabledFields = [];
4444

45+
/**
46+
* @var string
47+
*/
48+
public string $model = 'gpt-3.5-turbo';
49+
4550
/**
4651
* @return string
4752
*/

src/services/PromptService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public function getPrompts(bool $enabled = false): array
2525
$prompts = $prompts->where(['enabled' => true]);
2626
}
2727

28-
$prompts = $prompts->all();
29-
30-
foreach($prompts as $prompt) {
28+
foreach($prompts->all() as $prompt) {
3129
$result[] = $this->mapToModel($prompt);
3230
}
3331

src/templates/form.twig

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,28 @@
4949
{% endfor %}
5050
</ul>
5151
{% if displayTranslationPrompts %}
52-
{% if prompts|length %}
53-
<hr>
54-
{% endif %}
55-
<ul class="padded">
56-
{% if craft.app.sites.getAllSites()|length > 1 %}
57-
{% for site in craft.app.sites.getAllSites () %}
58-
<li><a href="#" class="doAi" data-hash="{{ hash }}" data-prompt="Translate to {{ site.language }}: " type="button" role="button" aria-label="{{ 'Translate to {language}'|t('chatgpt-integration', params = {
59-
language: site.language
60-
} ) }}">{{ 'Translate to {language}'|t('chatgpt-integration', params = {
61-
language: site.language
62-
} ) }}</a></li>
63-
{% endfor %}
52+
{% set allSites = craft.app.sites.getAllSites() %}
53+
54+
{% if allSites|length %}
55+
{% if prompts|length %}
56+
<hr>
6457
{% endif %}
65-
</ul>
66-
{#<hr>
67-
<ul class="padded">
68-
<li>
69-
<a href="#" class="open-modal">{{ 'Senden'|t('chatgpt-integration') }}</a>
70-
</li>
71-
</ul>#}
58+
<ul class="padded">
59+
{% if allSites|length %}
60+
{% for site in allSites %}
61+
{% set translationText = 'Translate to {language}'|t('chatgpt-integration', params = { language: site.language }) %}
62+
<li><a href="#"
63+
class="doAi"
64+
data-hash="{{ hash }}"
65+
data-prompt="Translate to {{ site.language }}: "
66+
type="button"
67+
role="button"
68+
aria-label="{{ translationText }}">
69+
{{ translationText }}</a></li>
70+
{% endfor %}
71+
{% endif %}
72+
</ul>
73+
{% endif %}
7274
{% endif %}
7375
</div>
7476
</div>

src/templates/functions.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% js %}
44

5-
$(document).ready(function() {
5+
$(function() {
66

77
$('.chatgpt-button').each(function () {
88
let that = $(this);
@@ -74,7 +74,7 @@ function sendRequest(prompt, query, textField, hash) {
7474
xhr.setRequestHeader("Authorization", "Bearer {{ settings.getAccessToken }}");
7575
},
7676
data: JSON.stringify({
77-
"model": "gpt-3.5-turbo",
77+
"model": "{{ settings.model }}",
7878
"messages": [{"role": "user", "content": prompt + query }],
7979
"temperature": 0.7,
8080
"max_tokens": {{ settings.maxTokens }},

src/templates/settings/api.twig

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,107 @@
2929
type: 'number',
3030
}) }}
3131

32+
{% set preselected = settings ? settings.model : '' %}
33+
{% set modelList = { (preselected): preselected} %}
34+
35+
<div id="modelList">
36+
{% set modelInput %}
37+
<div class="flex">
38+
{{ forms.select({
39+
name: 'model',
40+
options: modelList,
41+
value: settings ? settings.model : '' ,
42+
readonly: true,
43+
class: 'model-select-1',
44+
targetPrefix: ''
45+
}) }}
46+
47+
<div class="btn openai-refresh-models">{{ "Refresh"|t('chatgpt-integration') }}</div>
48+
<div class="spinner hidden"></div>
49+
</div>
50+
{% endset %}
51+
52+
{{ forms.field({
53+
label: "Gateway plan"|t('app'),
54+
instructions: "Choose the model to be used for your prompt requests.",
55+
id: 'model',
56+
}, modelInput) }}
57+
</div>
58+
59+
3260
{% endnamespace %}
3361
{% endblock %}
3462

63+
{% js %}
64+
65+
$(function () {
66+
let openaiBtn = $('.openai-refresh-models');
67+
68+
openaiBtn.on('click', function (ev) {
69+
if (openaiBtn.hasClass('disabled')) {
70+
return;
71+
}
72+
73+
openaiBtn.addClass('disabled').siblings('.spinner').removeClass('hidden');
74+
75+
let modelSelect = $('.model-select-1 select');
76+
var currentModel = $(modelSelect).first().val();
77+
78+
$.ajax({
79+
type: "GET",
80+
url: "https://api.openai.com/v1/models",
81+
//url: "https://api.openai.com/v1/edits",
82+
beforeSend: function (xhr) {
83+
xhr.setRequestHeader("Authorization", "Bearer {{ settings.getAccessToken }}");
84+
},
85+
success: function(data) {
86+
87+
let modelList = data['data'];
88+
89+
openaiBtn
90+
.removeClass('disabled')
91+
.siblings('.spinner')
92+
.addClass('hidden');
93+
94+
let currentModelStillAvailable = false;
95+
96+
modelSelect.empty();
97+
98+
console.log(currentModel);
99+
100+
for (var i = 0; i < modelList.length; i++) {
101+
102+
console.log(modelList[i].id);
103+
104+
if (modelList[i].id === currentModel) {
105+
currentModelStillAvailable = true;
106+
}
107+
108+
modelSelect.append(
109+
'<option value="' +
110+
modelList[i].id +
111+
'">' +
112+
modelList[i].id + ' (' + modelList[i].owned_by + ')' +
113+
'</option>'
114+
);
115+
}
116+
117+
if (currentModelStillAvailable) {
118+
modelSelect.val(currentModel);
119+
}
120+
121+
},
122+
contentType: "application/json; charset=utf-8",
123+
dataType: "json"
124+
}).done(function(data) {
125+
126+
}).fail(function(data) {
127+
alert(data.responseJSON.error.message);
128+
});
129+
130+
});
131+
132+
openaiBtn.click();
133+
});
134+
135+
{% endjs %}

src/templates/settings/fields.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<th scope="col">{{ 'Label' |t('chatgpt-integration') }}</th>
2323
<th scope="col">{{ 'Handle' |t('chatgpt-integration') }}</th>
2424
<th scope="col">{{ 'Field type' |t('chatgpt-integration') }}</th>
25-
<th scope="col">{{ 'Group' |t('chatgpt-integration') }}</th>
25+
{#<th scope="col">{{ 'Group' |t('chatgpt-integration') }}</th>#}
2626
<th class="thin">{{ 'Enabled' |t('chatgpt-integration') }}</th>
2727

2828
</tr>
@@ -37,7 +37,7 @@
3737
<span data-icon="clipboard" aria-hidden="true"></span>
3838
</div></td>
3939
<td class="group-column">{{ craft.chatgptIntegration.getClass(field) }}</td>
40-
<td class="group-column">{{ field.group }}</td>
40+
{#<td class="group-column">{{ field.group }}</td>#}
4141
<td class="enabled-column">
4242
{% if craft.chatgptIntegration.getClass(field) != "craft\\fields\\Matrix" %}
4343
{{ forms.lightswitch({

0 commit comments

Comments
 (0)