Skip to content

Commit 2be315a

Browse files
committed
Added option to set a max_tokens value
1 parent 4dd4e78 commit 2be315a

File tree

4 files changed

+87
-57
lines changed

4 files changed

+87
-57
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Release Notes for chatgpt-integration
22

3+
## 1.0.2 - 2023-04-04
4+
5+
### Added
6+
- Added option to set the "max_tokens" request attribute value in the plugin settings.
7+
- Now displays a warning if the ChatGPT output is incomplete due to the "max_tokens" restriction.
8+
9+
### Changed
10+
- Lowered required craft version to 4.0.0
11+
312
## 1.0.1 - 2023-04-04
413

514
### Added

src/models/SettingsModel.php

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class SettingsModel extends Model
2727
*/
2828
public bool $usePageLang = true;
2929

30+
/**
31+
* @var int
32+
*/
33+
public int $maxTokens = 256;
34+
3035
/**
3136
* @return string
3237
*/

src/templates/functions.twig

+55-49
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% set settings = craft.app.getPlugins.getPlugin('chatgpt-integration').settings %}
2+
13
{% js %}
24

35
$(document).ready(function() {
@@ -45,66 +47,70 @@ $(document).ready(function() {
4547
}
4648

4749
sendRequest(prompt, query, input, hash);
48-
4950
});
50-
5151
});
5252

5353
/*$('.open-modal').click(function(e) {
54-
e.preventDefault();
55-
let that = $(this);
56-
let input = that.parent(".input").find("input, textarea");
57-
let label = $('.field[data-layout-element="' + that.attr('data-layout-element') + '"]').find('label').text();
54+
e.preventDefault();
55+
let that = $(this);
56+
let input = that.parent(".input").find("input, textarea");
57+
let label = $('.field[data-layout-element="' + that.attr('data-layout-element') + '"]').find('label').text();
5858

59-
console.log('.field[data-layout-element="' + that.attr('data-layout-element') + '"]');
60-
console.log(label);
59+
console.log('.field[data-layout-element="' + that.attr('data-layout-element') + '"]');
60+
console.log(label);
6161

62-
$('#my-awesome-modal header h2').text(label);
62+
$('#my-awesome-modal header h2').text(label);
6363

64-
var modal = new Garnish.Modal($('#my-awesome-modal'));
65-
$('#my-awesome-modal').attr('data-layout-element', that.attr('data-layout-element'));
64+
var modal = new Garnish.Modal($('#my-awesome-modal'));
65+
$('#my-awesome-modal').attr('data-layout-element', that.attr('data-layout-element'));
6666
});*/
6767

6868
function sendRequest(prompt, query, textField, hash) {
6969

70-
$.ajax({
71-
type: "POST",
72-
url: "https://api.openai.com/v1/chat/completions",
73-
//url: "https://api.openai.com/v1/edits",
74-
beforeSend: function (xhr) {
75-
xhr.setRequestHeader("Authorization", "Bearer {{ craft.app.getPlugins.getPlugin('chatgpt-integration').settings.getAccessToken }}");
76-
},
77-
data: JSON.stringify({
78-
"model": "gpt-3.5-turbo",
79-
"messages": [{"role": "user", "content": prompt + query }],
80-
"temperature": 0.7,
81-
"max_tokens": 256,
82-
"top_p": 1,
83-
"frequency_penalty": 0,
84-
"presence_penalty": 0
85-
}),
86-
success: function(data) {
87-
if ($('button[data-hash="' + hash + '"]').closest('.field').attr('data-type') == 'craft\\redactor\\Field') {
88-
let textareaId = textField.attr('id');
89-
$R('#' + textareaId, 'source.setCode', data.choices[0].message.content);
90-
} else {
91-
$(textField).val(data.choices[0].message.content);
92-
}
93-
},
94-
contentType: "application/json; charset=utf-8",
95-
dataType: "json"
96-
}).done(function(data) {
97-
98-
}).fail(function(data) {
99-
alert(data.responseJSON.error.message);
100-
if ($('button[data-hash="' + hash + '"]').closest('.field').attr('data-type') == 'craft\\redactor\\Field') {
101-
let textareaId = textField.attr('id');
102-
$R('#' + textareaId, 'source.setCode', query);
103-
} else {
104-
$(textField).val(query);
105-
}
106-
});
107-
}
70+
$.ajax({
71+
type: "POST",
72+
url: "https://api.openai.com/v1/chat/completions",
73+
//url: "https://api.openai.com/v1/edits",
74+
beforeSend: function (xhr) {
75+
xhr.setRequestHeader("Authorization", "Bearer {{ settings.getAccessToken }}");
76+
},
77+
data: JSON.stringify({
78+
"model": "gpt-3.5-turbo",
79+
"messages": [{"role": "user", "content": prompt + query }],
80+
"temperature": 0.7,
81+
"max_tokens": {{ settings.maxTokens }},
82+
"top_p": 1,
83+
"frequency_penalty": 0,
84+
"presence_penalty": 0
85+
}),
86+
success: function(data) {
87+
let result = data.choices[0].message.content;
88+
let state = data.choices[0].finish_reason;
89+
90+
if ($('button[data-hash="' + hash + '"]').closest('.field').attr('data-type') == 'craft\\redactor\\Field') {
91+
let textareaId = textField.attr('id');
92+
$R('#' + textareaId, 'source.setCode', result);
93+
} else {
94+
$(textField).val(result);
95+
}
96+
if(state == 'length') {
97+
alert({{ 'The reply has exceeded the specified maximum length. To fix this, either increase the value of the max_token setting or try telling chat-gpt to limit itself to a certain number of words.'|t('chatgpt-integration') }});
98+
}}
99+
},
100+
contentType: "application/json; charset=utf-8",
101+
dataType: "json"
102+
}).done(function(data) {
103+
104+
}).fail(function(data) {
105+
alert(data.responseJSON.error.message);
106+
if ($('button[data-hash="' + hash + '"]').closest('.field').attr('data-type') == 'craft\\redactor\\Field') {
107+
let textareaId = textField.attr('id');
108+
$R('#' + textareaId, 'source.setCode', query);
109+
} else {
110+
$(textField).val(query);
111+
}
112+
});
113+
}
108114
{% endjs %}
109115

110116
{#<div id="my-awesome-modal" class="modal">

src/templates/settings/general.twig

+18-8
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,30 @@
2020
first: true,
2121
}) }}
2222

23+
{{ forms.lightSwitchField({
24+
label: 'Use page language'|t('chatgpt-integration'),
25+
name: 'usePageLang',
26+
value: settings.usePageLang,
27+
on: settings.usePageLang,
28+
instructions: 'Always use current page language for query results.'|t('chatgpt-integration'),
29+
}) }}
30+
2331
{{ forms.autosuggestField({
24-
label: 'API Access Token'|t('chatgpt-integration'),
32+
label: 'API: Access Token'|t('chatgpt-integration'),
2533
name: 'accessToken',
2634
value: settings.accessToken,
27-
instructions: 'Access token for the Chat GPT API'|t('chatgpt-integration'),
35+
instructions: 'Access token for the ChatGPT API'|t('chatgpt-integration'),
2836
suggestEnvVars: true,
2937
}) }}
3038

31-
{{ forms.lightSwitchField({
32-
label: 'Use page language'|t('chatgpt-integration'),
33-
name: 'usePageLang',
34-
value: settings.usePageLang,
35-
on: settings.usePageLang,
36-
instructions: 'Always use current page language for query results.'|t('chatgpt-integration')
39+
{{ forms.textField({
40+
label: 'API: Max Tokens'|t('chatgpt-integration'),
41+
name: 'maxTokens',
42+
value: settings.maxTokens,
43+
autofocus: true,
44+
instructions: 'Maximum amount of tokens for chatgpt\'s response (<a href="https://platform.openai.com/docs/introduction/tokens">https://platform.openai.com/docs/introduction/tokens</a>)'|t('chatgpt-integration'),
45+
first: true,
46+
type: 'number',
3747
}) }}
3848
{% endnamespace %}
3949
{% endblock %}

0 commit comments

Comments
 (0)