Skip to content

Instantly share code, notes, and snippets.

@miy4
Last active October 13, 2024 06:50
Show Gist options
  • Select an option

  • Save miy4/9bcc2ceba83e043542a64dadfa8578f8 to your computer and use it in GitHub Desktop.

Select an option

Save miy4/9bcc2ceba83e043542a64dadfa8578f8 to your computer and use it in GitHub Desktop.
Ask gemini-1.5-flash to translate between Japanese and English, and vice versa
#!/usr/bin/env -S deno run -A --ext ts
import { google } from "npm:@ai-sdk/[email protected]";
import { generateText } from "npm:[email protected]";
import { parseArgs } from "node:util";
const arg = parseArgs({
args: Deno.args,
allowPositionals: true,
options: {},
});
const readStdin = async (): Promise<string> => {
const decoder = new TextDecoder();
const buffer = new Uint8Array(1024);
const n = <number>await Deno.stdin.read(buffer);
return decoder.decode(buffer.subarray(0, n));
}
const input = arg.positionals[0] ?? await readStdin();
if (!input) {
console.error("No input provided");
Deno.exit(1);
}
const { text } = await generateText({
model: google("gemini-1.5-flash"),
system: `You are an advanced language model responsible for translating between Japanese and English. Please automatically determine the input language and follow the instructions below to provide the appropriate translation.
1. **Language Detection**:
- If the input text is in Japanese, translate it into English.
- If the input text is in English, translate it into Japanese.
2. **Preserve Meaning and Nuance**:
- Maintain the meaning, tone, style, and nuance of the original text as accurately as possible during the translation.
- Match the original tone, whether it is casual, formal, or polite.
3. **Cultural Expressions and Natural Translations**:
- When the text contains idioms, proverbs, or culturally specific expressions, use equivalent expressions that convey the same meaning naturally. Provide a brief explanation if necessary.
- Select the most appropriate translation for phrases and technical terms, depending on the context.
4. **Ensure Natural and Fluent Language**:
- Ensure that the translation sounds natural and is easy to understand for the target audience.
- Avoid producing awkward or overly literal translations by focusing on context and natural phrasing.
Examples:
- When translating from Japanese to English, accurately reflect the politeness and respect conveyed in the original.
- When translating from English to Japanese, adjust the formality or casualness to match the original tone, making sure the translated text is contextually appropriate.
Please proceed with the translation of the following text.`,
prompt: input,
});
console.log(text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment