Skip to content

Commit 7b5865a

Browse files
committed
add code and more
1 parent 2bbfec2 commit 7b5865a

9 files changed

Lines changed: 200 additions & 2 deletions

InstallModule.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
$fullpath = $env:PSModulePath -split ":(?!\\)|;|," |
3+
Where-Object { $_ -notlike ([System.Environment]::GetFolderPath("UserProfile") + "*") -and $_ -notlike "$pshome*" } |
4+
Select-Object -First 1
5+
6+
$fullPath = Join-Path $fullPath -ChildPath "PowerShellAI"
7+
8+
Push-location $PSScriptRoot
9+
Robocopy . $fullPath /mir /XD .vscode images .git .github /XF README.md .gitattributes .gitignore install.ps1 InstallModule.ps1 PublishToGallery.ps1
10+
Pop-Location

PowerShellAI.psd1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@{
2+
RootModule = 'PowerShellAI.psm1'
3+
ModuleVersion = '0.1.0'
4+
GUID = '081ce7b4-6e63-41ca-92a7-2bf72dbad018'
5+
Author = 'Douglas Finke'
6+
CompanyName = 'Doug Finke'
7+
Copyright = 'c 2023 All rights reserved.'
8+
9+
Description = @'
10+
PowerShell GPT AI module allows to integrate with OpenAI API and access GPT-3 model and easily to use with other PowerShell scripts and tools.
11+
'@
12+
13+
FunctionsToExport = @(
14+
'Disable-AIShortCutKey'
15+
'Enable-AIShortCutKey'
16+
'Get-GPT3Completion'
17+
)
18+
19+
PrivateData = @{
20+
PSData = @{
21+
Category = "PowerShell GPT Module"
22+
Tags = @("PowerShell", "GPT", "OpenAI")
23+
ProjectUri = "https://github.com/dfinke/PowerShellAI"
24+
LicenseUri = "https://github.com/dfinke/PowerShellAI/blob/master/LICENSE.txt"
25+
}
26+
}
27+
}

PowerShellAI.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foreach ($directory in @('Public')) {
2+
Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" | ForEach-Object { . $_.FullName }
3+
}

Public/Disable-AIShortCutKey.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Disable-AIShortCutKey {
2+
<#
3+
.SYNOPSIS
4+
Disable the Ctrl+g shortcut key for getting completions
5+
6+
.EXAMPLE
7+
Disable-AIShortCutKey
8+
#>
9+
10+
Remove-PSReadLineKeyHandler -Chord Ctrl+g
11+
}

Public/Enable-AIShortCutKey.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function Enable-AIShortCutKey {
2+
<#
3+
.SYNOPSIS
4+
Enable the Ctrl+g shortcut key for getting completions
5+
6+
.EXAMPLE
7+
Enable-AIShortCutKey
8+
#>
9+
Set-PSReadLineKeyHandler -Key Ctrl+g `
10+
-BriefDescription OpenAICli `
11+
-LongDescription "Calls Open AI on the current buffer" `
12+
-ScriptBlock {
13+
param($key, $arg)
14+
15+
$line = $null
16+
$cursor = $null
17+
18+
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
19+
20+
$prompt = "Using PowerShell, just code: $($line)"
21+
22+
$output = Get-GPT3Completion $prompt -max_tokens 256
23+
$output = $output -replace "`r", ""
24+
25+
# check if output is not null
26+
if ($null -ne $output) {
27+
foreach ($str in $output) {
28+
if ($null -ne $str -and $str -ne "") {
29+
[Microsoft.PowerShell.PSConsoleReadLine]::AddLine()
30+
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($str)
31+
}
32+
}
33+
}
34+
}
35+
}

