Public/Ask-PSUAi.ps1
function Ask-PSUAi { <# .SYNOPSIS Interactive Gemini 2.0 Flash chatbot using Google's Generative Language API. .DESCRIPTION Opens a PowerShell-based chat session with Gemini AI. This function interacts with Google's Generative Language API (Gemini 2.0 Flash model) to perform fast and lightweight AI content generation. Requires an environment variable named 'GOOGLE_GEMINI_API_KEY'. 📌 How to get started: ---------------------- 1️⃣ Visit: https://makersuite.google.com/app/apikey 2️⃣ Sign in with your Google account 3️⃣ Click **"Create API Key"** 4️⃣ Copy the key and save it using: Set-PSUUserEnvironmentVariable -Name "GOOGLE_GEMINI_API_KEY" -Value "<your-api-key>" .NOTES Author: Lakshmanachari Panuganti Date: 4th July 2025 History: Initial development of Ask-PSUAi Chatbot. .EXAMPLE Ask-PSUAi #> [CmdletBinding()] [Alias("Ask-Ai")] param ( [string]$ApiKey = $env:GOOGLE_GEMINI_API_KEY ) if (-not $ApiKey) { Write-Error "Gemini API key not found. Please set it using:`nSet-PSUUserEnvironmentVariable -Name 'GOOGLE_GEMINI_API_KEY' -Value '<your-api-key>'" return } $uri = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$ApiKey" $chatHistory = @() Write-Host "💬 Welcome to PSU Ai Chatbot!!" -ForegroundColor Green Write-Host "Type your message. Type 'exit' or 'q' to quit." -ForegroundColor Yellow while ($true) { Write-Host "" $prompt = Read-Host -Prompt "👤 You" if ($prompt -in @('exit', 'q')) { Write-Host "`n👋 Exiting chat. Goodbye!" -ForegroundColor Cyan break } $chatHistory += @{ role = "user"; parts = @(@{ text = $prompt }) } $body = @{ contents = $chatHistory } | ConvertTo-Json -Depth 10 try { $response = Invoke-RestMethod -Method Post -Uri $uri -Body $body -ContentType 'application/json' $text = $response.candidates[0].content.parts[0].text $text = $text -replace '```json', '' -replace '```', '' -replace '^[\s\r\n]+|[\s\r\n]+$', '' Write-Host "`n🤖 PSU-Ai: " -NoNewline Write-Host $text -ForegroundColor Yellow $chatHistory += @{ role = "model"; parts = @(@{ text = $text }) } } catch { Write-Error "Error communicating with Gemini: $($_.Exception.Message)" } } } |