LibreChat/config/translations/anthropic.ts
Danny Avila 094a40dbb0
🌏 i18n: Added Missing Localizations (Ar, De, Es, Fr, It, Jp, Ko, Ru, Zh) (#4540)
* chore: remove comparisons

* feat: use prompt caching for translations

* chore: wip translation readme

* i18n: korean translations

* refactor: use promises for faster translation processing

* refactor: update translation model to 'claude-3-5-sonnet-20241022'

* refactor: optimize sleep duration for translation processing

* i18n: add missing keys

* refactor: standardize languages in their own respective languages

* Refactor translation instructions in README.md
2024-10-24 10:48:57 -04:00

85 lines
2.8 KiB
TypeScript

import Anthropic from '@anthropic-ai/sdk';
import type * as a from '@anthropic-ai/sdk';
import { parseParamFromPrompt, genTranslationPrompt } from '~/app/clients/prompts/titlePrompts';
/**
* Get the initialized Anthropic client.
* @returns {Anthropic} The Anthropic client instance.
*/
export function getClient() {
/** @type {Anthropic.default.RequestOptions} */
const options = {
apiKey: process.env.ANTHROPIC_API_KEY,
defaultHeaders: {
'anthropic-beta': 'prompt-caching-2024-07-31',
},
};
return new Anthropic(options);
}
/**
* This function capitlizes on [Anthropic's function calling training](https://docs.anthropic.com/claude/docs/functions-external-tools).
*
* @param {Object} params - The parameters for the generation.
* @param {string} params.key
* @param {string} params.baselineTranslation
* @param {string} params.translationPrompt
* @param {Array<{ pageContent: string }>} params.context
*
* @returns {Promise<string | 'New Chat'>} A promise that resolves to the generated conversation title.
* In case of failure, it will return the default title, "New Chat".
*/
export async function translateKeyPhrase({ key, baselineTranslation, translationPrompt, context }) {
let translation: string | undefined;
const model = 'claude-3-5-sonnet-20241022';
const prompt = genTranslationPrompt(translationPrompt);
const system = prompt;
const translateCompletion = async () => {
const text = `Current key: \`${key}\`
Baseline translation: ${baselineTranslation}
Please generate a translation for the key in the target language as described by the function.
Similar key and phrases: ${context.map((c) => c.pageContent).join(', ')}
Remember to invoke the tool with proper tool invocation; e.g.:
<invoke>\n<tool_name>submit_translation</tool_name>\n<parameters>\n<translation>Your Translation Here</translation>\n</parameters>\n</invoke>`;
const message: a.Anthropic.MessageParam = {
role: 'user',
content: [
{
type: 'text',
text,
/* @ts-ignore */
cache_control: { type: 'ephemeral' },
},
],
};
const requestOptions: a.Anthropic.MessageCreateParamsNonStreaming = {
model,
temperature: 0.3,
max_tokens: 1024,
system,
stop_sequences: ['\n\nHuman:', '\n\nAssistant', '</function_calls>'],
messages: [message],
stream: false,
};
try {
const client = getClient();
const response = await client.messages.create(requestOptions);
const text = response.content[0].text;
translation = parseParamFromPrompt(text, 'translation');
} catch (e) {
console.error('[AnthropicClient] There was an issue generating the translation', e);
}
};
await translateCompletion();
return translation;
}