fkirc / attranslate

A command line tool for translating JSON, YAML, CSV, ARB, XML (via a CLI)
https://www.npmjs.com/package/attranslate
Other
336 stars 27 forks source link

Add TypeChat service #256

Closed abichinger closed 1 year ago

abichinger commented 1 year ago

Servus!

Thanks for this awesome tool!

I added a new services which uses OpenAI and TypeChat to translate all strings.

New services:

Environment variables: Name Default
OPENAI_API_KEY required
OPENAI_MODEL text-davinci-003
OPENAI_ENDPOINT https://api.openai.com/v1/chat/completions
OPEN_AI_BATCH_SIZE 10
TYPECHAT_SCHEMA_NAME AppLocalizations
TYPECHAT_SCHEMA_COMMENT undefined

I wasn't able to test it with the official OpenAI API, because I don't have an OpenAI API key. But it worked with a local llama-cpp-python web server.

Example

A generated prompt will look like this:

You are a service that translates user requests into JSON objects of type "AppLocalizations" according to the following TypeScript definitions:
\`\`\`
export interface AppLocalizations {
  globalRankings: string;
  share: string;
  shareText: string;
  rank: string;
  train: string;
  forfeit: string;
  forfeitContent: string;
  yes: string;
  no: string;
}
\`\`\`
The following is a user request:
"""
Translate the following JSON object from English into German:
{
  "globalRankings": "Global Rankings",
  "share": "Share",
  "shareText": "Yo! Check out Nono Battle.",
  "rank": "Rank",
  "train": "Practice",
  "forfeit": "Forfeit",
  "forfeitContent": "Are you sure you want to forfeit?",
  "yes": "Yes",
  "no": "No"
}
"""
The following is the user request translated into a JSON object with 2 spaces of indentation and no properties with the value undefined:

Response from ChatGPT:

Of course, here's the translated JSON object in German according to the provided TypeScript definitions:

\`\`\`json
{
  "globalRankings": "Weltweite Rangliste",
  "share": "Teilen",
  "shareText": "Hey! Schau dir Nono Battle an.",
  "rank": "Rang",
  "train": "Üben",
  "forfeit": "Aufgeben",
  "forfeitContent": "Bist du sicher, dass du aufgeben möchtest?",
  "yes": "Ja",
  "no": "Nein"
}
\`\`\`

The JSON object is formatted with 2 spaces of indentation, and properties with the value undefined have been omitted. If you need any further assistance or modifications, feel free to let me know!
fkirc commented 1 year ago

Thank you for submitting the PR! The typechat-approach looks interesting and could be an alternative to the current openai-service with a primitive prompt-engineering.

However, there is currently the problem that the typechat-tests are failing in the CI: https://github.com/fkirc/attranslate/actions/runs/5770043007/job/15643564890?pr=256 image

Based on the test-summary, it seems that some module-import within the typechat-package is failing?

abichinger commented 1 year ago

Looks like typechat requires node >= 18. (package.json)

I added a check to ensure the correct node version is installed.

fkirc commented 1 year ago

After upgrading node to v18, I see a new CI-error:

src/util/util.ts:10:20 - error TS7016: Could not find a declaration file for module 'semver'. '/home/runner/work/attranslate/attranslate/node_modules/semver/index.js' implicitly has an 'any' type. Try npm i --save-dev @types/semver if it exists or add a new declaration (.d.ts) file containing declare module 'semver';

10 import semver from "semver";



Found 1 error.
abichinger commented 1 year ago

Oops, I forgot to commit the package.json file

fkirc commented 1 year ago

Now the CI passes again, I merged typechat and released version 2.1.0!