Created
February 19, 2025 02:08
-
-
Save robert-mcdermott/5cf613b35f434231a406ba29dca9153d to your computer and use it in GitHub Desktop.
Local Ollama inference with PowerShell
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
# Define API key and endpoint | |
$ApiKey = "sk-12345" | |
$ApiEndpoint = "http://localhost:11434/v1/chat/completions" | |
<# | |
System message. | |
You can use this to give the AI instructions on what to do, how to act or how to respond to future prompts. | |
Default value for ChatGPT = "You are a helpful assistant." | |
#> | |
$AiSystemMessage = "You are a helpful AI assistant." | |
# Function to send a message to ChatGPT | |
function Invoke-ChatGPT ($message) { | |
# List of Hashtables that will hold the system message and user message. | |
[System.Collections.Generic.List[Hashtable]]$messages = @() | |
# Sets the initial system message | |
$messages.Add(@{"role" = "system"; "content" = $AiSystemMessage}) | Out-Null | |
# Add the user input | |
$messages.Add(@{"role"="user"; "content"=$message}) | |
# Set the request headers | |
$headers = @{ | |
"Content-Type" = "application/json" | |
"Authorization" = "Bearer $ApiKey" | |
} | |
# Set the request body | |
$requestBody = @{ | |
"model" = "llama3.2" | |
"messages" = $messages | |
"max_tokens" = 200 # Max amount of tokens the AI will respond with | |
"temperature" = 0.5 # lower is more coherent and conservative, higher is more creative and diverse. | |
} | |
# Send the request | |
$response = Invoke-RestMethod -Method POST -Uri $ApiEndpoint -Headers $headers -Body (ConvertTo-Json $requestBody) | |
# Return the message content | |
return $response.choices[0].message.content | |
} | |
# Get user input | |
$userInput = Read-Host "Your prompt: " | |
# Query ChatGPT | |
$AiResponse = Invoke-ChatGPT $UserInput | |
# Show response | |
write-output $AiResponse | |
Read-Host "Press enter to Exit..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment