|
# Nabil Redmann - 2025-11-04 |
|
[CmdletBinding()] |
|
|
|
param ( |
|
[string][Parameter(Mandatory=$true)]$content, # Inhalt kann an dieses Script übergeben werden |
|
[string][Parameter(Mandatory=$false)]$systemPrompt, # Fallback zu .\ask.ai.prompts.ps1 |
|
|
|
# gemini-2.0-flash 1M tokens |
|
# gemini-2.5-flash 250K tokens, öfters 'overloaded' (error: 503) --> gemini-2.0-flash nutzen |
|
[string][Parameter(Mandatory=$false)]$model = "gemini-2.0-flash", |
|
[string][Parameter(Mandatory=$false)]$GEMINI_API_KEY = "" |
|
) |
|
|
|
|
|
# Setze leeren API Key zu $null für vergleich |
|
if ($GEMINI_API_KEY -like "") { $GEMINI_API_KEY = $null } |
|
# erst checke argument, dann GOOGLE env, dann GEMINI env ( Powershell 7+ syntax: `??` ) |
|
$GEMINI_API_KEY = $GEMINI_API_KEY ?? $env:GOOGLE_API_KEY ?? $env:GEMINI_API_KEY |
|
if (-not $GEMINI_API_KEY) { throw "⚠️ KEINEN GOOGLE/GEMINI API KEY GEFUNDEN! (-GEMINI_API_KEY oder `$env:GEMINI_API_KEY)" } |
|
|
|
|
|
# importiere system prompt text, wenn kein argument gegeben |
|
if (-not $systemPrompt) { |
|
try { |
|
# import ... |
|
. .\ask.ai.prompts.ps1 |
|
if ($systemPrompt -like "") { Write-Warning "⚠️ KEINEN SYSTEM-PROMPT IN DATEI GEFUDNEN! (.\ask.ai.prompts.ps1)" } |
|
} |
|
catch { Write-Warning "⚠️ KEINE DATEI FÜR SYSTEM-PROMPT GEFUNDEN! (.\ask.ai.prompts.ps1)" } |
|
} |
|
|
|
|
|
|
|
|
|
|
|
# FREE API KEY: https://aistudio.google.com/apikey |
|
$url = "https://generativelanguage.googleapis.com/v1beta/models/" + $model + ":generateContent" # +"?key="+$GEMINI_API_KEY - or x-goog-api-key |
|
|
|
$headers = @{ |
|
"Content-Type" = "application/json" |
|
"x-goog-api-key" = $GEMINI_API_KEY |
|
} |
|
$body = @{ |
|
"system_instruction" = @{ |
|
"parts" = @( @{"text" = $systemPrompt} ) |
|
} |
|
"contents" = @( |
|
@{"role" = "user"; "parts" = @( @{ "text" = $content; } ) } |
|
# image: nwuer content oder merge parts --- ref: https://ai.google.dev/api#content-generation |
|
# $mimeType = "image/jpeg" |
|
# $inline_base64_content = "...base64 encoded image..." |
|
# ... "contents" = @{"role"="user"; "parts" = @( @{ "inline_data" = @{ "mime_type" = $mimeType; "data" = $inline_base64_content; } } )}; |
|
) |
|
} | ConvertTo-Json -Depth 10 |
|
|
|
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body |
|
$result = $response.candidates[0].content.parts[0].text |
|
|
|
## Debug output: |
|
# $result | Show-Markdown |
|
|
|
return $result |