Skip to content

Commit

Permalink
Merge pull request #1 from TNG/feature/prepare-pwsh-gallery
Browse files Browse the repository at this point in the history
Feature/prepare pwsh gallery
  • Loading branch information
kaspertng authored Apr 8, 2023
2 parents bbd08ae + f879a6a commit a233ac6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
19 changes: 14 additions & 5 deletions PleasePwsh/PleasePwsh.psd1
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
@{
RootModule = 'PleasePwsh.psm1'
ModuleVersion = '0.1'
Author = 'T. Kasper'
Description = 'Translates a prompt into a PowerShell command using OpenAI GPT.'
RootModule = 'PleasePwsh.psm1'
ModuleVersion = '0.1'
GUID = 'd430aac3-8363-4321-a610-ff33a366a50b'
Author = 'Tobias Kasper'
CompanyName = 'TNG Technology Consulting GmbH'
Copyright = '2023 TNG Technology Consulting GmbH. All rights reserved.'
Description = 'Translates a prompt into a PowerShell command using OpenAI GPT.'
PrivateData = @{
PSData = @{
Tags = @('PowerShell', 'GPT', 'OpenAI')
ProjectUri = 'https://github.com/TNG/please-pwsh'
}
}
PowerShellVersion = '7.0'
FunctionsToExport = 'Please'
AliasesToExport = '*'
AliasesToExport = '*'
}
38 changes: 19 additions & 19 deletions PleasePwsh/PleasePwsh.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If this switch is provided, the script will return a brief explanation of the sp
.EXAMPLE
PS> Please "Get all files in current directory that are larger than 1 MB"
PS> Please -Explain "Get-ChildItem -Path 'C:\'"
PS> Please -Explain "Get-ChildItem -Path 'C:\'"
#>
function Please {
[CmdletBinding()]
Expand All @@ -33,18 +33,18 @@ function Please {
[Alias("e")][switch]$Explain

)

Test-ApiKey

if ($Explain) {
$Explanation = Get-CommandExplanation $Prompt
Write-Host "`u{261D} $Explanation"
Write-Output "`u{261D} $Explanation"
$Command = $Prompt
}
else {
$Command = Get-Command $Prompt
$Command = Get-PwshCommand $Prompt
if ($Command.contains("I do not know")) {
Write-Host $Command
Write-Output $Command
Return
}
}
Expand All @@ -54,7 +54,7 @@ function Please {

function Test-ApiKey {
if ($null -eq $env:OPENAI_API_KEY) {
Write-Host "`u{1F50E} Api key missing. See https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key"
Write-Output "`u{1F50E} Api key missing. See https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key"
$Key = Read-Host "Please provide the api key"

if ([string]::IsNullOrWhiteSpace($Key)) {
Expand All @@ -64,7 +64,7 @@ function Test-ApiKey {
}
}

function Get-Command([string]$Prompt) {
function Get-PwshCommand([string]$Prompt) {
$Role = "You translate the input given into PowerShell command. You may not use natural language, but only a PowerShell commands as answer. Do not use markdown. Do not quote the whole output. If you do not know the answer, answer only with 'I do not know'"

$Payload = @{
Expand All @@ -80,9 +80,9 @@ function Get-Command([string]$Prompt) {

function Show-Menu($Command) {
$Title = "`u{1F523} Command:`n $Command"

$Question = "`u{2753} What should I do?"

$OptionAbort = [ChoiceDescription]::new('&Abort')
$OptionCopy = [ChoiceDescription]::new('&Copy')
$OptionInvoke = [ChoiceDescription]::new('&Invoke')
Expand All @@ -93,21 +93,21 @@ function Show-Menu($Command) {

function Invoke-Action ($Action) {
switch ($Action) {
0 {
Write-Host "`u{274C} Aborting"
0 {
Write-Output "`u{274C} Aborting"
}
1 {
Write-Host "`u{00A9} Copying to clipboard"
Write-Output "`u{00A9} Copying to clipboard"
Set-Clipboard -Value $Command
}
2 {
Write-Host "`u{25B6} Invoking command"
Invoke-Expression $Command
Write-Output "`u{25B6} Invoking command"
Invoke-Expression $Command
}
Default {
Write-Host "Invalid action"
Write-Output "Invalid action"
}
}
}
}

function Get-CommandExplanation([string]$Command) {
Expand All @@ -121,7 +121,7 @@ function Get-CommandExplanation([string]$Command) {
)
}

Return Invoke-OpenAIRequest $Payload
Return Invoke-OpenAIRequest $Payload
}

function Invoke-OpenAIRequest($Payload) {
Expand All @@ -133,12 +133,12 @@ function Invoke-OpenAIRequest($Payload) {
}

try {
$Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body ($Payload | ConvertTo-Json)
$Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body ($Payload | ConvertTo-Json)
}
catch {
Write-Error "Received $($_.Exception.Response.ReasonPhrase): $($_.Exception.Response.Content | ConvertTo-Json)"
}

Return $Response.choices[0].message.content
}

Expand Down
15 changes: 9 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Please CLI for PowerShell Core

An AI helper script to create PowerShell commands. It is a copy of [Please CLI](https://github.com/TNG/please-cli/).
An AI helper script to create PowerShell commands. It is a copy of [Please CLI](https://github.com/TNG/please-cli/).

The initial approach to `ChatGPT please convert the bash script to pwsh` did not work. So its a rewrite that still contains trace amounts of AI.

Expand Down Expand Up @@ -38,8 +38,11 @@ You need an [OpenAI API key](https://platform.openai.com/account/api-keys) and a

## Installation

For now copy the folder `PleasePwsh` in one of your module folders and set the OpenAI API key as environment variable.
- Find module folders with `$ENV:PSModulePath -Split ';'`
- Set environment variable permanently in PowerShell profile
- Open profile `Code $PROFILE`
- Add a line `$ENV:OPENAI_API_KEY = <YOUR_API_KEY>`
From the [PowerShell Gallery](https://www.powershellgallery.com/packages/PleasePwsh) with
```
Install-Module -Name PleasePwsh
```

Set the OpenAI API key as environment variable.
- Open PowerShell profile: `Code $PROFILE`
- Add a line with your API key: `$ENV:OPENAI_API_KEY = <YOUR_API_KEY>`

0 comments on commit a233ac6

Please sign in to comment.