forked from StartAutomating/PowerShellAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-AIExplain.ps1
More file actions
58 lines (54 loc) · 1.69 KB
/
Invoke-AIExplain.ps1
File metadata and controls
58 lines (54 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function Invoke-AIExplain {
<#
.SYNOPSIS
Explain the last command or a command by id
.DESCRIPTION
Invoke-AIExplain is a function that uses the OpenAI GPT-3 API to provide explain the last command or a command by id.
.EXAMPLE
explain
.EXAMPLE
explain 10 # where 10 is the id of the command in the history
.EXAMPLE
explain 10 13 # the start and end id of the commands in the history
.EXAMPLE
explain -Value "Get-Process"
#>
[CmdletBinding()]
[alias("explain")]
param(
$Id,
$IdEnd,
$Value,
$max_tokens = 300
)
if ($Id -and $IdEnd) {
foreach ($targetId in ($Id..$IdEnd)) {
$cli += (Get-History -Id $targetId).CommandLine + "`r`n"
}
}
elseif ($Value) {
$cli = $Value
}
elseif ($Id) {
$cli = (Get-History -Id $Id).CommandLine
}
else {
$cli = (Get-History | Select-Object -last 1).CommandLine
}
write-host $cli
$prompt = 'You are running powershell on ' + $PSVersionTable.Platform
$prompt += " Please explain the following: "
# Dynamically determine which OpenAI service is being used
$provider = $null
$provider = Get-ChatAPIProvider
switch ($provider.tolower()) {
openai { $result = $cli | ai $prompt -max_tokens $max_tokens }
azureopenai {
$prompt += $cli
$result = Get-GPT4Completion -Content $prompt -max_tokens $max_tokens
}
Default { $result = $cli | ai $prompt -max_tokens $max_tokens }
}
Write-Codeblock -Text $cli -SyntaxHighlight
$result
}