forked from StartAutomating/PowerShellAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-AIExplain.ps1
47 lines (43 loc) · 1.28 KB
/
Invoke-AIExplain.ps1
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
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
}
$prompt = 'You are running powershell on ' + $PSVersionTable.Platform
$prompt += " Please explain the following:"
$result = $cli | ai $prompt -max_tokens $max_tokens
Write-Codeblock -Text $cli -SyntaxHighlight
$result
}