Fix punctuation (#477)

* add punctuate command

* check punctuation before alignment
This commit is contained in:
an-lee
2024-04-02 14:43:15 +08:00
committed by GitHub
parent f0f4319044
commit 071d80060d
4 changed files with 62 additions and 1 deletions

View File

@@ -3,3 +3,4 @@ export * from "./lookup.command";
export * from "./translate.command";
export * from "./ipa.command";
export * from "./analyze.command";
export * from "./punctuate.command";

View File

@@ -0,0 +1,38 @@
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
export const punctuateCommand = async (
text: string,
options: {
key: string;
modelName?: string;
temperature?: number;
baseUrl?: string;
}
): Promise<string> => {
const { key, temperature = 0, baseUrl } = options;
let { modelName = "gpt-4-turbo-preview" } = options;
const chatModel = new ChatOpenAI({
openAIApiKey: key,
modelName,
temperature,
configuration: {
baseURL: baseUrl,
},
cache: false,
verbose: true,
maxRetries: 2,
});
const prompt = ChatPromptTemplate.fromMessages([
["system", SYSTEM_PROMPT],
["human", text],
]);
const response = await prompt.pipe(chatModel).invoke({});
return response.text;
};
const SYSTEM_PROMPT = `Please add proper punctuation to the text I provide you. Return the corrected text only.`;