-
-
Save OSDeploy/4a82e8ac9c36eaa9927715f4847eaf26 to your computer and use it in GitHub Desktop.
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
<# | |
.EXAMPLE | |
llm "what is the capital of spain" -provider openai | |
.EXAMPLE | |
llm "what is the capital of spain" -provider anthropic | |
.EXAMPLE | |
llm "what is the capital of spain" -provider gemini | |
#> | |
param( | |
[Parameter(Mandatory)] | |
$prompt, | |
[ValidateSet('openai', 'anthropic', 'gemini')] | |
$provider = 'openai' | |
) | |
if ($null -eq $model) { | |
switch ($provider) { | |
'openai' { | |
if ($null -eq $env:OpenAIKey) { | |
throw "OpenAIKey environment variable not set" | |
} | |
$params = @{ | |
Headers = @{Authorization = "Bearer $($env:OpenAIKey)" } | |
Uri = 'https://api.openai.com/v1/chat/completions' | |
Method = 'POST' | |
ContentType = 'application/json' | |
body = [ordered]@{ | |
model = 'gpt-3.5-turbo' | |
max_tokens = 1024 | |
messages = @( | |
@{ | |
role = 'user' | |
content = $prompt | |
} | |
) | |
} | ConvertTo-Json -Depth 10 | |
} | |
$r = Invoke-RestMethod @params | |
[pscustomobject][ordered]@{ | |
provider = $provider | |
Response = $r.choices[0].message.content | |
} | |
} | |
'anthropic' { | |
if ($null -eq $env:AnthropicAPIKey) { | |
throw "AnthropicKey environment variable not set" | |
} | |
$headers = @{ | |
'x-api-key' = $env:AnthropicAPIKey | |
'anthropic-version' = '2023-06-01' | |
'Content-Type' = 'application/json' | |
} | |
$body = @{ | |
model = "claude-3-opus-20240229" | |
max_tokens = 1024 | |
messages = @( | |
@{ | |
role = "user" | |
content = $prompt | |
} | |
) | |
} | |
$r = Invoke-RestMethod -Uri 'https://api.anthropic.com/v1/messages' -Method Post -Headers $headers -Body ($body | ConvertTo-Json -Depth 10) | |
[pscustomobject][ordered]@{ | |
provider = $provider | |
Response = $r.content.text | |
} | |
} | |
'gemini' { | |
if ($null -eq $env:GeminiKey) { | |
throw "GeminiKey environment variable not set" | |
} | |
$params = @{ | |
Uri = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$($env:GeminiKey)" | |
Method = "Post" | |
ContentType = 'application/json' | |
body = @{ | |
contents = @( | |
@{ | |
"parts" = @( | |
@{ | |
"text" = $prompt | |
} | |
) | |
} | |
) | |
} | ConvertTo-Json -Depth 10 | |
} | |
$r = Invoke-RestMethod @params | |
[pscustomobject][ordered]@{ | |
provider = $provider | |
Response = $r.candidates[0].content.parts[0].text | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment