forked from starmorph/langchain-js-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
57 lines (43 loc) · 1.55 KB
/
app.ts
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
import { OpenAI, PromptTemplate } from "langchain";
import { StructuredOutputParser } from "langchain/output_parsers";
import fs from 'fs';
import { ChatOpenAI } from "langchain/chat_models";
import { HumanChatMessage, SystemChatMessage } from "langchain/schema";
//Load environment variables (populate process.env from .env file)
import * as dotenv from "dotenv";
dotenv.config();
const chat = new ChatOpenAI({ temperature: 0 });
export const run = async () => {
const response = await chat.call([
new HumanChatMessage(
"give me a nextjs component that renders an h1 tag with the text Starmorph'"
),
]);
try {
await fs.promises.writeFile('response.txt', JSON.stringify(response, null, 2));
console.log('File saved successfully.');
} catch (err) {
console.error('Error writing file:', err);
}
const removeEscapedCharacters = (str: string) => {
return str.replace(/\\n/g, '\n');
};
const processFile = async () => {
try {
// Read the JSON file
const fileContent = await fs.promises.readFile('response.txt', 'utf8');
// Parse the JSON content
const jsonData = JSON.parse(fileContent);
// Extract the 'text' property and remove escaped characters
const text = removeEscapedCharacters(jsonData.text);
// Save the result to a new file
await fs.promises.writeFile('output.txt', text);
console.log('File processed successfully.');
} catch (err) {
console.error('Error processing file:', err);
}
};
processFile();
console.log(response);
};
run();