Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cohere as new translate engine #1250

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Pfad für benutzerdefinierten Namespace. Dokumentation für weitere details prüfen.",
"config.preferred_delimiter": "Bevorzugtes Trennzeichen für zusammengesetzte Schlüsse. Standardwert ist \"-\"",
"config.prompt_translating_source": "Aufforderung zur Auswahl der Quellsprache bei jeder Verwendung. Wenn false, die Quellsprache wird aus der Konfiguration gelesen.",
Expand Down
4 changes: 4 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "OpenAI API key",
"config.openai_api_model": "OpenAI chatgpt model",
"config.openai_api_root": "OpenAI API root URL",
"config.cohere_api_key": "Cohere API key",
"config.cohere_api_model": "Cohere Chat model",
"config.cohere_api_root": "Cohere API root URL",
"config.cohere_translate_instructions": "Cohere custom instructions for translating text",
"config.path_matcher": "Match path for custom locale/namespace. Check out the docs for more details.",
"config.preferred_delimiter": "Preferred delimiter of composed keypath, default to \"-\"(dash)",
"config.prompt_translating_source": "Prompt to select source locale on translating every time. If set false, the source language in the config will be used.",
Expand Down
4 changes: 4 additions & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Igual el path para configuraciones regionales/espacios de nombres personalizados. Comprueba la documentación para más detalles.",
"config.preferred_delimiter": "Delimitador preferido para las rutas de acceso compuestas, por defecto \"-\"(guión)",
"config.prompt_translating_source": "Solicitud para seleccionar la configuración regional de origen al traducir. Si se establece en false, se utilizará el idioma de origen en la configuración.",
Expand Down
4 changes: 4 additions & 0 deletions locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Faire correspondre le chemin pour les locales/namespaces personnalisés. Consultez la documentation pour plus de détails.",
"config.preferred_delimiter": "Délimiteur recommandé pour les noms de chemins à trous, par défaut « - » (tiret)",
"config.prompt_translating_source": "Invite à sélectionner la langue source à chaque fois. Si défini sur False, le langage du système sera automatiquement utilisé.",
Expand Down
4 changes: 4 additions & 0 deletions locales/hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "A locale / névterekhez tartozó egyezési út. További információért lásd a dokumentációt.",
"config.preferred_delimiter": "Az összetett kulcsú útvonalak preferált határolója, alapértelmezetten az - (kötőjel)",
"config.prompt_translating_source": "Mindig kérdezze meg forrás nyelvre fordításkor. Ha hamisra van állítva, a konfigurációban megadott forrásnyelv lesz használva.",
Expand Down
4 changes: 4 additions & 0 deletions locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "",
"config.preferred_delimiter": "キーパスを構成するデリミタ。デフォルトは\"-\"(ダッシュ)",
"config.prompt_translating_source": "",
Expand Down
4 changes: 4 additions & 0 deletions locales/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "사용자 정의 로케일/네임스페이스에 대한 일치 경로. \n자세한 내용은 문서를 확인하세요.",
"config.preferred_delimiter": "구성된 키 경로의 기본 구분 기호, 기본값은 \"-\"(대시)",
"config.prompt_translating_source": "번역할 때마다 소스 로케일을 선택하라는 메시지가 표시됩니다. \nfalse로 설정하면 구성의 소스 언어가 사용됩니다.",
Expand Down
4 changes: 4 additions & 0 deletions locales/nb-NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "",
"config.preferred_delimiter": "Foretrukket skilletegn for komponert nøkkelbane, \"-\"(bindestrek) som standard",
"config.prompt_translating_source": "",
Expand Down
4 changes: 4 additions & 0 deletions locales/nl-NL.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Pad voor onderscheiding van sleutels op basis van folderstructuur/bestandsnaam. Zie documentatie voor meer uitleg.",
"config.preferred_delimiter": "Gewenst scheidingsteken voor sleutel pad, standaard waarde \"-\"(dash)",
"config.prompt_translating_source": "Altijd vragen om de bron locale te selectering bij vertalingen. Indien false, dan zal de bron taal in de configuratie gebruikt worden.",
Expand Down
4 changes: 4 additions & 0 deletions locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "",
"config.preferred_delimiter": "Delimitador para o caminho da chave, o padrão é \"-\"(hífen)",
"config.prompt_translating_source": "Sempre perguntar idioma de origem durante tradução. Se o valor for \"false\", será usado o idioma de origem das configurações.",
Expand Down
4 changes: 4 additions & 0 deletions locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Сопоставить путь для пользовательского перевода/пространства имен. Подробности смотрите в документации.",
"config.preferred_delimiter": "Желаемый разделитель пути составного ключа, значение по умолчанию \"-\"(тире)",
"config.prompt_translating_source": "Предлагать выбрать исходный перевод при переводе каждый раз. Если установлено false, будет использоваться исходный перевод из конфигурации.",
Expand Down
4 changes: 4 additions & 0 deletions locales/sv-SE.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Matcha sökväg för egna locale-filer/namespace. Se dokumentation för mer detaljer.",
"config.preferred_delimiter": "Föredragen avskiljare av sammansatta nyckelsökvägar, som standard satt till \"-\" (bindestreck)",
"config.prompt_translating_source": "Fråga om att välja locale-filskälla varje gång vid översättning. Om satt till `false` kommer locale-filskällan i konfigurationen användas.",
Expand Down
4 changes: 4 additions & 0 deletions locales/th.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "จับคู่เส้นทางสำหรับภาษา/namespace ที่กำหนดเอง ตรวจสอบเอกสารสำหรับรายละเอียดเพิ่มเติม",
"config.preferred_delimiter": "ตัวคั่นที่ต้องการของเส้นทางที่ประกอบขึ้น ค่าเริ่มต้นคือ \"-\"(เส้นประ)",
"config.prompt_translating_source": "พร้อมท์ให้เลือกภาษาต้นทางในการแปลทุกครั้ง หากตั้งค่าเป็นเท็จ ภาษาต้นทางในการกำหนดค่าจะถูกใช้",
Expand Down
4 changes: 4 additions & 0 deletions locales/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Dosya yolunu özelleştirilmiş bir dil/namespace'e göre eşleştirin. Daha fazla ayrıntı için dokümantasyona göz atın.",
"config.preferred_delimiter": "Bileşik anahtar yolları için ayıran karakter, varsayılan \"-\" (tire)",
"config.prompt_translating_source": "Her seferinde çeviride kaynak dili seçmesini isteyin. Eğer false olarak ayarlıysa, yapılandırmadaki kaynak dil kullanılacaktır.",
Expand Down
4 changes: 4 additions & 0 deletions locales/uk-UA.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Шлях для розрізнення ключів на основі структури папок/імені файлу. Подробиці дивіться в документації.",
"config.preferred_delimiter": "Бажаний роздільник шляху складеного ключа, значення за замовчуванням \"-\"(тире)",
"config.prompt_translating_source": "Пропонувати обирати мовний стандарт кожен раз при перекладі. Якщо встановлено false, буде використовуватися мова з конфігурації.",
Expand Down
4 changes: 4 additions & 0 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "匹配自定义区域设置/命名空间的路径。查看文档了解更多详细信息。",
"config.preferred_delimiter": "组合键路径的默认分隔符,默认为 \"-\"(短划线)",
"config.prompt_translating_source": "每次翻译时都会提示选择源语言环境。\n如果设置为false,将使用配置中的源语言。",
Expand Down
4 changes: 4 additions & 0 deletions locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"config.openai_api_key": "",
"config.openai_api_model": "",
"config.openai_api_root": "",
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "匹配自定義區域設置/命名空間的路徑。\n查看文件了解更多詳細信息。",
"config.preferred_delimiter": "組合鍵路徑的首選分隔符,默認為 \"-\"(短劃線)",
"config.prompt_translating_source": "每次翻譯時都會提示選擇源語言環境。\n如果設置為false,將使用配置中的源語言。",
Expand Down
29 changes: 28 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,8 @@
"deepl",
"libretranslate",
"baidu",
"openai"
"openai",
"cohere"
]
},
"default": [
Expand Down Expand Up @@ -1127,6 +1128,32 @@
],
"description": "%config.openai_api_model%"
},
"i18n-ally.translate.cohere.apiKey": {
"type": "string",
"default": null,
"description": "%config.cohere_api_key%"
},
"i18n-ally.translate.cohere.apiRoot": {
"type": "string",
"default": "https://api.cohere.com",
"description": "%config.cohere_api_root%"
},
"i18n-ally.translate.cohere.apiModel": {
"type": "string",
"default": "command-r7b-12-2024",
"enum": [
"command-r7b-12-2024",
"command-r-plus-08-2024",
"command-r-08-2024"
],
"description": "%config.cohere_api_model%"
},
"i18n-ally.translate.cohere.translateInstructions": {
"type": "string",
"default": "You are a professional translation engine. Please translate text without explanation.",
"description": "%config.cohere_translate_instructions%",
"editPresentation": "multilineText"
},
"i18n-ally.usage.scanningIgnore": {
"type": "array",
"items": {
Expand Down
18 changes: 17 additions & 1 deletion src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class Config {
return this.getConfig<SortCompare>('sortCompare') || 'binary'
}

static get sortLocale(): string | undefined{
static get sortLocale(): string | undefined {
return this.getConfig<string>('sortLocale')
}

Expand Down Expand Up @@ -584,6 +584,22 @@ export class Config {
return this.getConfig<string>('translate.openai.apiModel') ?? 'gpt-3.5-turbo'
}

static get cohereApiKey() {
return this.getConfig<string | null | undefined>('translate.cohere.apiKey')
}

static get cohereApiRoot() {
return this.getConfig<string | null | undefined>('translate.cohere.apiRoot')
}

static get cohereApiModel() {
return this.getConfig<string>('translate.cohere.apiModel') ?? 'command-r7b-12-2024'
}

static get cohereTranslateInstructions() {
return this.getConfig<string>('translate.cohere.translateInstructions') ?? 'You are a professional translation engine. Please translate text without explanation.'
}

static get telemetry(): boolean {
return workspace.getConfiguration().get('telemetry.enableTelemetry') as boolean
}
Expand Down
67 changes: 67 additions & 0 deletions src/translators/engines/cohere.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import axios from 'axios'
import TranslateEngine, { TranslateOptions, TranslateResult } from './base'
import { Config } from '~/core'

export default class CohereTranslate extends TranslateEngine {
apiRoot = 'https://api.cohere.com'

async translate(options: TranslateOptions) {
const systemPrompt = Config.cohereTranslateInstructions
const apiKey = Config.cohereApiKey
let apiRoot = this.apiRoot
if (Config.cohereApiRoot) apiRoot = Config.cohereApiRoot.replace(/\/$/, '')
const model = Config.cohereApiModel

const response = await axios.post(
`${apiRoot}/v2/chat`,
{
model,
messages: [
{
role: 'system',
content: systemPrompt,
},
{
role: 'user',
content: this.generateUserPrompts(options),
},
],
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
'X-Client-Name': 'i18n-ally',
},
},
)

return this.transform(response, options)
}

transform(response: any, options: TranslateOptions): TranslateResult {
const { text, from = 'auto', to = 'auto' } = options

const translatedText = response.data.message.content[0].text.trim()

const r: TranslateResult = {
text,
to,
from,
response,
result: translatedText ? [translatedText] : undefined,
linkToResult: '',
}

return r
}

generateUserPrompts(options: TranslateOptions): string {
const sourceLang = options.from
const targetLang = options.to

const generatedUserPrompt = `translate from ${sourceLang} to ${targetLang}:\n\n${options.text}`

return generatedUserPrompt
}
}
3 changes: 3 additions & 0 deletions src/translators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import DeepLTranslateEngine from './engines/deepl'
import LibreTranslateEngine from './engines/libretranslate'
import BaiduTranslate from './engines/baidu'
import OpenAITranslateEngine from './engines/openai'
import CohereTranslateEngine from './engines/cohere'

export class Translator {
engines: Record<string, TranslateEngine> ={
Expand All @@ -14,6 +15,7 @@ export class Translator {
'libretranslate': new LibreTranslateEngine(),
'baidu': new BaiduTranslate(),
'openai': new OpenAITranslateEngine(),
'cohere': new CohereTranslateEngine(),
}

async translate(options: TranslateOptions & { engine: string }) {
Expand All @@ -30,6 +32,7 @@ export {
LibreTranslateEngine,
BaiduTranslate,
OpenAITranslateEngine,
CohereTranslateEngine,
}

export * from './engines/base'