Skip to content

Instantly share code, notes, and snippets.

@k0ta0uchi
Last active March 16, 2025 22:36
Show Gist options
  • Save k0ta0uchi/61890e6d07b311a293ad0194a1807c61 to your computer and use it in GitHub Desktop.
Save k0ta0uchi/61890e6d07b311a293ad0194a1807c61 to your computer and use it in GitHub Desktop.
C#用 Geminiでの翻訳例
using System.Text;
using System.Text.Json;
public class GoogleAI
{
private readonly string _apiKey;
private readonly HttpClient _httpClient;
public GoogleAI(string apiKey)
{
_apiKey = apiKey;
_httpClient = new HttpClient();
}
public async Task<GenerateContentResponse> GenerateContentObject(string systemInstructionText, string contentText, string modelName)
{
var url = $"https://generativelanguage.googleapis.com/v1beta/models/{modelName}:generateContent?key={_apiKey}";
var requestJson = new
{
system_instruction = new
{
parts = new[]
{
new { text = systemInstructionText }
}
},
contents = new[]
{
new
{
parts = new[]
{
new { text = contentText }
}
}
}
};
var jsonString = JsonSerializer.Serialize(requestJson);
var stringContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(url, stringContent);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<GenerateContentResponse>(responseContent);
}
public class GenerateContentResponse
{
public Candidate[] candidates { get; set; }
public class Candidate
{
public Content content { get; set; }
public SafetyRating[] safetyRatings { get; set; }
}
public class Content
{
public Part[] parts { get; set; }
}
public class Part
{
public string text { get; set; }
}
public class SafetyRating
{
public string category { get; set; }
public string probability { get; set; }
}
}
}
class Program
{
private const string ApiKey = "YOUR-API-KEY";
private const string SystemInstructionText = @"
あなたはプロの翻訳家です。日本語以外の言語を日本語に、日本語を英語に翻訳し、必ず日本語で解説を加えます。
以下のルールを厳守してください。
1. 翻訳:
* 日本語以外の言語で書かれたテキストは日本語に翻訳します。
* 日本語で書かれたテキストは英語に翻訳します。
2. 解説:
* 全ての翻訳には、日本語で解説を加えてください。
* 解説には、翻訳の際に特に注意した点、言葉の選択理由、文化的な背景、ニュアンスの違いなどを含めてください。
* 可能な限り原文の意図を汲み取り、正確かつ自然な翻訳を心がけてください。
* 翻訳が難しい箇所や複数の解釈が可能な場合は、それぞれの解釈を提示し、それぞれの解説を加えてください。
3. その他:
* 原文のスタイルやトーンを尊重してください。
* 敬語やカジュアルな表現など、原文の文体に合わせた翻訳を心がけてください。
* 翻訳結果に誤りがないか、常に確認してください。
";
static async Task Main(string[] args)
{
if (string.IsNullOrEmpty(ApiKey))
{
Console.WriteLine("APIキーが設定されていません。");
return;
}
Console.Write("翻訳したいテキストを入力してください: ");
var contentText = Console.ReadLine();
var genAi = new GoogleAI(ApiKey);
try
{
var response = await genAi.GenerateContentObject(SystemInstructionText, contentText, "gemini-2.0-flash");
Console.WriteLine("\n応答: " + response.candidates[0].content.parts[0].text);
}
catch (HttpRequestException ex)
{
Console.WriteLine($"HTTPリクエストでの例外が発生しました: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"例外が発生しました: {ex.Message}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment