-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathllm.js
63 lines (56 loc) · 1.89 KB
/
llm.js
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
import { PromptTemplate } from "@langchain/core/prompts";
import { JSONLoader } from "langchain/document_loaders/fs/json";
import { OpenAI } from "@langchain/openai";
import { StructuredOutputParser } from "langchain/output_parsers";
import { RunnableSequence } from "@langchain/core/runnables";
import { OpenAIEmbeddings } from "@langchain/openai";
import { z } from "zod";
import "dotenv/config";
export async function generateEmbeddings(property) {
const stringifiedProperty = JSON.stringify(property);
const embeddings_model = new OpenAIEmbeddings({
apiKey: process.env.OPENAI_API_KEY,
});
return embeddings_model.embedQuery(stringifiedProperty);
}
const llmApi = async (description) => {
const llm = new OpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
});
const parser = StructuredOutputParser.fromZodSchema(
z.object({
price_ending: z
.string()
.describe("Ending price of budget. Return 1000000 if not passed"),
price_starting: z
.string()
.describe("Starting price of budget. Return 0 if not passed"),
bedrooms: z
.number()
.describe("Number of bedrooms. Return 1 if not passed"),
bathrooms: z
.number()
.describe("Number of bathrooms. Return 1 if not passed"),
nice_to_haves: z
.string()
.array()
.describe(
"Nice to haves. Return as an array of string. If nothing is passed, return backyard."
),
})
);
const chain = RunnableSequence.from([
PromptTemplate.fromTemplate(
"Parse the description provided by user to extract information about real estate preferences.\n{format_instructions}\n{description}."
),
llm,
parser,
]);
const response = await chain.invoke({
description: description,
format_instructions: parser.getFormatInstructions(),
});
console.log(response);
return response;
};
export default llmApi;