Skip to content

Commit af2eaf9

Browse files
authored
Merge pull request #146 from dfinke/Enable-Disable-Chat-Persistence
Make it so you can enable and disable persisting chat messages during a session
2 parents 465c072 + 1a9b2a9 commit af2eaf9

15 files changed

+265
-32
lines changed

PowerShellAI.psd1

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
RootModule = 'PowerShellAI.psm1'
3-
ModuleVersion = '0.7.5'
3+
ModuleVersion = '0.7.6'
44
GUID = '081ce7b4-6e63-41ca-92a7-2bf72dbad018'
55
Author = 'Douglas Finke'
66
CompanyName = 'Doug Finke'
@@ -19,9 +19,13 @@ The PowerShell AI module integrates with the OpenAI API and let's you easily acc
1919
'ConvertFrom-GPTMarkdownTable'
2020
'copilot'
2121
'Disable-AIShortCutKey'
22+
'Disable-ChatPersistence'
2223
'Enable-AIShortCutKey'
24+
'Enable-ChatPersistence'
25+
'Get-ChatCompletion'
26+
'Get-ChatPersistence'
27+
'Get-CompletionFromMessages'
2328
'Get-DalleImage'
24-
'Get-ChatCompletion'
2529
'Get-GPT3Completion'
2630
'Get-GPT4Completion'
2731
'Get-GPT4Response'

PowerShellAI.psm1

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
$Script:OpenAIKey = $null
1+
# Set the OpenAI key to null
2+
$Script:OpenAIKey = $null
23

4+
# Set the chat API provider to OpenAI
5+
$Script:ChatAPIProvider = 'OpenAI'
36

7+
# Set the chat in progress flag to false
8+
$Script:ChatInProgress = $false
9+
10+
# Create an array list to store chat messages
11+
[System.Collections.ArrayList]$Script:ChatMessages = @()
12+
13+
# Enable chat persistence
14+
$Script:ChatPersistence = $true
15+
16+
# Set the options for the chat session
17+
$Script:ChatSessionOptions = @{
18+
'model' = 'gpt-4'
19+
'temperature' = 0.0
20+
'max_tokens' = 256
21+
'top_p' = 1.0
22+
'frequency_penalty' = 0
23+
'presence_penalty' = 0
24+
'stop' = $null
25+
}
26+
27+
# Set the options for the Azure OpenAI API
28+
$Script:AzureOpenAIOptions = @{
29+
Endpoint = 'not set'
30+
DeploymentName = 'not set'
31+
ApiVersion = 'not set'
32+
}
33+
34+
# Load all PowerShell scripts in the Public and Private directories
435
foreach ($directory in @('Public', 'Private')) {
536
Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" | ForEach-Object { . $_.FullName }
637
}

