Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme
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
const COMPLETION = "/chat/completions";
const MODELS = "/models";
const supported_urls = [COMPLETION, MODELS];
// Define a common set of CORS headers
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "*",
};
function err(msg): Response {
return new Response(
JSON.stringify({
error: {
message: msg,
code: 400,
},
}),
{
status: 400, // Bad Request
headers: {
"Content-Type": "application/json",
...corsHeaders, // Spread the CORS headers
},
},
);
}
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const pathname = url.pathname;
// Set up the headers for the new request
// const headers = {"Authorization": req.headers.get("Authorization")}
const headers = new Headers(req.headers);
// Delete the 'If-None-Match' header from the headers object
// headers.delete("If-None-Match");
// console.log("in-headers", headers);
// Check if the request is an OPTIONS request
if (req.method === "OPTIONS") {
// Create a response for the OPTIONS request
return new Response(null, {
status: 204, // No Content
headers: {
...corsHeaders,
"Access-Control-Max-Age": "86400", // 24 hours
},
});
}
if (!supported_urls.includes(pathname)) {
// Redirect to chatcraft.org
return new Response(null, {
status: 302, // Temporary redirect
headers: {
"Location": "https://chatcraft.org",
},
});
}
let body: string | ReadableStream<Uint8Array> | null = null;
// https://api.fireworks.ai/inference/v1/models
if (pathname == COMPLETION) {
const params = await req.clone().json();
const model = params.model.replace("fireworks/", "accounts/fireworks/models/");
const messages = params.messages.map(msg => {
// console.log(msg);
const content = typeof msg.content == "string" ? msg.content : msg.content.map(x => x.text).join("");
return { ...msg, content: content };
});
body = JSON.stringify({ ...params, model: model, messages: messages });
} else {
// console.log("else", pathname);
body = req.body;
}
url.host = "api.fireworks.ai";
url.port = "443";
url.protocol = "https";
url.pathname = "/inference/v1" + pathname;
// console.log(url.toString());
// console.log(body);
const init = {
method: req.method,
headers: {
"authorization": headers.get("authorization"),
"content-type": headers.get("content-type"),
},
body: body,
};
// console.log(init);
// console.log(init.body);
const response = await fetch(url.toString(), init);
const responseHeaders = new Headers(response.headers);
// Special handling for /api/v1/models
if (pathname == MODELS) {
// Clone the response to read it without consuming the original stream
const responseBody = await response.clone().json();
// Filter the models to only include those with ids ending in ':free'
const fixedModels = responseBody.data
.filter((model) => model.supports_chat)
taras-fireworks_ai_proxy.web.val.run
April 4, 2024