Last active
February 1, 2025 15:32
-
-
Save markgodiy/9f9721b26449cc41a1a2e1ca367b93da to your computer and use it in GitHub Desktop.
Invoke-Groq
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
| function Invoke-Groq { | |
| [cmdletbinding()] | |
| param ( | |
| [Parameter(Mandatory, Position=0)] | |
| [ValidateNotNull()] | |
| [string]$Prompt, # The input prompt or query to be processed by the API. | |
| [Parameter(Position=1)] | |
| [ValidateNotNullOrEmpty()] | |
| [string]$APIKey = $env:GROQ_API_Key, # API key to authenticate requests to the Groq API. | |
| [Parameter(Position=2)] | |
| [ValidateSet("llama70b", "llama8b", "mixtral", "gemma","deepseek")] | |
| [string]$Model, # The specific model to use for processing the request (e.g., "llama70b" for a 70B parameter model). | |
| [Parameter(Position=3)] | |
| [ValidateRange(0.1, 2.0)] | |
| [double]$Temp = 0.5 # Temperature setting for the model's output. Lower values (e.g., 0.1) make the output more focused, while higher values (e.g., 2.0) produce more diverse responses. | |
| ) | |
| $apiUrl = "https://api.groq.com/openai/v1/chat/completions" | |
| $llmmodels = @{ | |
| mixtral = "mixtral-8x7b-32768" | |
| llama70b = "llama3-70b-8192" | |
| llama8b = "llama3-8b-8192" | |
| gemma = "gemma2-9b-it" | |
| deepseek = "deepseek-r1-distill-llama-70b" | |
| } | |
| switch ($Model) { | |
| "mixtral" { $selectModel = $llmmodels["mixtral"] } | |
| "llama8b" { $selectModel = $llmmodels["llama8b"] } | |
| "llama70b" { $selectModel = $llmmodels["llama70b"] } | |
| "gemma" { $selectModel = $llmmodels["gemma"] } | |
| "deepseek" { $selectModel = $llmmodels["deepseek"] } | |
| default { $selectModel = $llmmodels["llama70b"] } | |
| } | |
| $body = @{ | |
| model = $selectModel | |
| temperature = $Temp | |
| messages = @( | |
| @{ | |
| role = "system" | |
| content = "My job depends on this. I need precise and accurate PowerShell tips for system administration tasks. Please respond in plain text without using Markdown. Respond with script examples with very concise comments." | |
| }, | |
| @{ | |
| role = "user" | |
| content = $Prompt | |
| }) | |
| } | |
| $systemPrompt = ($body.messages | Where-Object { $_.role -eq 'system' }).content | |
| $requestPrompt = ($body.messages | Where-Object { $_.role -eq 'user' }).content | |
| $bodyJSON = $body | ConvertTo-Json | |
| try { | |
| Write-verbose "API Key: $APIKey" | |
| Write-Verbose "LLM Model: $selectModel" | |
| Write-verbose "Groq API URL: $apiUrl" | |
| Write-verbose "AI System prompt: $systemPrompt" | |
| Write-verbose "My query: $requestPrompt" | |
| if (!($PSVersionTable.PSEdition -eq "Desktop")) { | |
| # PowerShell 7.x and above | |
| Write-Verbose "PowerShell 7.x or above detected." | |
| $bodyJSON = $body | ConvertTo-Json | |
| Write-Verbose $bodyJSON | |
| $responseJSON = curl.exe -s "$apiUrl" -H "Content-Type: application/json" -H "Authorization: Bearer $APIKey" -d $bodyJSON | |
| } else { | |
| # PowerShell v5.1 - ensuring double quotes are properly escaped | |
| Write-Verbose "PowerShell v5.1 detected, ensuring double quotes are properly escaped." | |
| $jsonDataEscaped = $bodyJSON.Replace('"', '\"') | |
| Write-Verbose $jsonDataEscaped | |
| $responseJSON = curl.exe -s "$apiUrl" -H "Content-Type: application/json" -H "Authorization: Bearer $APIKey" -d $jsonDataEscaped | |
| } | |
| # Convert from JSON | |
| $response = $responseJSON | ConvertFrom-Json | |
| # Write-verbose $response | |
| # Return actual response | |
| Write-debug $response | |
| Return "[$($response.model)]`r`n$($response.choices[0].message.content)`r`n" | |
| } | |
| catch { | |
| Write-Error "Something went wrong. $_" | |
| } | |
| } | |
| Set-Alias groq Invoke-Groq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment