forked from vespaiach/axios-fetch-adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.ts
121 lines (109 loc) · 3.22 KB
/
script.ts
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
import axios from "axios";
import fetchAdapter from "./index";
import { createParser, EventSourceParseCallback } from "eventsource-parser";
window["axios"] = axios;
window.onload = async function () {
try {
const data = await axios.request({
url: "/package.json",
method: "get",
adapter: fetchAdapter,
});
document.getElementById("app")?.append(JSON.stringify(data, null, 4));
} catch (e) {
console.log(e);
}
};
const formElem = document.getElementById("formElem");
if (formElem && formElem instanceof HTMLFormElement) {
formElem.onsubmit = async (e) => {
e.preventDefault();
let response = await axios.request({
url: "https://httpbin.org/post",
method: "post",
data: new FormData(formElem),
adapter: fetchAdapter,
});
console.log(response);
};
}
const chatForm = document.getElementById("chat-gpt");
const chatInput = document.getElementById("chat-input");
if (chatForm && chatInput && chatInput instanceof HTMLInputElement) {
chatForm.onsubmit = async (e) => {
e.preventDefault();
const input = {
model: { id: "gpt-3.5-turbo", name: "GPT-3.5" },
systemPrompt:
"You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.",
messages: [
{
role: "user",
content: chatInput.value,
},
],
};
const res = await axios.post(
`https://api.openai.com/v1/chat/completions`,
JSON.stringify({
model: input.model.id,
messages: [
{
role: "system",
content: input.systemPrompt,
},
...input.messages,
],
max_tokens: 1000,
temperature: 1,
stream: true,
}),
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${import.meta.env.VITE_OPENAI_API_KEY}`,
},
adapter: fetchAdapter,
responseType: "stream",
}
);
const decoder = new TextDecoder();
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
const onParse: EventSourceParseCallback = (event) => {
if (event.type === "event") {
const data = event.data;
if (data === "[DONE]") {
controller.close();
return;
}
try {
const json = JSON.parse(data);
const text = json.choices[0].delta.content;
const queue = encoder.encode(text);
controller.enqueue(queue);
} catch (e) {
controller.error(e);
}
}
};
const parser = createParser(onParse);
const reader = res.data.getReader();
let done = false;
while (!done) {
const { value: chunk, done: doneReading } = await reader.read();
done = doneReading;
parser.feed(decoder.decode(chunk));
}
},
});
const reader = stream.getReader();
let done = false;
while (!done) {
const { value, done: doneReading } = await reader.read();
done = doneReading;
console.log(decoder.decode(value));
}
};
}