substrate-shapeshift.web.val.run
Readme

Semantically map a JSON object to a target schema using Substrate.

🪩 To fork, sign up for Substrate to get your own API key and $50 free credits.

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
import { ComputeJSON, ComputeText, sb, Substrate } from "npm:substrate";
import { z } from "npm:zod";
import { zodToJsonSchema } from "npm:zod-to-json-schema";
const original = {
personalInfo: {
name: "John Doe",
age: 30,
},
occupation: "Software Engineer",
fullAddress: "123 Main St, Anytown",
address: {
street: "123 Main St",
city: "Anytown",
},
};
const substrate = new Substrate({ apiKey: Deno.env.get("SUBSTRATE_API_KEY") });
const TargetSchema = z.object({
fullName: z.string(),
yearsOld: z.number().int().nonnegative(),
profession: z.string(),
location: z.object({
streetAddress: z.string(),
cityName: z.string(),
}),
});
const json = new ComputeJSON({
prompt: `Translate the following JSON object to the target schema.
${JSON.stringify(original)}`,
json_schema: zodToJsonSchema(TargetSchema),
});
const res = await substrate.run(json);
export default async function render(req: Request): Promise<Response> {
if (new URL(req.url).pathname === "/robots.txt") {
return new Response("User-agent: *\nDisallow: /");
}
return new Response(`<pre>${JSON.stringify(res.get(json).json_object, null, 2)}</pre>`, {
headers: {
"Content-Type": "text/html",
},
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 1, 2024