Private/Invoke-RestMethodWithProgress.ps1

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function Set-APIResponseTime {
3737

3838
function Invoke-RestMethodWithProgress {
3939
param (
40-
[hashtable] $Params
40+
[hashtable] $Params,
41+
$ProgressActivity = "Thinking..."
4142
)
4243

4344
# Some hosts can't support background jobs. It's best to opt-in to this feature by using a list of supported hosts
@@ -70,10 +71,10 @@ function Invoke-RestMethodWithProgress {
7071
if($logPercent -eq 100) {
7172
$status = "API is taking longer than expected"
7273
}
73-
Write-Progress -Id 1 -Activity "Invoking AI" -Status $status -PercentComplete $logPercent
74+
Write-Progress -Id 1 -Activity $ProgressActivity -Status $status -PercentComplete $logPercent
7475
Start-Sleep -Milliseconds 50
7576
}
76-
Write-Progress -Id 1 -Activity "Invoking AI" -Completed
77+
Write-Progress -Id 1 -Activity $ProgressActivity -Completed
7778

7879
# If Invoke-RestMethod failed in the job rethrow this up to the caller so it's like a normal web error
7980
if($job.State -eq "Failed") {

Public/Disable-ChatPersistence.ps1

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function Disable-ChatPersistence {
2+
<#
3+
.SYNOPSIS
4+
Disables chat persistence.
5+
6+
.DESCRIPTION
7+
This function disables chat persistence by setting the $ChatPersistence variable to $false.
8+
9+
.EXAMPLE
10+
Disable-ChatPersistence
11+
#>
12+
$Script:ChatPersistence = $false
13+
}

Public/Enable-ChatPersistence.ps1

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function Enable-ChatPersistence {
2+
<#
3+
.SYNOPSIS
4+
Enables chat persistence.
5+
6+
.DESCRIPTION
7+
The Enable-ChatPersistence function sets the $Script:ChatPersistence variable to $true, which enables chat persistence.
8+
9+
.EXAMPLE
10+
Enable-ChatPersistence
11+
#>
12+
13+
$Script:ChatPersistence = $true
14+
}

Public/Get-ChatPersistence.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function Get-ChatPersistence {
2+
<#
3+
.SYNOPSIS
4+
Retrieves the chat persistence flag.
5+
#>
6+
$Script:ChatPersistence
7+
}

Public/Get-CompletionFromMessages.ps1

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function Get-CompletionFromMessages {
2+
<#
3+
.SYNOPSIS
4+
Gets completion suggestions based on the array of messages.
5+
6+
.DESCRIPTION
7+
The Get-CompletionFromMessages function returns completion suggestion based on the messages.
8+
9+
.PARAMETER Messages
10+
Specifies the chat messages to use for generating completion suggestions.
11+
12+
.EXAMPLE
13+
Get-CompletionFromMessages $(
14+
New-ChatMessageTemplate -Role system 'You are a PowerShell expert'
15+
New-ChatMessageTemplate -Role user 'List even numbers between 1 and 10'
16+
)
17+
#>
18+
[CmdletBinding()]
19+
param(
20+
[Parameter(Mandatory)]
21+
$Messages
22+
)
23+
24+
$payload = (Get-ChatSessionOptions).Clone()
25+
26+
$payload.messages = $messages
27+
$payload = $payload | ConvertTo-Json -Depth 10
28+
29+
$body = [System.Text.Encoding]::UTF8.GetBytes($payload)
30+
31+
if ((Get-ChatAPIProvider) -eq 'OpenAI') {
32+
$uri = Get-OpenAIChatCompletionUri
33+
}
34+
elseif ((Get-ChatAPIProvider) -eq 'AzureOpenAI') {
35+
$uri = Get-ChatAzureOpenAIURI
36+
}
37+
38+
$result = Invoke-OpenAIAPI -Uri $uri -Method 'Post' -Body $body
39+
$result.choices.message
40+
}

Public/Get-GPT4Completion.ps1

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
$Script:ChatSessionOptions = @{
2-
'model' = 'gpt-4'
3-
'temperature' = 0.0
4-
'max_tokens' = 256
5-
'top_p' = 1.0
6-
'frequency_penalty' = 0
7-
'presence_penalty' = 0
8-
'stop' = $null
9-
}
10-
11-
$Script:AzureOpenAIOptions = @{
12-
Endpoint = 'not set'
13-
DeploymentName = 'not set'
14-
ApiVersion = 'not set'
15-
}
16-
17-
$Script:ChatAPIProvider = 'OpenAI'
18-
$Script:ChatInProgress = $false
19-
20-
[System.Collections.ArrayList]$Script:ChatMessages = @()
21-
221
function Get-AzureOpenAIOptions {
232
[CmdletBinding()]
243
param()
@@ -197,6 +176,8 @@ function Reset-ChatSessionOptions {
197176
'presence_penalty' = 0
198177
'stop' = $null
199178
}
179+
180+
Enable-ChatPersistence
200181
}
201182

202183
function Clear-ChatMessages {

Public/SessionManagement.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ function Export-ChatSession {
181181
[CmdletBinding()]
182182
param ()
183183

184+
if ((Get-ChatPersistence) -eq $false) { return }
185+
184186
$sessionPath = Get-ChatSessionPath
185187
if (-not (Test-Path $sessionPath)) {
186188
New-Item -ItemType Directory -Path $sessionPath -Force | Out-Null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Import-Module "$PSScriptRoot\..\PowerShellAI.psd1" -Force
2+
3+
Describe 'Disable-ChatPersistence' -Tag ChatPersistence {
4+
5+
BeforeEach {
6+
Reset-ChatSessionOptions
7+
}
8+
9+
It 'tests the function Disable-ChatPersistence exists' {
10+
$actual = Get-Command Disable-ChatPersistence -ErrorAction SilentlyContinue
11+
$actual | Should -Not -BeNullOrEmpty
12+
}
13+
14+
It 'tests that it disables chat persistence' {
15+
Disable-ChatPersistence
16+
$actual = Get-ChatPersistence
17+
18+
$actual | Should -Be $false
19+
}
20+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Import-Module "$PSScriptRoot\..\PowerShellAI.psd1" -Force
2+
3+
Describe 'Enable-ChatPersistence' -Tag ChatPersistence {
4+
5+
AfterEach {
6+
Reset-ChatSessionOptions
7+
}
8+
9+
It 'tests the function Enable-ChatPersistence exists' {
10+
$actual = Get-Command Enable-ChatPersistence -ErrorAction SilentlyContinue
11+
$actual | Should -Not -BeNullOrEmpty
12+
}
13+
14+
It 'tests that it enables chat persistence' {
15+
Enable-ChatPersistence
16+
$actual = Get-ChatPersistence
17+
18+
$actual | Should -Be $true
19+
}
20+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Import-Module "$PSScriptRoot\..\PowerShellAI.psd1" -Force
2+
3+
Describe 'Get-ChatPersistence' -Tag ChatPersistence {
4+
It 'tests the function Get-ChatPersistence exists' {
5+
$actual = Get-Command Get-ChatPersistence -ErrorAction SilentlyContinue
6+
$actual | Should -Not -BeNullOrEmpty
7+
}
8+
9+
It 'tests the function Get-ChatPersistence returns a boolean' {
10+
$actual = Get-ChatPersistence
11+
$actual.GetType().Name | Should -Be 'Boolean'
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Import-Module $PSScriptRoot\..\PowerShellAI.psd1 -Force
2+
3+
Describe "Get-CompletionFromMessages" -Tag "Get-CompletionFromMessages" {
4+
BeforeAll {
5+
$script:savedKey = $env:OpenAIKey
6+
$env:OpenAIKey = 'sk-1234567890'
7+
}
8+
9+
AfterAll {
10+
$env:OpenAIKey = $savedKey
11+
}
12+
13+
It "tests the function Get-CompletionFromMessages exists" {
14+
$actual = Get-Command Get-CompletionFromMessages -ErrorAction SilentlyContinue
15+
$actual | Should -Not -BeNullOrEmpty
16+
}
17+
18+
It "tests Get-CompletionFromMessages has a parameter named Messages" {
19+
$actual = Get-Command Get-CompletionFromMessages -ErrorAction SilentlyContinue
20+
$actual.Parameters.Keys | Should -Contain Messages
21+
}
22+
23+
It "tests Get-CompletionFromMessages returns a response" {
24+
Mock Invoke-RestMethodWithProgress -ModuleName PowerShellAI -ParameterFilter {
25+
$Params.Method -eq 'Post' -and $Params.Uri -eq (Get-OpenAIChatCompletionUri)
26+
} -MockWith {
27+
[PSCustomObject]@{
28+
choices = @(
29+
[PSCustomObject]@{
30+
message = [PSCustomObject]@{
31+
content = 'Mocked Get-GPT4Completion call'
32+
}
33+
}
34+
)
35+
}
36+
}
37+
38+
$messages = $(
39+
New-ChatMessageTemplate -Role system "I am a bot"
40+
New-ChatMessageTemplate -Role user "Hello"
41+
)
42+
43+
$actual = Get-CompletionFromMessages -Messages $messages
44+
45+
$actual.content | Should -BeExactly "Mocked Get-GPT4Completion call"
46+
}
47+
}

__tests__/SessionManagement.tests.ps1

+40-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ Describe "Session Management" -Tag SessionManagement {
1212
}
1313
}
1414

15-
BeforeEach {
16-
# Set-ChatSessionPath "TestDrive:\PowerShell\ChatGPT"
17-
}
18-
1915
AfterEach {
2016
Reset-ChatSessionPath
17+
Reset-ChatSessionOptions
2118
Clear-ChatMessages
22-
# Get-ChatSessionPath | Remove-Item -Recurse -force
2319
}
2420

2521
AfterAll {
@@ -384,4 +380,43 @@ Describe "Session Management" -Tag SessionManagement {
384380
$result[8].role | Should -BeExactly 'assistant'
385381
$result[8].content | Should -BeExactly 'assistant test 3'
386382
}
383+
384+
It "tests Export-ChatSession respects ChatPersistence flag" {
385+
Set-ChatSessionPath "TestDrive:\PowerShell\ChatGPT"
386+
Get-ChatSessionPath | Get-ChildItem | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
387+
388+
Add-ChatMessage -Message ([PSCustomObject]@{
389+
role = 'assistant'
390+
content = 'assistant test 2'
391+
})
392+
393+
Export-ChatSession
394+
395+
(Get-ChatSessionPath | Get-ChildItem ).Count | Should -Be 1
396+
397+
Get-ChatSessionPath | Get-ChildItem | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
398+
399+
Disable-ChatPersistence
400+
401+
Add-ChatMessage -Message ([PSCustomObject]@{
402+
role = 'assistant'
403+
content = 'assistant test 2'
404+
})
405+
406+
Export-ChatSession
407+
408+
(Get-ChatSessionPath | Get-ChildItem ).Count | Should -Be 0
409+
410+
Enable-ChatPersistence
411+
Get-ChatSessionPath | Get-ChildItem | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
412+
413+
Add-ChatMessage -Message ([PSCustomObject]@{
414+
role = 'assistant'
415+
content = 'assistant test 2'
416+
})
417+
418+
Export-ChatSession
419+
420+
(Get-ChatSessionPath | Get-ChildItem ).Count | Should -Be 1
421+
}
387422
}

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v0.7.6
2+
3+
- Implement mechanism to turn on/off persistence of chat message
4+
- Add `Get-CompletionFromMessages` so you can get messages can be passes standalone to get the completions
5+
16
# v0.7.5
27

38
- Fixed spaces in property names [#141](https://github.com/dfinke/PowerShellAI/issues/141)

0 commit comments

Comments
 (0)