App.js
6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import './normal.css';
import './App.css';
import './color_theme_1.css';
import { useState, useEffect } from 'react';
import SideMenu from './SideMenu'
import ChatBox from './ChatBox'
import OpenAIAPI from "react-openai-api";
function App() {
useEffect(() => {
getEngines();
}, [])
const [chatInput, setChatInput] = useState("");
const [models, setModels] = useState([]);
const [temperature, setTemperature] = useState(0.7);
const [currentModel, setCurrentModel] = useState("text-davinci-003");
const [chatLog, setChatLog] = useState([{
user: "gpt",
message: "Welcome to AI-PRO... How can I help you?"
}]);
// clear chats
function clearChat(){
setChatLog([]);
}
function getEngines(){
fetch("http://localhost:3080/models")
.then(res => res.json())
.then(data => {
// console.log(data.models.data)
// // set models in order alpahbetically
data.models.data.sort((a, b) => {
if(a.id < b.id) { return -1; }
if(a.id > b.id) { return 1; }
return 0;
})
setModels(data.models.data)
})
// var myHeaders = new Headers();
// myHeaders.append("Authorization", "Bearer " + "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");
// var requestOptions = {
// method: 'GET',
// headers: myHeaders,
// redirect: 'follow'
// };
// fetch("https://api.openai.com/v1/models", requestOptions)
// .then(res => res.json())
// .then(data => {
// // set models in order alpahbetically
// data.models.data.sort((a, b) => {
// if (a.id < b.id) { return -1; }
// if (a.id > b.id) { return 1; }
// return 0;
// })
// setModels(data.models.data)
// })//.catch(error => console.log('error', error));
}
async function handleSubmit(e){
e.preventDefault();
let chatLogNew = [...chatLog, { user: "me", message: `${chatInput}`} ]
setChatInput("");
setChatLog(chatLogNew)
// fetch response to the api combining the chat log array of messages and seinding it as a message to localhost:3000 as a post
const messages = chatLogNew.map((message) => message.message).join("\n")
const response = await fetch("http://localhost:3080/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
message: messages,
currentModel,
})
});
const data = await response.json();
setChatLog([...chatLogNew, { user: "gpt", message: `${data.message}`} ])
var scrollToTheBottomChatLog = document.getElementsByClassName("chat-log")[0];
scrollToTheBottomChatLog.scrollTop = scrollToTheBottomChatLog.scrollHeight;
var oHttp = new XMLHttpRequest();
// oHttp.open("POST", "https://api.openai.com/v1/completions");
// oHttp.setRequestHeader("Accept", "application/json");
// oHttp.setRequestHeader("Content-Type", "application/json");
// oHttp.setRequestHeader("Authorization", "Bearer " + "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")
oHttp.onreadystatechange = function () {
if (oHttp.readyState === 4) {
console.log("11111", oHttp.responseText);
var s = ''
var oJson = {}
if (s != "") s += "\n";
try {
oJson = JSON.parse(oHttp.responseText);
} catch (ex) {
s += "Error: " + ex.message
}
if (oJson.error && oJson.error.message) {
s += "Error: " + oJson.error.message;
} else if (oJson.choices && oJson.choices[0].text) {
s = oJson.choices[0].text;
var a = s.split("?\n");
if (a.length == 2) {
s = a[1];
}
// if (selLang.value != "en-US") {
// var a = s.split("?\n");
// if (a.length == 2) {
// s = a[1];
// }
// }
// if (s == "") s = "No response";
// console.log('ssssssssssssssssssssss',s);
// var replaceBR = (s.replace(/(?:\r\n|\r|\n)/g, "<br>"));
// // console.log('replaceBR replaceBR', replaceBR);
// //var replaceBR = (s.replace(/(?:\r\n|\r|\n)/g, "<br>")).replace(/\r?\n|\r/, "");
// var newStr = replaceBR.replace(/^(<br>)+/,"");
// var newStr11 = newStr.replace(/<br><br>/g,"<br>");
// setChatLog([...chatLogNew, { user: "gpt", message: `${s}` }]);
// setChatLog([...chatLogNew, { user: "gpt", message: `${replaceBR}`} ]);
}
}
};
var sModel = currentModel;// "text-davinci-003";
var iMaxTokens = 2048;
var sUserId = "1";
var dTemperature =temperature;
var data1 = {
model: sModel,
prompt: messages,
max_tokens: iMaxTokens,
//user: sUserId,
temperature: dTemperature,
frequency_penalty: 0.0, //Number between -2.0 and 2.0 Positive value decrease the model's likelihood to repeat the same line verbatim.
presence_penalty: 0.0, //Number between -2.0 and 2.0. Positive values increase the model's likelihood to talk about new topics.
//stop: ["#", ";"] //Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
stop:["\"\"\""]
}
oHttp.send(JSON.stringify(data1));
}
function handleTemp(temp) {
if(temp > 1){
setTemperature(1)
} else if (temp < 0){
setTemperature(0)
} else {
setTemperature(temp)
}
}
return (
<div className="App">
<SideMenu
currentModel={currentModel}
setCurrentModel={setCurrentModel}
models={models}
setTemperature={handleTemp}
temperature={temperature}
clearChat={clearChat}
/>
<ChatBox
chatInput={chatInput}
chatLog={chatLog}
setChatInput={setChatInput}
handleSubmit={handleSubmit} />
{/*
<CopyBlock
text={ chatLog.map((message, index) => (
<>
{message.message}
</>
))}
language="text"
showLineNumbers={true}
theme={dracula}
codeBlock
/> */}
</div>
);
}
export default App;