-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translate): implement and tests generating translations with LLMs
- Loading branch information
Showing
9 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ package-lock.json | |
tsconfig.json | ||
dist | ||
bun.lockb | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
import { create } from "logua"; | ||
import { create } from 'logua' | ||
import { Language } from './types' | ||
|
||
export const log = create("epic-language", "magenta"); | ||
export const log = create('epic-language', 'magenta') | ||
|
||
export const readableLanguage = { | ||
[Language.en]: 'English', | ||
[Language.es]: 'Spanish', | ||
[Language.zh]: 'Chinese', | ||
[Language.de]: 'German', | ||
[Language.fr]: 'French', | ||
[Language.it]: 'Italian', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { test, expect } from 'bun:test' | ||
import { translate } from '../translate' | ||
import { Language } from '../types' | ||
|
||
// Bun automatically reads env variables. | ||
|
||
test('Translates single key to various languages.', async () => { | ||
const inputSheet = JSON.stringify({ title: 'My Title' }) | ||
const sheetSpanish = await translate(inputSheet, Language.es) | ||
const sheetChinese = await translate(inputSheet, Language.zh) | ||
const sheetGerman = await translate(inputSheet, Language.de) | ||
|
||
// Casing can vary in Spanish. | ||
expect(sheetSpanish.title.toLowerCase()).toBe('Mi Título'.toLowerCase()) | ||
expect(sheetChinese.title).toBe('我的标题') | ||
expect(sheetGerman.title).toBe('Mein Titel') | ||
}) | ||
|
||
test('Translates several to various languages.', async () => { | ||
const inputSheet = JSON.stringify({ title: 'My Title', description: 'This is the description.' }) | ||
const sheetFrench = await translate(inputSheet, Language.fr) | ||
const sheetChinese = await translate(inputSheet, Language.zh) | ||
const sheetGerman = await translate(inputSheet, Language.de) | ||
|
||
expect(sheetFrench.title).toBe('Mon titre') | ||
// Ceci est la description. / Voici la description. | ||
expect(sheetFrench.description).toContain('la description') | ||
|
||
expect(sheetChinese).toEqual({ | ||
title: '我的标题', | ||
description: '这是描述。', | ||
}) | ||
|
||
expect(sheetGerman).toEqual({ | ||
title: 'Mein Titel', | ||
description: 'Dies ist die Beschreibung.', | ||
}) | ||
}, 20000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import OpenAI from 'openai' | ||
import type { Language } from './types' | ||
import { readableLanguage } from './helper' | ||
|
||
const openai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
organization: 'org-jec41Owv49cySXoL8adosmOh', | ||
}) | ||
|
||
export async function translate(input: string, language: Language) { | ||
const prompt = `Translate the following object values to ${readableLanguage[language]} keeping the object keys intact: | ||
${input}` | ||
|
||
const chatCompletion = await openai.chat.completions.create({ | ||
model: 'gpt-3.5-turbo', | ||
stream: false, | ||
messages: [{ role: 'user', content: prompt }], | ||
}) | ||
|
||
const { choices } = chatCompletion | ||
|
||
if (choices.length === 1) { | ||
return JSON.parse(choices[0].message.content ?? '{}') | ||
} | ||
|
||
return {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters