
@wilt
5 likes16 public vals
Joined January 12, 2023
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
/**
* Create a new Qdrant collection in an existing cluster with the given name
* Uses recommended values for OpenAPI embeddings
*/
async function createQdrantCollection(args: {
collectionName: string;
qdrantKey: string;
qdrantUrl: string;
}) {
const { collectionName, qdrantKey, qdrantUrl } = args;
const resp = await fetch(
`https://${qdrantUrl}/collections/${collectionName}`,
{
method: "PUT",
headers: {
"api-key": qdrantKey,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "facts",
vectors: { size: 1536, distance: "Cosine" },
}),
},
);
return resp.json();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Call OpenAPI Embeddings api to vectorize a query string
* Returns an array of 1536 numbers
*/
const getOpenapiEmbedding = async ({ openapiToken, query }: {
openapiToken: string;
query: string;
}): Promise<number[]> =>
stevekrouse.fetchJSON("https://api.openai.com/v1/embeddings", {
method: "POST",
headers: {
Authorization: `Bearer ${openapiToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "text-embedding-ada-002",
input: query.trim().replaceAll("\n", " "),
}),
}).then((j) => j.data[0].embedding);
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
// See https://open-meteo.com/en/docs for usage
type OMHourlyMeasures =
| "temperature_2m"
| "relativehumidity_2m"
| "dewpoint_2m"
| "apparent_temperature"
| "pressure_msl"
| "surface_pressure"
| "cloudcover"
| "cloudcover_low"
| "cloudcover_mid"
| "cloudcover_high"
| "windspeed_10m"
| "windspeed_80m"
| "windspeed_120m"
| "windspeed_180m"
| "winddirection_10m"
| "winddirection_80m"
| "winddirection_120m"
| "winddirection_180m"
| "windgusts_10m"
| "shortwave_radiation"
| "direct_radiation"
| "direct_normal_irradiance"
| "diffuse_radiation"
| "vapor_pressure_deficit"
| "cape"
| "evapotranspiration"
| "et0_fao_evapotranspiration"
| "precipitation"
| "snowfall"
| "precipitation_probability"
| "rain"
| "showers"
| "weathercode"
| "snow_depth"
| "freezinglevel_height"
| "visibility"
| "soil_temperature_0cm"
| "soil_temperature_6cm"
| "soil_temperature_18cm"
| "soil_temperature_54cm"
| "soil_moisture_0_1cm"
| "soil_moisture_1_3cm"
| "soil_moisture_3_9cm"
| "soil_moisture_9_27cm"
| "soil_moisture_27_81cm"
| "is_day";
type OMDailyMeasures =
| "temperature_2m_max"
| "temperature_2m_min"
| "apparent_temperature_max"
| "apparent_temperature_min"
| "precipitation_sum"
| "rain_sum"
| "showers_sum"
| "snowfall_sum"
| "precipitation_hours"
| "precipitation_probability_max"
| "precipitation_probability_min"
| "precipitation_probability_mean"
| "weathercode"
| "sunrise"
| "sunset"
| "windspeed_10m_max"
| "windgusts_10m_max"
| "winddirection_10m_dominant"
| "shortwave_radiation_sum"
| "et0_fao_evapotranspiration"
| "uv_index_max"
| "uv_index_clear_sky_max";
interface OMParams {
latitude: number;
longitude: number;
hourly?: OMHourlyMeasures[];
daily?: OMDailyMeasures[];
current_weather?: boolean;
temperature_unit?: "celcius" | "fahrenheit";
windspeed_unit?: "kmh" | "ms" | "mph" | "kn";
precipitation_unit?: "mm" | "inch";
timeformat?: string;
timezone?: string;
past_days?: number;
forecast_days?: number;
start_date?: string;
end_date?: string;
models?: string[];
cell_selection?: "land" | "sea" | "nearest";
}
async function getOpenMeteoForecast(params: OMParams) {
const data = await stevekrouse.fetchJSON(
`https://api.open-meteo.com/v1/forecast?${new URLSearchParams(
params as {} // Typescript is stricter than Deno here
).toString()}`
);
if (Array.isArray(data.hourly?.time)) {
data.hourly.time = await me.toDatesWithTz(
data.hourly.time,
data.timezone
);
}
if (Array.isArray(data.daily?.time)) {
data.daily.time = data.daily.time.map((s) => new Date(s));
}
return data;
}
1
2
3
4
5
6
7
8
9
10
11
// Outputs, e.g., "It is currently Sunday, May 21, 2023 at 12:11:04 PM Pacific Daylight Time."
// timeZone should be a tz identifier as listed at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
function formatCurrentDatetimeSentence(timeZone: string): string {
const dateFormatter = new Intl.DateTimeFormat([], {
dateStyle: "full",
timeStyle: "full",
timeZone,
});
const now = dateFormatter.format(Date.now());
return `It is currently ${now}.`;
}
1
2
3
4
5
6
7
8
// Currently broken! Val.Town cannot fetch from .gov sites
// You can get these input values by calling https://api.weather.gov/points/{latitude},{longitude}
async function getWeatherGovData({ office, gridX, gridY }) {
const data = await stevekrouse.fetchJSON(
`https://api.weather.gov/gridpoints/${office}/${gridX},${gridY}/forecast`
);
return data.properties;
}
Get limit
random public domain images from the Art Institute of Chicago
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
const artICGetRandom = (limit: number) =>
stevekrouse.fetchJSON("https://api.artic.edu/api/v1/search", {
method: "POST",
headers: {
"AIC-User-Agent": "https://www.val.town/v/wilt.artICGetRandom",
},
body: JSON.stringify({
resources: "artworks",
fields: [
"id",
"title",
"artist_title",
"image_id",
"date_display",
"thumbnail",
],
limit,
boost: false,
query: {
function_score: {
query: {
bool: {
filter: [
{ term: { is_public_domain: true } },
{ exists: { field: "image_id" } },
{ exists: { field: "thumbnail.alt_text" } },
],
},
},
boost_mode: "replace",
random_score: { field: "id", seed: Date.now() },
},
},
}),
}).then((resp) => ({
...resp.info,
art: resp.data.map((item) => ({
...item,
image_url:
`${resp.config.iiif_url}/${item.image_id}/full/843,/0/default.jpg`,
art_url: `${resp.config.website_url}/artworks/${item.id}`,
})),
}));
1
2
3
4
5
6
7
8
9
async function getYoutubeLinkFromPageAPI(req: Request) {
const url = new URL(req.url).searchParams.get("url");
if (!url || url === "undefined")
return new Response("", { status: 400, statusText: "Bad Request" });
return Response.json({
page: url,
youtube: await wilt.getYoutubeLinkFromPage(url),
});
}
1
2
3
4
5
6
7
8
9
const apiTest = async (arg: Request) => {
return Response.json({
ok: true,
t: typeof arg,
i: arg instanceof Request,
// headers: Object.fromEntries(arg.headers.entries()),
// params: @neverstew.queryParams(arg),
});
};
1
2
3
4
5
6
async function getYoutubeLinkFromPage(url: string) {
const resp = await fetch(url);
const htmlText = await resp.text();
const match = htmlText.match(/https:\/\/youtube.com\/[^"]+/);
return Array.isArray(match) ? match[0] : match;
}
1
2
3
4
let untitled_tealYak = (req, res) => {
res.status(200);
res.send(me.untitled_goldLungfish);
};