Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active April 19, 2024 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dfinke/9e5c10e8ad1333af1e4e426dd3dbb5e3 to your computer and use it in GitHub Desktop.
Save dfinke/9e5c10e8ad1333af1e4e426dd3dbb5e3 to your computer and use it in GitHub Desktop.
<#
.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
}
}
}
}
@dfinke
Copy link
Author

dfinke commented Apr 10, 2024

'openai', 'anthropic', 'gemini' | % { psllm.ps1 'capital of spain' -provider $_  }
provider  Response
--------  --------
openai    Madrid
anthropic The capital of Spain is Madrid.
gemini    Madrid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment