Skip to content

Instantly share code, notes, and snippets.

@amnweb
Last active January 4, 2024 16:10
Show Gist options
  • Save amnweb/3427a3fe01268ea273ac18e7c9a26ef0 to your computer and use it in GitHub Desktop.
Save amnweb/3427a3fe01268ea273ac18e7c9a26ef0 to your computer and use it in GitHub Desktop.
ollama ai powershell
function ChatInit {
param(
[Parameter(Mandatory = $true)][string]$Model
)
Write-Host "AI Chat Started`nModel: $Model" -ForegroundColor Blue
wsl -e ollama run $Model
}
function CreateMenu {
param(
[Parameter(Mandatory = $true)][string]$MenuTitle,
[Parameter(Mandatory = $true)][array]$MenuOptions
)
Clear-Host
$selectedIndex = 0
$enterPressed = $false
while (-not $enterPressed) {
Write-Host "$MenuTitle"
foreach ($index in 0..($MenuOptions.Count - 1)) {
if ($index -eq $selectedIndex) {
Write-Host "[ $($MenuOptions[$index]) ]" -BackgroundColor Green -ForegroundColor White
} else {
Write-Host "$($MenuOptions[$index])"
}
}
$keyInput = $host.ui.rawui.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
switch ($keyInput) {
13 { # Enter key
$enterPressed = $true
Clear-Host
ChatInit -Model $MenuOptions[$selectedIndex]
}
38 { # Up arrow key
$selectedIndex = ($selectedIndex - 1 + $MenuOptions.Count) % $MenuOptions.Count
Clear-Host
}
40 { # Down arrow key
$selectedIndex = ($selectedIndex + 1) % $MenuOptions.Count
Clear-Host
}
default {
Clear-Host
}
}
}
}
$wslOutput = wsl -e ollama list
if ([string]::IsNullOrWhiteSpace($wslOutput)) {
Write-Host "The WSL does not work or models are not installed."
} else {
$firstColumnItems = $wslOutput | Select-Object -Skip 1 | ForEach-Object {
($_ -split '\t+|\s{2,}')[0].Trim()
}
if ($firstColumnItems -and $firstColumnItems.Count -gt 0) {
CreateMenu -MenuTitle 'Select Model' -MenuOptions $firstColumnItems
} else {
Write-Host "No items were returned from the WSL command after parsing."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment