├── readme.md
├── ChatGPT.html
└── ChatGPT.js
/readme.md:
--------------------------------------------------------------------------------
1 | ## Simple Chatting app implemented by using chatGPT.
2 |
3 | You can know well about chatGPT API and how shall we use it.
4 |
5 | You can enjoy your chat freely by using this, but before starting, you have to input your openAI API key at the ChatGPT.js file at the top of line to OPENAI_API_KEY variable.
--------------------------------------------------------------------------------
/ChatGPT.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Chat GPT
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
22 |
23 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ChatGPT.js:
--------------------------------------------------------------------------------
1 | var OPENAI_API_KEY = "";
2 | var bTextToSpeechSupported = false;
3 | var bSpeechInProgress = false;
4 | var oSpeechRecognizer = null
5 | var oSpeechSynthesisUtterance = null;
6 | var oVoices = null;
7 |
8 | function OnLoad() {
9 | if ("webkitSpeechRecognition" in window) {
10 | } else {
11 | //speech to text not supported
12 | lblSpeak.style.display = "none";
13 | }
14 |
15 | if ('speechSynthesis' in window) {
16 | bTextToSpeechSupported = true;
17 |
18 | speechSynthesis.onvoiceschanged = function () {
19 | oVoices = window.speechSynthesis.getVoices();
20 | for (var i = 0; i < oVoices.length; i++) {
21 | selVoices[selVoices.length] = new Option(oVoices[i].name, i);
22 | }
23 | };
24 | }
25 | }
26 |
27 | function ChangeLang(o) {
28 | if (oSpeechRecognizer) {
29 | oSpeechRecognizer.lang = selLang.value;
30 | //SpeechToText()
31 | }
32 | }
33 |
34 | function Send() {
35 |
36 | var sQuestion = txtMsg.value;
37 | if (sQuestion == "") {
38 | alert("Type in your question!");
39 | txtMsg.focus();
40 | return;
41 | }
42 |
43 | var oHttp = new XMLHttpRequest();
44 | oHttp.open("POST", "https://api.openai.com/v1/completions");
45 | oHttp.setRequestHeader("Accept", "application/json");
46 | oHttp.setRequestHeader("Content-Type", "application/json");
47 | oHttp.setRequestHeader("Authorization", "Bearer " + OPENAI_API_KEY)
48 |
49 | oHttp.onreadystatechange = function () {
50 | if (oHttp.readyState === 4) {
51 | //console.log(oHttp.status);
52 | var oJson = {}
53 | if (txtOutput.value != "") txtOutput.value += "\n";
54 |
55 | try {
56 | oJson = JSON.parse(oHttp.responseText);
57 | } catch (ex) {
58 | txtOutput.value += "Error: " + ex.message
59 | }
60 |
61 | if (oJson.error && oJson.error.message) {
62 | txtOutput.value += "Error: " + oJson.error.message;
63 | } else if (oJson.choices && oJson.choices[0].text) {
64 | var s = oJson.choices[0].text;
65 |
66 | if (selLang.value != "en-US") {
67 | var a = s.split("?\n");
68 | if (a.length == 2) {
69 | s = a[1];
70 | }
71 | }
72 |
73 | if (s == "") s = "No response";
74 | txtOutput.value += "Chat GPT: " + s;
75 | TextToSpeech(s);
76 | }
77 | }
78 | };
79 |
80 | var sModel = selModel.value;// "text-davinci-003";
81 | var iMaxTokens = 2048;
82 | var sUserId = "1";
83 | var dTemperature = 0.5;
84 |
85 | var data = {
86 | model: sModel,
87 | prompt: sQuestion,
88 | max_tokens: iMaxTokens,
89 | user: sUserId,
90 | temperature: dTemperature,
91 | frequency_penalty: 0.0, //Number between -2.0 and 2.0 Positive value decrease the model's likelihood to repeat the same line verbatim.
92 | presence_penalty: 0.0, //Number between -2.0 and 2.0. Positive values increase the model's likelihood to talk about new topics.
93 | stop: ["#", ";"] //Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
94 | }
95 |
96 | oHttp.send(JSON.stringify(data));
97 |
98 | if (txtOutput.value != "") txtOutput.value += "\n";
99 | txtOutput.value += "Me: " + sQuestion;
100 | txtMsg.value = "";
101 | }
102 |
103 | function TextToSpeech(s) {
104 | if (bTextToSpeechSupported == false) return;
105 | if (chkMute.checked) return;
106 |
107 | oSpeechSynthesisUtterance = new SpeechSynthesisUtterance();
108 |
109 | if (oVoices) {
110 | var sVoice = selVoices.value;
111 | if (sVoice != "") {
112 | oSpeechSynthesisUtterance.voice = oVoices[parseInt(sVoice)];
113 | }
114 | }
115 |
116 | oSpeechSynthesisUtterance.onend = function () {
117 | //finished talking - can now listen
118 | if (oSpeechRecognizer && chkSpeak.checked) {
119 | oSpeechRecognizer.start();
120 | }
121 | }
122 |
123 | if (oSpeechRecognizer && chkSpeak.checked) {
124 | //do not listen to yourself when talking
125 | oSpeechRecognizer.stop();
126 | }
127 |
128 | oSpeechSynthesisUtterance.lang = selLang.value;
129 | oSpeechSynthesisUtterance.text = s;
130 | //Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed
131 | window.speechSynthesis.speak(oSpeechSynthesisUtterance);
132 | }
133 |
134 | function Mute(b) {
135 | if (b) {
136 | selVoices.style.display = "none";
137 | } else {
138 | selVoices.style.display = "";
139 | }
140 | }
141 |
142 | function SpeechToText() {
143 |
144 | if (oSpeechRecognizer) {
145 |
146 | if (chkSpeak.checked) {
147 | oSpeechRecognizer.start();
148 | } else {
149 | oSpeechRecognizer.stop();
150 | }
151 |
152 | return;
153 | }
154 |
155 | oSpeechRecognizer = new webkitSpeechRecognition();
156 | oSpeechRecognizer.continuous = true;
157 | oSpeechRecognizer.interimResults = true;
158 | oSpeechRecognizer.lang = selLang.value;
159 | oSpeechRecognizer.start();
160 |
161 | oSpeechRecognizer.onresult = function (event) {
162 | var interimTranscripts = "";
163 | for (var i = event.resultIndex; i < event.results.length; i++) {
164 | var transcript = event.results[i][0].transcript;
165 |
166 | if (event.results[i].isFinal) {
167 | txtMsg.value = transcript;
168 | Send();
169 | } else {
170 | transcript.replace("\n", " ");
171 | interimTranscripts += transcript;
172 | }
173 |
174 | var oDiv = document.getElementById("idText");
175 | oDiv.innerHTML = '' + interimTranscripts + '';
176 | }
177 | };
178 |
179 | oSpeechRecognizer.onerror = function (event) {
180 |
181 | };
182 | }
--------------------------------------------------------------------------------