Skip to content

Commit

Permalink
Merge pull request #20 from dfinke/add-type-for-pschat
Browse files Browse the repository at this point in the history
feat: Add Update-TypeData
  • Loading branch information
dfinke authored Aug 3, 2024
2 parents ea4f8a7 + 5e117f1 commit 27fdd15
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 9 deletions.
5 changes: 3 additions & 2 deletions PSAI.psd1
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
23 changes: 21 additions & 2 deletions PSAI.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -125,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
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
}
}
1 change: 1 addition & 0 deletions Private/ArgumentCompletion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function ModelArgumentCompleter {

if (Get-Command -ErrorAction SilentlyContinue -name Register-ArgumentCompleter) {
$functionNames = $(
'Invoke-AIPrompt'
'Invoke-Chat'
'Invoke-OAIChat'
'Invoke-OAIChatCompletion'
Expand Down
68 changes: 68 additions & 0 deletions Public/Invoke-AIPrompt.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<#
.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(
[Parameter(Mandatory)]
$Prompt,
[Parameter(Mandatory)]
$Data,
$Model = 'gpt-4o-mini',
[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
}
12 changes: 8 additions & 4 deletions Public/Invoke-FilesToPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
...
Expand All @@ -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()]
Expand Down
20 changes: 20 additions & 0 deletions __tests__/Invoke-AIPrompt.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 'Model'
$keyArray[3] | Should -BeExactly 'UsePowerShellPersona'

$actual.Parameters['UsePowerShellPersona'].SwitchParameter | Should -Be $true
}
}
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 0.2.5
## v0.2.6

- Added `Invoke-AIPrompt` - Uses persona and system messages
- Added `Update-TypeData` to extend `System.String` and `System.Array`

## v0.2.5

- Fix $model to 'gpt-4o-mini'

Expand Down

0 comments on commit 27fdd15

Please sign in to comment.