Public/Get-GPT3Completion.ps1

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
function Get-GPT3Completion {
2+
[CmdletBinding()]
3+
<#
4+
.SYNOPSIS
5+
Get a completion from the OpenAI GPT-3 API
6+
7+
.DESCRIPTION
8+
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position
9+
10+
.PARAMETER prompt
11+
The prompt to generate completions for
12+
13+
.PARAMETER model
14+
ID of the model to use. Defaults to 'text-davinci-003'
15+
16+
.PARAMETER temperature
17+
The temperature used to control the model's likelihood to take risky actions. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. Defaults to 0
18+
19+
.PARAMETER max_tokens
20+
The maximum number of tokens to generate. By default, this will be 64 if the prompt is not provided, and 1 if a prompt is provided. The maximum is 2048
21+
22+
.PARAMETER top_p
23+
An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. Defaults to 1
24+
25+
.PARAMETER frequency_penalty
26+
A value between 0 and 1 that penalizes new tokens based on whether they appear in the text so far. Defaults to 0
27+
28+
.PARAMETER presence_penalty
29+
A value between 0 and 1 that penalizes new tokens based on whether they appear in the text so far. Defaults to 0
30+
31+
.PARAMETER stop
32+
A list of tokens that will cause the API to stop generating further tokens. By default, the API will stop generating when it hits one of the following tokens: ., !, or ?.
33+
34+
.EXAMPLE
35+
Get-GPT3Completion -prompt "What is 2%2? - please explain"
36+
#>
37+
param(
38+
[Parameter(Mandatory)]
39+
$prompt,
40+
$model = 'text-davinci-003',
41+
$temperature = 0,
42+
$max_tokens = 256,
43+
$top_p = 1,
44+
$frequency_penalty = 0,
45+
$presence_penalty = 0,
46+
$stop,
47+
[Switch]$Raw
48+
)
49+
50+
if ([string]::IsNullOrEmpty($env:OpenAIKey)) {
51+
throw 'You must set the $env:OpenAIKey environment variable to your OpenAI API key. https://beta.openai.com/account/api-keys'
52+
}
53+
54+
$body = [ordered]@{
55+
model = $model
56+
prompt = $prompt
57+
temperature = $temperature
58+
max_tokens = $max_tokens
59+
top_p = $top_p
60+
frequency_penalty = $frequency_penalty
61+
presence_penalty = $presence_penalty
62+
stop = $stop
63+
}
64+
65+
$body = $body | ConvertTo-Json -Depth 5
66+
$body = [System.Text.Encoding]::UTF8.GetBytes($body)
67+
$params = @{
68+
Uri = "https://api.openai.com/v1/completions"
69+
Method = 'Post'
70+
Headers = @{Authorization = "Bearer $($env:OpenAIKey)" }
71+
ContentType = 'application/json'
72+
#body = $body | ConvertTo-Json -Depth 5
73+
body = $body
74+
}
75+
76+
#$params["body"] = [System.Text.Encoding]::UTF8.GetBytes($json)
77+
78+
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
79+
if ($env:USERNAME -eq 'finke') { $exclude = 'Headers' }
80+
81+
$params |
82+
ConvertTo-Json -Depth 10 |
83+
ConvertFrom-Json |
84+
Select-Object * -ExcludeProperty $exclude |
85+
Format-List |
86+
Out-Host
87+
}
88+
89+
$result = Invoke-RestMethod @params
90+
91+
if ($Raw) {
92+
$result
93+
}
94+
else {
95+
$result.choices[0].text
96+
}
97+
}

PublishToGallery.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$p = @{
2+
Name = "PowerShellAI"
3+
NuGetApiKey = $NuGetApiKey
4+
}
5+
6+
Publish-Module @p

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
>![Alt text](media/AIReplace.png)
1+
>![](./media/AIReplace.png)
22
33
# Unleash the Power of Artificial Intelligence with PowerShell
44
Welcome to the PowerShell Artificial Intelligence repository! Here, you will find a collection of powerful PowerShell scripts that will enable you to easily integrate AI into your projects and take them to the next level. Imagine being able to interact directly with OpenAI's GPT AI with just a few simple commands. With this module, it's now possible.
@@ -11,4 +11,13 @@ By using this module, you'll have the ability to add cutting-edge AI functionali
1111
Check out these PowerShell scripts to see how easy it is to get started with AI in PowerShell:
1212

1313
|PS Script | Description | Location
14-
|--|--|--|
14+
|--|--|--|
15+
| Disable-AIShortCutKey | Disable the <kbd>ctrl+g</kbd> shortcut key go getting completions | [Disable-AIShortCutKey.ps1](./Public/Disable-AIShortCutKey.ps1) |
16+
| Enable-AIShortCutKey | Enable the <kbd>ctrl+g</kbd> | [Enable-AIShortCutKey.ps1](./Public/Enable-AIShortCutKey.ps1) |
17+
| Get-GPT3Completion | Get a completion from the OpenAI GPT-3 API | [Get-GPT3Completion.ps1](./Public/Get-GPT3Completion.ps1)
18+
19+
## In Action
20+
21+
Here is `Get-GPT3Completion` posting your request to the OpenAI GPT-3 API and returning the completion:
22+
23+
![](./media/GPT3Completion.gif)

media/GPT3Completion.gif

794 KB
Loading

0 commit comments

Comments
 (0)