@collectionscms/plugin-text-generator
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "@collectionscms/plugin-text-generator", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Text Generation Plugin for Collections.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -7,4 +7,4 @@ import * as deepl from "deepl-node"; | ||
model: string; | ||
constructor(deepLAuthKey: string, openAiAuthKey: string, model: string); | ||
rewriteBodyTextByOpenAI: (englishName: string, bodyText: string) => Promise<string>; | ||
constructor(translatorAuthKey: string, generatorAuthKey: string, model: string); | ||
rewriteParagraphByOpenAI: (englishName: string, bodyText: string) => Promise<string>; | ||
translate({ title, subtitle, body, sourceLang, targetLang, targetLangEnglishName, }: { | ||
@@ -11,0 +11,0 @@ title: string; |
import * as deepl from "deepl-node"; | ||
import OpenAI from "openai"; | ||
export class Translator { | ||
constructor(deepLAuthKey, openAiAuthKey, model) { | ||
this.rewriteBodyTextByOpenAI = async (englishName, bodyText) => { | ||
constructor(translatorAuthKey, generatorAuthKey, model) { | ||
this.rewriteParagraphByOpenAI = async (englishName, bodyText) => { | ||
const paragraphs = bodyText.match(/<p>(.*?)<\/p>/g)?.map((p) => p.replace(/<\/?p>/g, "")) || | ||
[]; | ||
const orderTexts = paragraphs.map((p) => `- ${p}`).join("\n"); | ||
const completion = await this.openai.chat.completions.create({ | ||
@@ -11,11 +14,23 @@ model: this.model, | ||
role: "user", | ||
content: `Rewrite the text in easy-to-read ${englishName}. ${bodyText}`, | ||
content: ` | ||
Rewrite the text in easy-to-read ${englishName}. | ||
${orderTexts} | ||
`, | ||
}, | ||
], | ||
}); | ||
return completion.choices[0].message.content ?? ""; | ||
const content = completion.choices[0].message.content ?? ""; | ||
const rewrittenParagraphs = content | ||
.split("\n") | ||
.map((c) => c.replace(/^-/, "").trim()); | ||
if (paragraphs.length !== rewrittenParagraphs.length) | ||
return bodyText; | ||
paragraphs.forEach((p, i) => { | ||
bodyText = bodyText.replace(p, rewrittenParagraphs[i]); | ||
}); | ||
return bodyText; | ||
}; | ||
this.translator = new deepl.Translator(deepLAuthKey); | ||
this.translator = new deepl.Translator(translatorAuthKey); | ||
this.openai = new OpenAI({ | ||
apiKey: openAiAuthKey, | ||
apiKey: generatorAuthKey, | ||
}); | ||
@@ -34,3 +49,3 @@ this.model = model; | ||
const translatedBody = body | ||
? await this.rewriteBodyTextByOpenAI(targetLangEnglishName, translatedTexts[translatedTexts.length - 1].text) | ||
? await this.rewriteParagraphByOpenAI(targetLangEnglishName, translatedTexts[translatedTexts.length - 1].text) | ||
: ""; | ||
@@ -37,0 +52,0 @@ return { |
@@ -11,58 +11,79 @@ /* eslint-disable max-len */ | ||
const translator = new Translator(translatorApiKey, openAiApiKey, "gpt-4o-mini"); | ||
it.only("should be translated, title and subtitle, body", async () => { | ||
const result = await translator.translate({ | ||
title: "Introducing ChatGPT search", | ||
subtitle: "Get fast, timely answers with links to relevant web sources.", | ||
body: "ChatGPT can now search the web in a much better way than before. You can get fast, timely answers with links to relevant web sources, which you would have previously needed to go to a search engine for. This blends the benefits of a natural language interface with the value of up-to-date sports scores, news, stock quotes, and more.", | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
describe("response check", () => { | ||
const title = "Introducing ChatGPT search"; | ||
const subtitle = "Get fast, timely answers with links to relevant web sources."; | ||
const body = "<p>ChatGPT can now search the web in a much better way than before. You can get fast, timely answers with links to relevant web sources, which you would have previously needed to go to a search engine for. This blends the benefits of a natural language interface with the value of up-to-date sports scores, news, stock quotes, and more.</p>"; | ||
it("should be translated, title and subtitle, body", async () => { | ||
const result = await translator.translate({ | ||
title, | ||
subtitle, | ||
body, | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBeTruthy(); | ||
expect(result.subtitle).toBeTruthy(); | ||
expect(result.body).toBeTruthy(); | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBeTruthy(); | ||
expect(result.subtitle).toBeTruthy(); | ||
expect(result.body).toBeTruthy(); | ||
}); | ||
it("should be translated, only title", async () => { | ||
const result = await translator.translate({ | ||
title: "Introducing ChatGPT search", | ||
subtitle: "", | ||
body: "", | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
it("should be translated, only title", async () => { | ||
const result = await translator.translate({ | ||
title, | ||
subtitle: "", | ||
body: "", | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBeTruthy(); | ||
expect(result.subtitle).toBe(""); | ||
expect(result.body).toBe(""); | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBeTruthy(); | ||
expect(result.subtitle).toBe(""); | ||
expect(result.body).toBe(""); | ||
}); | ||
it("should be translated, title and subtitle", async () => { | ||
const result = await translator.translate({ | ||
title: "Introducing ChatGPT search", | ||
subtitle: "Get fast, timely answers with links to relevant web sources.", | ||
body: "", | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
it("should be translated, title and subtitle", async () => { | ||
const result = await translator.translate({ | ||
title, | ||
subtitle, | ||
body: "", | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBeTruthy(); | ||
expect(result.subtitle).toBeTruthy(); | ||
expect(result.body).toBe(""); | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBeTruthy(); | ||
expect(result.subtitle).toBeTruthy(); | ||
expect(result.body).toBe(""); | ||
it("should be translated, only body", async () => { | ||
const result = await translator.translate({ | ||
title: "", | ||
subtitle: "", | ||
body, | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBe(""); | ||
expect(result.subtitle).toBe(""); | ||
expect(result.body).toBeTruthy(); | ||
}); | ||
}); | ||
it("should be translated, only body", async () => { | ||
const result = await translator.translate({ | ||
title: "", | ||
subtitle: "", | ||
body: "ChatGPT can now search the web in a much better way than before. You can get fast, timely answers with links to relevant web sources, which you would have previously needed to go to a search engine for. This blends the benefits of a natural language interface with the value of up-to-date sports scores, news, stock quotes, and more.", | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
describe.only("rewrite variation", () => { | ||
it("should be rewritten paragraph", async () => { | ||
const result = await translator.translate({ | ||
title: "", | ||
subtitle: "", | ||
body: "<p>Go straight to the source</p><pre><code># Mocked approach</code></pre><p>Chats now include links to sources, such as news articles and blog posts, giving you a way to learn more</p>", | ||
sourceLang: "en", | ||
targetLang: "ja", | ||
targetLangEnglishName: "Japanese", | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBe(""); | ||
expect(result.subtitle).toBe(""); | ||
expect(result.body).toBeTruthy(); | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBe(""); | ||
expect(result.subtitle).toBe(""); | ||
expect(result.body).toBeTruthy(); | ||
}); | ||
}); |
11802
269