From d2261719d37802e97d72fd9e28f78b1f0cc4b17b Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 25 Jul 2024 09:08:14 -0400 Subject: [PATCH 01/11] feat: Add Update-TypeData --- changelog.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index fae2b10..e5ca4a4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,8 @@ -## 0.2.5 +## v0.2.6 + +- Add Update-TypeData + +## v0.2.5 - Fix $model to 'gpt-4o-mini' From 9c0a0152c4d457c2c6acf976d8f43d9929afc88f Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 25 Jul 2024 10:13:50 -0400 Subject: [PATCH 02/11] Update Invoke-FilesToPrompt function to process Markdown files recursively --- Public/Invoke-FilesToPrompt.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Public/Invoke-FilesToPrompt.ps1 b/Public/Invoke-FilesToPrompt.ps1 index e36464e..71588e9 100644 --- a/Public/Invoke-FilesToPrompt.ps1 +++ b/Public/Invoke-FilesToPrompt.ps1 @@ -27,11 +27,15 @@ This example invokes the Invoke-FilesToPrompt function to process all Markdown files in the "C:\MyFiles" directory. - .EXAMPLE +.EXAMPLE Invoke-FilesToPrompt -Path "C:\MyFiles\*.md", "C:\MyOtherFiles\*.md" This example invokes the Invoke-FilesToPrompt function to process all Markdown files in the "C:\MyFiles" directory. +.EXAMPLE + (Invoke-FilesToPrompt (dir . -r *.md)) + + This example recursively processes all Markdown files in the current directory. #> function Invoke-FilesToPrompt { [CmdletBinding()] From 32b36c33be28847ce619cd7fd7b9c3a49d355404 Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 25 Jul 2024 10:14:00 -0400 Subject: [PATCH 03/11] feat: Add Invoke-AIPrompt function for AI-powered prompts --- Public/Invoke-AIPrompt.ps1 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Public/Invoke-AIPrompt.ps1 diff --git a/Public/Invoke-AIPrompt.ps1 b/Public/Invoke-AIPrompt.ps1 new file mode 100644 index 0000000..ef74a2f --- /dev/null +++ b/Public/Invoke-AIPrompt.ps1 @@ -0,0 +1,38 @@ +function Invoke-AIPrompt { + [CmdletBinding()] + param( + $Prompt, + $Data, + [Switch]$UsePowerShellPersona + ) + + $messages = @() + if ($UsePowerShellPersona) { + $messages += New-ChatRequestSystemMessage "You are tuned to be a PowerShell expert. Answer the question in the style of Doug Finke Microsoft PowerShell MVP." + } + + $messages += New-ChatRequestSystemMessage "Here is the data, please answer the question:`n $(ConvertTo-Json -Compress $Data -Depth 10)" + + $messages += New-ChatRequestUserMessage $Prompt + + $result = Invoke-OAIChatCompletion -Messages $messages + $result.choices[0].message.content +} + +$targetTypes = "System.String", "System.Array" + +foreach ($targetType in $targetTypes) { + Update-TypeData -TypeName $targetType -MemberType ScriptMethod -MemberName "Chat" -Force -Value { + param($Prompt) + + Invoke-AIPrompt -Prompt $Prompt -Data $this + } +} + +foreach ($targetType in $targetTypes) { + Update-TypeData -TypeName $targetType -MemberType ScriptMethod -MemberName "PSChat" -Force -Value { + param($Prompt) + + Invoke-AIPrompt -Prompt $Prompt -Data $this -UsePowerShellPersona + } +} \ No newline at end of file From 0e80d1abc7b1a2c69c30ef8a776bb77c4096ad1d Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 25 Jul 2024 10:14:09 -0400 Subject: [PATCH 04/11] feat: Add tests for Invoke-AIPrompt function --- __tests__/Invoke-AIPrompt.tests.ps1 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 __tests__/Invoke-AIPrompt.tests.ps1 diff --git a/__tests__/Invoke-AIPrompt.tests.ps1 b/__tests__/Invoke-AIPrompt.tests.ps1 new file mode 100644 index 0000000..9b7b853 --- /dev/null +++ b/__tests__/Invoke-AIPrompt.tests.ps1 @@ -0,0 +1,19 @@ +Describe "Invoke-AIPrompt" -Tag Invoke-AIPrompt { + BeforeAll { + Import-Module "$PSScriptRoot/../PSAI.psd1" -Force + } + + It "should have these parameters" { + $actual = Get-Command Invoke-AIPrompt -ErrorAction SilentlyContinue + + $actual | Should -Not -BeNullOrEmpty + + $keyArray = $actual.Parameters.Keys -as [array] + + $keyArray[0] | Should -BeExactly 'Prompt' + $keyArray[1] | Should -BeExactly 'Data' + $keyArray[2] | Should -BeExactly 'UsePowerShellPersona' + + $actual.Parameters['UsePowerShellPersona'].SwitchParameter | Should -Be $true + } +} From ea5edd3be3f8c34b657d55110fb9c1a1a6202b6a Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 25 Jul 2024 10:14:19 -0400 Subject: [PATCH 05/11] Update module version to 0.2.6 and add Invoke-AIPrompt function --- PSAI.psd1 | 5 +++-- PSAI.psm1 | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/PSAI.psd1 b/PSAI.psd1 index 83a8ec3..e9fd05b 100644 --- a/PSAI.psd1 +++ b/PSAI.psd1 @@ -1,6 +1,6 @@ @{ RootModule = 'PSAI.psm1' - ModuleVersion = '0.2.5' + ModuleVersion = '0.2.6' GUID = '68662d19-a8f1-484f-b1b7-3bf0e8a436df' Author = 'Douglas Finke' CompanyName = 'Doug Finke' @@ -22,7 +22,6 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in 'Invoke-OAIBeta' # Public - 'Show-OAIBilling' 'Add-OAIVectorStore' 'Clear-OAIAllItems' 'Clear-OAIAssistants' @@ -73,6 +72,7 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in 'Get-UnitTestingStatus' 'Import-OAIAssistant' 'Invoke-AIExplain' + 'Invoke-AIPrompt' 'Invoke-FilesToPrompt' 'Invoke-OAIChat' 'Invoke-OAIChatCompletion' @@ -107,6 +107,7 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in 'Set-OAIProvider' 'Show-OAIAPIReferenceWebPage' 'Show-OAIAssistantWebPage' + 'Show-OAIBilling' 'Show-OAIPlaygroundWebPage' 'Show-OAIVectorStoreWebPage' 'Stop-OAIRun' diff --git a/PSAI.psm1 b/PSAI.psm1 index 198a558..15a47f4 100644 --- a/PSAI.psm1 +++ b/PSAI.psm1 @@ -3,7 +3,6 @@ . $PSScriptRoot/Private/Invoke-OAIBeta.ps1 . $PSScriptRoot/Private/Get-MultipartFormData.ps1 -. $PSScriptRoot/Public/Show-OAIBilling.ps1 . $PSScriptRoot/Public/Add-OAIVectorStore.ps1 . $PSScriptRoot/Public/Clear-OAIAllItems.ps1 . $PSScriptRoot/Public/Clear-OAIAssistants.ps1 @@ -50,6 +49,7 @@ . $PSScriptRoot/Public/Get-OpenAISpecDescriptions.ps1 . $PSScriptRoot/Public/Import-OAIAssistant.ps1 . $PSScriptRoot/Public/Invoke-AIExplain.ps1 +. $PSScriptRoot/Public/Invoke-AIPrompt.ps1 . $PSScriptRoot/Public/Invoke-FilesToPrompt.ps1 . $PSScriptRoot/Public/Invoke-OAIChat.ps1 . $PSScriptRoot/Public/Invoke-OAIChatCompletion.ps1 @@ -83,6 +83,7 @@ . $PSScriptRoot/Public/Set-OAIProvider.ps1 . $PSScriptRoot/Public/Show-OAIAPIReferenceWebPage.ps1 . $PSScriptRoot/Public/Show-OAIAssistantWebPage.ps1 +. $PSScriptRoot/Public/Show-OAIBilling.ps1 . $PSScriptRoot/Public/Show-OAIPlaygroundWebPage.ps1 . $PSScriptRoot/Public/Show-OAIVectorStoreWebPage.ps1 . $PSScriptRoot/Public/Stop-OAIRun.ps1 From d46182ae17ee9ef6e5f3bf8d287925769afa5251 Mon Sep 17 00:00:00 2001 From: dfinke Date: Fri, 2 Aug 2024 19:25:03 -0400 Subject: [PATCH 06/11] feat: Update Invoke-AIPrompt function to include model parameter --- Public/Invoke-AIPrompt.ps1 | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/Public/Invoke-AIPrompt.ps1 b/Public/Invoke-AIPrompt.ps1 index ef74a2f..2aaa0a3 100644 --- a/Public/Invoke-AIPrompt.ps1 +++ b/Public/Invoke-AIPrompt.ps1 @@ -1,8 +1,11 @@ function Invoke-AIPrompt { [CmdletBinding()] param( + [Parameter(Mandatory)] $Prompt, + [Parameter(Mandatory)] $Data, + $Model = 'gpt-4o-mini', [Switch]$UsePowerShellPersona ) @@ -17,22 +20,4 @@ function Invoke-AIPrompt { $result = Invoke-OAIChatCompletion -Messages $messages $result.choices[0].message.content -} - -$targetTypes = "System.String", "System.Array" - -foreach ($targetType in $targetTypes) { - Update-TypeData -TypeName $targetType -MemberType ScriptMethod -MemberName "Chat" -Force -Value { - param($Prompt) - - Invoke-AIPrompt -Prompt $Prompt -Data $this - } -} - -foreach ($targetType in $targetTypes) { - Update-TypeData -TypeName $targetType -MemberType ScriptMethod -MemberName "PSChat" -Force -Value { - param($Prompt) - - Invoke-AIPrompt -Prompt $Prompt -Data $this -UsePowerShellPersona - } } \ No newline at end of file From 9e9cd8567ae2bb6635a699bb5707b39fc9091e35 Mon Sep 17 00:00:00 2001 From: dfinke Date: Fri, 2 Aug 2024 19:25:09 -0400 Subject: [PATCH 07/11] feat: Add 'Invoke-AIPrompt' function to ArgumentCompletion.ps1 --- Private/ArgumentCompletion.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Private/ArgumentCompletion.ps1 b/Private/ArgumentCompletion.ps1 index f2e5913..6442512 100644 --- a/Private/ArgumentCompletion.ps1 +++ b/Private/ArgumentCompletion.ps1 @@ -34,6 +34,7 @@ function ModelArgumentCompleter { if (Get-Command -ErrorAction SilentlyContinue -name Register-ArgumentCompleter) { $functionNames = $( + 'Invoke-AIPrompt' 'Invoke-Chat' 'Invoke-OAIChat' 'Invoke-OAIChatCompletion' From 62a4b1c32517361a95f142a0dd5dcccaca49a3c1 Mon Sep 17 00:00:00 2001 From: dfinke Date: Fri, 2 Aug 2024 19:25:19 -0400 Subject: [PATCH 08/11] feat: Add AI-powered chat functionality to PSAI module --- PSAI.psm1 | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/PSAI.psm1 b/PSAI.psm1 index 15a47f4..a43042a 100644 --- a/PSAI.psm1 +++ b/PSAI.psm1 @@ -126,4 +126,22 @@ Set-Alias roaia Remove-OAIAssistant Set-Alias noaia New-OAIAssistant Set-Alias noait New-OAIThread Set-Alias uoaia Update-OAIAssistant -Set-Alias ai Invoke-OAIChat \ No newline at end of file +Set-Alias ai Invoke-OAIChat + +$targetTypes = "System.String", "System.Array" + +foreach ($targetType in $targetTypes) { + Update-TypeData -TypeName $targetType -MemberType ScriptMethod -MemberName "Chat" -Force -Value { + param($Prompt) + + Invoke-AIPrompt -Prompt $Prompt -Data $this + } +} + +foreach ($targetType in $targetTypes) { + Update-TypeData -TypeName $targetType -MemberType ScriptMethod -MemberName "PSChat" -Force -Value { + param($Prompt) + + Invoke-AIPrompt -Prompt $Prompt -Data $this -UsePowerShellPersona + } +} \ No newline at end of file From dc3c6c3049f33e815bc1a8b026bc8855379d040e Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 3 Aug 2024 07:32:12 -0400 Subject: [PATCH 09/11] Update file extension in Invoke-FilesToPrompt.ps1 to .ps1 --- Public/Invoke-FilesToPrompt.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Public/Invoke-FilesToPrompt.ps1 b/Public/Invoke-FilesToPrompt.ps1 index 71588e9..92a6803 100644 --- a/Public/Invoke-FilesToPrompt.ps1 +++ b/Public/Invoke-FilesToPrompt.ps1 @@ -5,12 +5,12 @@ .DESCRIPTION Takes one or more paths to files or directories and outputs every file, recursively, each one preceded with its filename like this: - path/to/file.py + path/to/file.ps1 ---- - Contents of file.py goes here + Contents of file.ps1 goes here --- - path/to/file2.py + path/to/file2.ps1 --- ... From fa44e4d0e6825954a288d5b9f01421c6db44c7a5 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 3 Aug 2024 07:32:23 -0400 Subject: [PATCH 10/11] feat: Update Invoke-AIPrompt function to include model parameter --- Public/Invoke-AIPrompt.ps1 | 45 +++++++++++++++++++++++++++++ __tests__/Invoke-AIPrompt.tests.ps1 | 3 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Public/Invoke-AIPrompt.ps1 b/Public/Invoke-AIPrompt.ps1 index 2aaa0a3..556d3be 100644 --- a/Public/Invoke-AIPrompt.ps1 +++ b/Public/Invoke-AIPrompt.ps1 @@ -1,3 +1,48 @@ +<# +.SYNOPSIS +Prompts the user for input using OpenAI ChatGPT and returns the generated response. + +.DESCRIPTION +The Invoke-AIPrompt function prompts the user for input using OpenAI ChatGPT and returns the generated response. It sends a series of messages to the ChatGPT model, including system messages and user messages, to guide the conversation. + +.PARAMETER Prompt +Specifies the prompt message to be displayed to the user. + +.PARAMETER Data +Specifies the data to be included in the conversation. This data will be displayed to the user before the prompt. + +.PARAMETER Model +Specifies the ChatGPT model to use for generating the response. The default value is 'gpt-4o-mini'. + +.PARAMETER UsePowerShellPersona +Indicates whether to use a PowerShell persona for the conversation. If this switch is specified, a system message will be added to the conversation to inform the model that the user is a PowerShell expert. + +.OUTPUTS +System.String +Returns the generated response from the ChatGPT model. + +.EXAMPLE +$data = @" +Region,State,Units,Price +West,Texas,927,923.71 +North,Tennessee,466,770.67 +East,Florida,520,458.68w +East,Maine,828,661.24 +West,Virginia,465,53.58 +North,Missouri,436,235.67 +South,Kansas,214,992.47 +North,North Dakota,789,640.72 +South,Delaware,712,508.55 +"@ + +Invoke-AIPrompt -Prompt "top 3" -Data $data + +This example prompts the user with the question "What is the capital of France?" and includes the question in the conversation data. The function then returns the generated response from the ChatGPT model. + +.NOTES +This function requires the Invoke-OAIChatCompletion function to be available in the current session. + +#> function Invoke-AIPrompt { [CmdletBinding()] param( diff --git a/__tests__/Invoke-AIPrompt.tests.ps1 b/__tests__/Invoke-AIPrompt.tests.ps1 index 9b7b853..86d089f 100644 --- a/__tests__/Invoke-AIPrompt.tests.ps1 +++ b/__tests__/Invoke-AIPrompt.tests.ps1 @@ -12,7 +12,8 @@ Describe "Invoke-AIPrompt" -Tag Invoke-AIPrompt { $keyArray[0] | Should -BeExactly 'Prompt' $keyArray[1] | Should -BeExactly 'Data' - $keyArray[2] | Should -BeExactly 'UsePowerShellPersona' + $keyArray[2] | Should -BeExactly 'Model' + $keyArray[3] | Should -BeExactly 'UsePowerShellPersona' $actual.Parameters['UsePowerShellPersona'].SwitchParameter | Should -Be $true } From 5e117f159a409f24c6ab231b61192d84b6d46d45 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 3 Aug 2024 07:32:27 -0400 Subject: [PATCH 11/11] feat: Extend Invoke-AIPrompt with persona and system messages, and add Update-TypeData --- changelog.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index e5ca4a4..e306306 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,7 @@ ## v0.2.6 -- Add Update-TypeData +- Added `Invoke-AIPrompt` - Uses persona and system messages +- Added `Update-TypeData` to extend `System.String` and `System.Array` ## v0.2.5