Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme
curl 'https://taras-free_open_router.web.val.run/api/v1/chat/completions' \
     -H 'accept: application/json' \
     -H 'authorization: Bearer THIS_IS_OVERRIDEN_ON_SERVER' \
     -H 'content-type: application/json' \
     --data-raw '{
  "model": "auto",
  "temperature": 0,
  "messages": [
    {
      "role": "system",
      "content": "stuff"
    },
    {
      "role": "user",
      "content": "hello"
    }
  ],
  "stream": true
}' 
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 = "/api/v1/chat/completions";
const MODELS = "/api/v1/models";
const supported_urls = [COMPLETION, MODELS];
import { blob } from "https://esm.town/v/std/blob";
const isValTown = Deno.env.get("VALTOWN_API_URL");
// 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": "*",
};
// Base class KV
abstract class KV {
abstract set(key: string, value: object): Promise<void>;
abstract get(key: string): Promise<object | undefined>;
}
// DenoDeployKV class
class DenoDeployKV extends KV {
_kv = undefined as any;
async kv() {
if (this._kv)
return this._kv;
this._kv = await Deno.openKv();
return this._kv;
}
async set(key: string, value: object): Promise<void> {
await (await this.kv()).set([key], value);
}
async get(key: string): Promise<object | undefined> {
try {
const data = await (await this.kv()).get([key]);
return data.value;
} catch (e) {
console.error("DenoDeployKV.get catch", e);
}
return undefined;
}
}
// ValTownKV class
class ValTownKV extends KV {
async set(key: string, value: object): Promise<void> {
await blob.set(key, JSON.stringify(value));
}
async get(key: string): Promise<object | undefined> {
try {
const data = await (await blob.get(key)).text();
return data ? JSON.parse(data) : undefined;
} catch (e) {
return undefined;
}
}
}
// Factory function to create the appropriate KV instance
export function createKV(): KV {
return isValTown ? new ValTownKV() : new DenoDeployKV();
}
const kv = createKV();
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
},
},
);
}
function isFreeModel(model: any) {
if (model.id.endsWith(":free")) {
return true;
}
const pricing = model.pricing;
if (pricing) {
const maxPrice = Math.max(...Object.values(pricing).map(x => x as any * 1));
if (maxPrice === 0) {
return true;
}
}
return false;
}
function getModelValue(modelId: string): int {
taras-free_open_router.web.val.run
September 18, 2024