-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
37 lines (31 loc) · 1 KB
/
ai.js
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
async function AiMessage(message, system = "be a helpful doctor. ONLY answer medical questions. DO NOT ANSWER ANYTHING OTHER THAN MEDICAL QUESTIONS. Be kind and symphathetic to the user. Do not use emojis. Do not use slang. Write short and concise.", model, temp = 0.8) {
const url = 'https://api.deepinfra.com/v1/openai/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Accept': 'text/event-stream',
}
const body = {
model: model,
messages: [
{ role: 'system', content: system },
{ role: 'user', content: message },
],
stream: false,
temperature: temp
};
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(body),
});
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
let result = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
result += chunk;
}
return result;
}