forked from xpportal/alchemy-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (136 loc) · 4.58 KB
/
index.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
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
/*
* Copyright 2023 SXP Digital, LLC
*
* This file is part of Alchemy Worker: https://github.com/xpportal/alchemy-worker.
*
* Alchemy Worker is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License version 2.0 as
* published by the Mozilla Foundation.
*
* Alchemy Worker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public License for more details.
*
* You should have received a copy of the Mozilla Public License
* along with Alchemy Worker. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
*
* Alternatively, the contents of this file may be used under the terms of the
* GNU General Public License version 2.0 or (at your option) any later version,
* in which case the provisions of the GNU General Public License version 2.0
* or later shall apply instead of those above.
*
* If you wish to allow use of your version of this file only under the terms of
* the GPL version 2.0, and not to allow others to use your version of this file
* under the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the GPL version 2.0.
* If you do not delete the provisions above, a recipient may use your version of
* this file under either the MPL or the GPL version 2.0.
*/
async function handleRequest(request) {
if (request.method === "OPTIONS") {
return handleOptions(request);
} else {
return handleFetch(request);
}
}
async function handleFetch(request) {
// Extract the bearer token from the request headers
const authorization = request.headers.get("Authorization");
if (!authorization) {
return new Response("Unauthorized", { status: 401 });
}
const data = await request.json();
console.log("data", data);
const history = data.Input.messages ?? [];
console.log("history", history);
const lastUserMessage = history.length > 1 ? history[history.length - 2].split(": ")[1] : null;
const lastAssistantMessage = history.length > 0 ? history[history.length - 1].split(": ")[1] : null;
//let prompt = data.inputs.personality;
let prompt = data.Input.personality;
let finalPrompt = prompt
.replaceAll("#speaker", data.Input.Speaker)
.replaceAll("#input", data.Input.Input)
.replaceAll("#agent", data.Input.Agent)
.replaceAll("#conversation", data.Input.Conversation)
.replaceAll("undefined\n", "")
.replaceAll("undefined", "")
.slice(-5000);
console.log("finalPrompt", finalPrompt);
const token = authorization.split(" ")[1];
console.log(data.Input);
const messages = [
{
"role": "system",
"content": data.Input.personality,
},
{
"content": "Hello, how are you?",
"role": "user",
},
{
"content": "I'm doing well! Excited to be with you here in the metaverse!",
"role": "assistant",
},
];
if (lastUserMessage) {
messages.push({
"content": lastUserMessage,
"role": "user",
});
}
if (lastAssistantMessage) {
messages.push({
"content": lastAssistantMessage,
"role": "assistant",
});
}
messages.push({
"content": data.Input.Input,
"role": "user",
});
const postData = {
model: "gpt-4",
messages,
temperature: 0.7,
max_tokens: 500,
stop: "###",
};
// Make the request to the OpenAI API
const gptResponse = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(postData),
});
// Process the response from the OpenAI API
const gptData = await gptResponse.json();
if (gptData.choices && gptData.choices.length > 0) {
console.log(gptData.choices[0].text);
} else {
console.log("No completion found.", gptData);
}
// Return the results to the client
const headers = {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
return new Response(JSON.stringify(gptData), { headers });
}
function handleOptions(request) {
// Make sure the necessary headers are present
// for this to be a valid pre-flight request
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
return new Response(null, { headers });
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});