Skip to content

fix(deps): update dependencies to use google/genai #1021

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

Merged
merged 1 commit into from
Apr 10, 2025
Merged
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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
},
"packageManager": "[email protected]",
"devDependencies": {
"@google/generative-ai": "0.21.0",
"@google/genai": "^0.8.0",
"@types/node": "20.14.10",
"chokidar": "3.6.0",
"consola": "3.2.3",
"execa": "^9.3.0",
"globby": "14.0.2",
"prh-rules": "prh/rules",
"rxjs": "7.8.1",
"sitemap": "8.0.0",
"textlint": "^14.0.4",
Expand Down
2 changes: 1 addition & 1 deletion prh.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 1
imports:
- path: ./node_modules/prh-rules/files/markdown.yml
- path: ./node_modules/prh/prh-rules/files/markdown.yml
rules:
# 言い換え

Expand Down
52 changes: 24 additions & 28 deletions tools/translator/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
* 発行した API キーは環境変数 GOOGLE_API_KEY に設定してください。
*/

import { GoogleGenerativeAI } from '@google/generative-ai';
import { GoogleAIFileManager } from '@google/generative-ai/server';
import { GoogleGenAI } from '@google/genai';
import { setTimeout } from 'node:timers/promises';
import { renderMarkdown, splitMarkdown } from './markdown';

export class GeminiTranslator {
readonly #genAI: GoogleGenerativeAI;
readonly #fileManager: GoogleAIFileManager;
readonly #client: GoogleGenAI;

constructor(apiKey: string) {
this.#genAI = new GoogleGenerativeAI(apiKey);
this.#fileManager = new GoogleAIFileManager(apiKey);
this.#client = new GoogleGenAI({ apiKey });
}

async translate(content: string, prh: string): Promise<string> {
Expand All @@ -30,7 +27,6 @@ ${content}
あなたはこのMarkdownファイルを段落ごとに分割したテキストを受け取ります。
次のルールに従って、受け取ったテキストを翻訳してください。

- 入出力の形式 入力: { text: "## Hello" } 出力: { text: "## こんにちは" }
- 見出しのレベルを維持する。
- 改行やインデントの数を維持する。
- 英単語の前後にスペースを入れない。
Expand All @@ -43,32 +39,32 @@ ${prh}

`.trim();

const model = this.#genAI.getGenerativeModel({
model: 'gemini-2.0-flash',
generationConfig: {
responseMimeType: 'application/json',
temperature: 0.2,
},
systemInstruction,
});

const chatSession = model.startChat({});

const blocks = splitMarkdown(content);
const translated = [];

for (const block of blocks) {
const { response } = await chatSession.sendMessage([
{
text: '次のテキストに含まれる英語を日本語に翻訳してください。\n\n',
},
{
text: JSON.stringify({ text: block }),
const prompt = `
次のテキストに含まれる英語を日本語に翻訳してください。

${block}
`.trim();

const response = await this.#client.models.generateContent({
model: 'gemini-2.0-flash',
contents: [prompt],
config: {
systemInstruction,
temperature: 0.1,
},
]);
const { text: translatedText } = JSON.parse(response.text());
translated.push(translatedText);
await setTimeout(3000);
});

if (response.text) {
Copy link
Preview

Copilot AI Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional check on response.text may inadvertently treat a valid empty string as a missing response. Consider explicitly checking for undefined or null to distinguish an empty but valid response from an error.

Suggested change
if (response.text) {
if (response.text !== undefined && response.text !== null) {

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

translated.push(response.text);
} else {
translated.push(''); // Fallback in case of no response
}

await setTimeout(3000); // Rate limiting
}
return renderMarkdown(translated);
}
Expand Down
Loading