|
| 1 | +{% set settings = craft.app.getPlugins.getPlugin('chatgpt-integration').settings %} |
| 2 | + |
1 | 3 | {% js %}
|
2 | 4 |
|
3 | 5 | $(document).ready(function() {
|
@@ -45,66 +47,70 @@ $(document).ready(function() {
|
45 | 47 | }
|
46 | 48 |
|
47 | 49 | sendRequest(prompt, query, input, hash);
|
48 |
| - |
49 | 50 | });
|
50 |
| - |
51 | 51 | });
|
52 | 52 |
|
53 | 53 | /*$('.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(); |
58 | 58 |
|
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); |
61 | 61 |
|
62 |
| - $('#my-awesome-modal header h2').text(label); |
| 62 | +$('#my-awesome-modal header h2').text(label); |
63 | 63 |
|
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')); |
66 | 66 | });*/
|
67 | 67 |
|
68 | 68 | function sendRequest(prompt, query, textField, hash) {
|
69 | 69 |
|
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 | +} |
108 | 114 | {% endjs %}
|
109 | 115 |
|
110 | 116 | {#<div id="my-awesome-modal" class="modal">
|
|
0 commit comments