Last active
July 27, 2025 10:35
-
-
Save Aldo-f/1f78ba3a4a6336ceaab2f189ab40ec23 to your computer and use it in GitHub Desktop.
Chat with gemini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $API_KEY = "" # Haal hier api key op: https://aistudio.google.com/apikey | |
| # Vraag taalkeuze | |
| function Choose-Language { | |
| while ($true) { | |
| $lang = Read-Host "Choose language / Kies taal (EN/NL)" | |
| switch ($lang.ToLower()) { | |
| "en" { return "en" } | |
| "nl" { return "nl" } | |
| default { Write-Host "Please enter EN or NL / Geef EN of NL in." -ForegroundColor Yellow } | |
| } | |
| } | |
| } | |
| $lang = Choose-Language | |
| # Vertalingen | |
| $Text = @{ | |
| "enterName" = @{ | |
| en = "Enter your name" | |
| nl = "Vul je naam in" | |
| } | |
| "defaultName" = @{ | |
| en = "Aldo" | |
| nl = "Aldo" | |
| } | |
| "startChat" = @{ | |
| en = "Starting Gemini chat..." | |
| nl = "Gemini-chat starten..." | |
| } | |
| "invalidApiKey" = @{ | |
| en = "❌ API key invalid or no access to Gemini." | |
| nl = "❌ API key ongeldig of geen toegang tot Gemini." | |
| } | |
| "chatEnded" = @{ | |
| en = "Chat will be terminated." | |
| nl = "Chat wordt beëindigd." | |
| } | |
| "apiKeyValid" = @{ | |
| en = "✅ API key valid. Type multiple lines, end with 'done'. Type 'exit' to quit." | |
| nl = "✅ API key geldig. Typ meerdere regels en eindig met 'done'. Typ 'exit' om te stoppen." | |
| } | |
| "emptyInput" = @{ | |
| en = "Empty input, nothing sent." | |
| nl = "Lege invoer, niets verzonden." | |
| } | |
| "promptUser" = @{ | |
| en = "{0} (type 'done' on empty line to send):" | |
| nl = "{0} (type 'done' op lege lijn om te verzenden):" | |
| } | |
| "errorApi" = @{ | |
| en = "❌ Error during API request:" | |
| nl = "❌ Fout tijdens API-verzoek:" | |
| } | |
| "geminiLabel" = @{ | |
| en = "Gemini:" | |
| nl = "Gemini:" | |
| } | |
| } | |
| # Naam vragen | |
| $username = Read-Host $Text["enterName"][$lang] | |
| if ([string]::IsNullOrWhiteSpace($username)) { | |
| $username = $Text["defaultName"][$lang] | |
| } | |
| $chatHistory = @() | |
| function Prompt-MultilineInput { | |
| param($name) | |
| Write-Host "" | |
| Write-Host ($Text["promptUser"][$lang] -f $name) -ForegroundColor Green | |
| $lines = @() | |
| while ($true) { | |
| $line = Read-Host | |
| if ($line -eq "done") { break } | |
| $lines += $line | |
| } | |
| return ($lines -join "`n").Trim() | |
| } | |
| function Add-ToChatHistory($entry) { | |
| $global:chatHistory = , $chatHistory + $entry | |
| } | |
| function Send-Message($message) { | |
| if ([string]::IsNullOrWhiteSpace($message)) { | |
| Write-Host $Text["emptyInput"][$lang] -ForegroundColor DarkYellow | |
| return | |
| } | |
| $userEntry = [PSCustomObject]@{ | |
| role = "user" | |
| parts = @([PSCustomObject]@{ text = $message }) | |
| } | |
| Add-ToChatHistory $userEntry | |
| $requestBody = [PSCustomObject]@{ | |
| contents = $chatHistory | |
| } | |
| $jsonBody = $requestBody | ConvertTo-Json -Depth 10 -Compress | |
| try { | |
| $response = Invoke-RestMethod ` | |
| -Uri "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$API_KEY" ` | |
| -Method Post ` | |
| -ContentType "application/json" ` | |
| -Body $jsonBody | |
| $reply = $response.candidates[0].content.parts[0].text.Trim() | |
| $modelEntry = [PSCustomObject]@{ | |
| role = "model" | |
| parts = @([PSCustomObject]@{ text = $reply }) | |
| } | |
| Add-ToChatHistory $modelEntry | |
| Write-Host "" | |
| Write-Host $Text["geminiLabel"][$lang] -ForegroundColor Cyan | |
| Write-Host "$reply`n" | |
| } | |
| catch { | |
| Write-Host "" | |
| Write-Host ($Text["errorApi"][$lang]) -ForegroundColor Red | |
| Write-Host $_ -ForegroundColor Red | |
| } | |
| } | |
| function Check-ApiKeyValid { | |
| $testMessage = [PSCustomObject]@{ | |
| contents = @( | |
| [PSCustomObject]@{ | |
| role = "user" | |
| parts = @([PSCustomObject]@{ text = "ping" }) | |
| } | |
| ) | |
| } | |
| $json = $testMessage | ConvertTo-Json -Depth 10 -Compress | |
| try { | |
| Invoke-RestMethod ` | |
| -Uri "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$API_KEY" ` | |
| -Method Post ` | |
| -ContentType "application/json" ` | |
| -Body $json | Out-Null | |
| return $true | |
| } | |
| catch { | |
| Write-Host "" | |
| Write-Host $Text["invalidApiKey"][$lang] -ForegroundColor Red | |
| return $false | |
| } | |
| } | |
| Write-Host $Text["startChat"][$lang] -ForegroundColor Gray | |
| if (-not (Check-ApiKeyValid)) { | |
| Write-Host $Text["chatEnded"][$lang] -ForegroundColor DarkRed | |
| exit 1 | |
| } | |
| Write-Host $Text["apiKeyValid"][$lang] | |
| Write-Host "" | |
| while ($true) { | |
| $input = Prompt-MultilineInput -name $username | |
| if ($input -eq "exit") { break } | |
| Send-Message $input | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment