-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
40 lines (33 loc) · 1.21 KB
/
index.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
import * as fs from "fs";
import * as path from "path";
import * as dotenv from "dotenv";
import { createLanguageModel, createJsonTranslator } from "typechat";
import { SentimentResponse } from "./sentimentSchema";
// TODO: use local .env file.
dotenv.config({ path: path.join(__dirname, ".env") });
const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join("./", "sentimentSchema.ts"), "utf8");
const translator = createJsonTranslator<SentimentResponse>(model, schema, "SentimentResponse");
export class Sentiment {
/** 获取文件内容 */
public getFile(fileName: string) {
return fs.readFileSync(fileName).toString().split(/\r?\n/);
}
/** 获取接口数据 */
public async getResponse(str: string) {
const response = await translator.translate(str);
if (!response.success) {
return response.message + " - " + str;
}
return `The sentiment is ${response.data.sentiment}`
}
}
/** test demo */
// async function test() {
// const sentiment = new Sentiment()
// const strs = sentiment.getFile("input.txt")
// for (let i = 0; i < strs.length; i++) {
// console.log(await sentiment.getResponse(strs[i]))
// }
// }
// test()