forked from haibbo/cf-openai-azure-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf-openai-palm-proxy.js
159 lines (137 loc) · 4.93 KB
/
cf-openai-palm-proxy.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
151
152
153
154
155
156
157
158
159
// The deployment name you chose when you deployed the model.
const chatmodel = 'chat-bison-001';
const textmodel = 'text-bison-001';
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method === 'OPTIONS') {
return handleOPTIONS(request)
}
const url = new URL(request.url);
if (url.pathname === '/v1/chat/completions') {
var path = "generateMessage"
var deployName = chatmodel;
} else if (url.pathname === '/v1/completions') {
var path = "generateText"
var deployName = textmodel;
} else {
return new Response('404 Not Found', { status: 404 })
}
let body;
if (request.method === 'POST') {
body = await request.json();
}
const authKey = request.headers.get('Authorization');
if (!authKey) {
return new Response("Not allowed", { status: 403 });
}
// Remove 'Bearer ' from the start of authKey
const apiKey = authKey.replace('Bearer ', '');
const fetchAPI = `https://generativelanguage.googleapis.com/v1beta2/models/${deployName}:${path}?key=${apiKey}`
// Transform request body from OpenAI to PaLM format
const transformedBody = {
temperature: body?.temperature,
candidateCount: body?.n,
topP: body?.top_p,
prompt: {
context: body?.messages?.find(msg => msg.role === 'system')?.content,
messages: body?.messages?.filter(msg => msg.role !== 'system').map(msg => ({
// author: msg.role === 'user' ? '0' : '1',
content: msg.content,
})),
},
};
const payload = {
method: request.method,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(transformedBody),
};
const response = await fetch(fetchAPI, payload);
const palmData = await response.json();
// Transform response from PaLM to OpenAI format
const transformedResponse = transformResponse(palmData);
if (body?.stream != true){
return new Response(JSON.stringify(transformedResponse), {
headers: {'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*' }
});
} else {
let { readable, writable } = new TransformStream();
streamResponse(transformedResponse, writable);
return new Response(readable, {
headers: {'Content-Type': 'text/event-stream',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*' }
});
}
}
function streamResponse(response, writable) {
let encoder = new TextEncoder();
let writer = writable.getWriter();
let content = response.choices[0].message.content;
// Split the content into chunks, and send each chunk as a separate event
let chunks = content.match(/\s+|\S+/g) || [];
chunks.forEach((chunk, i) => {
let chunkResponse = {
...response,
object: "chat.completion.chunk",
choices: [{
index: response.choices[0].index,
delta: { ...response.choices[0].message, content: chunk },
finish_reason: i === chunks.length - 1 ? 'stop' : null // Set 'stop' for the last chunk
}],
usage: null
};
writer.write(encoder.encode(`data: ${JSON.stringify(chunkResponse)}\n\n`));
});
// Write the done signal
writer.write(encoder.encode(`data: [DONE]\n`));
writer.close();
}
// Function to transform the response
function transformResponse(palmData) {
// Check if the 'candidates' array exists and if it's not empty
if (!palmData.candidates || palmData.candidates.length === 0) {
// If it doesn't exist or is empty, create a default candidate message
palmData.candidates = [
{
"author": "1",
"content": "Ooops, the model returned nothing"
}
];
}
return {
id: "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
object: 'chat.completion',
created: Math.floor(Date.now() / 1000), // Current Unix timestamp
model: 'gpt-3.5-turbo', // Static model name
usage: {
prompt_tokens: palmData.messages.length, // This is a placeholder. Replace with actual token count if available
completion_tokens: palmData.candidates.length, // This is a placeholder. Replace with actual token count if available
total_tokens: palmData.messages.length + palmData.candidates.length, // This is a placeholder. Replace with actual token count if available
},
choices: palmData.candidates.map((candidate, index) => ({
message: {
role: 'assistant',
content: candidate.content,
},
finish_reason: 'stop', // Static finish reason
index: index,
})),
};
}
async function handleOPTIONS(request) {
return new Response("pong", {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*'
}
})
}