-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcf-worker.js
95 lines (87 loc) · 3.19 KB
/
cf-worker.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
const TARGET_URL = 'https://fast.snova.ai/api/completion';
// use model override to disregard the model specified in the request and use the model specified here
// for example, you can set it to 'llama3-405b', so that even if the request said it want to use gpt-4o, it will use llama3-405b
const MODEL_OVERRIDE = ''; // Set this to null or an empty string if you don't want to override
export default {
async fetch(request) {
if (request.method === 'GET' && request.url.endsWith('/v1/models')) {
return new Response(JSON.stringify({
"object": "list",
"data": [
{
"id": "llama3-405b",
"object": "model",
"created": 1686935002,
"owned_by": "sambanova-ai"
},
{
"id": "Meta-Llama-3.1-405B-Instruct",
"object": "model",
"created": 1686935002,
"owned_by": "sambanova-ai",
},
{
"id": "Meta-Llama-3.1-70B-Instruct",
"object": "model",
"created": 1686935002,
"owned_by": "sambanova-ai",
},
{
"id": "Meta-Llama-3.1-8B-Instruct",
"object": "model",
"created": 1686935002,
"owned_by": "sambanova-ai",
},
],
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
});
}
if (request.method === 'POST' && request.url.endsWith('/v1/chat/completions')) {
try {
const originalPayload = await request.json();
// Override the model if MODEL_OVERRIDE is set
if (MODEL_OVERRIDE && MODEL_OVERRIDE.trim() !== '') {
originalPayload.model = MODEL_OVERRIDE;
}
const modifiedPayload = {
body: {
...originalPayload,
stop: ["<|eot_id|>"]
},
env_type: "tp16405b"
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(modifiedPayload)
};
const response = await fetch(TARGET_URL, options);
// Recreate the Response to set the appropriate CORS headers
const newResponse = new Response(response.body, {
status: response.status,
headers: {
...Object.fromEntries(response.headers),
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
});
return newResponse;
} catch (error) {
console.error('Error processing request:', error);
return new Response(JSON.stringify({ error: 'Bad Request' }), { status: 400, headers: { 'Content-Type': 'application/json' } });
}
} else {
return new Response(JSON.stringify({ error: 'Not Found' }), { status: 404, headers: { 'Content-Type': 'application/json' } });
}
}
};