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
import { getModelBuilder } from "https://esm.town/v/webup/getModelBuilder";
export const parserSampleJSONZod = (async () => {
const { z } = await import("npm:zod");
const { PromptTemplate } = await import("npm:langchain/prompts");
const { StructuredOutputParser } = await import(
"npm:langchain/output_parsers"
);
// We can use zod to define a schema for the output using the `fromZodSchema` method of `StructuredOutputParser`.
const parser = StructuredOutputParser.fromZodSchema(z.object({
answer: z.string().describe("answer to the user's question"),
sources: z.array(z.string()).describe(
"sources used to answer the question, should be websites.",
),
}));
const formatInstructions = parser.getFormatInstructions();
const prompt = new PromptTemplate({
template:
"Answer the users question as best as possible.\n{format_instructions}\n{question}",
inputVariables: ["question"],
partialVariables: { format_instructions: formatInstructions },
});
const builder = await getModelBuilder();
const model = await builder();
const input = await prompt.format({
question: "What is the capital of France?",
});
const response = await model.call(input);
console.log(input);
return await parser.parse(response);
})();
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!
October 23, 2023