File tree 15 files changed +265
-32
lines changed
15 files changed +265
-32
lines changed Original file line number Diff line number Diff line change 1
1
@ {
2
2
RootModule = ' PowerShellAI.psm1'
3
- ModuleVersion = ' 0.7.5 '
3
+ ModuleVersion = ' 0.7.6 '
4
4
GUID = ' 081ce7b4-6e63-41ca-92a7-2bf72dbad018'
5
5
Author = ' Douglas Finke'
6
6
CompanyName = ' Doug Finke'
@@ -19,9 +19,13 @@ The PowerShell AI module integrates with the OpenAI API and let's you easily acc
19
19
' ConvertFrom-GPTMarkdownTable'
20
20
' copilot'
21
21
' Disable-AIShortCutKey'
22
+ ' Disable-ChatPersistence'
22
23
' Enable-AIShortCutKey'
24
+ ' Enable-ChatPersistence'
25
+ ' Get-ChatCompletion'
26
+ ' Get-ChatPersistence'
27
+ ' Get-CompletionFromMessages'
23
28
' Get-DalleImage'
24
- ' Get-ChatCompletion'
25
29
' Get-GPT3Completion'
26
30
' Get-GPT4Completion'
27
31
' Get-GPT4Response'
Original file line number Diff line number Diff line change 1
- $Script :OpenAIKey = $null
1
+ # Set the OpenAI key to null
2
+ $Script :OpenAIKey = $null
2
3
4
+ # Set the chat API provider to OpenAI
5
+ $Script :ChatAPIProvider = ' OpenAI'
3
6
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
4
35
foreach ($directory in @ (' Public' , ' Private' )) {
5
36
Get-ChildItem - Path " $PSScriptRoot \$directory \*.ps1" | ForEach-Object { . $_.FullName }
6
37
}
Original file line number Diff line number Diff line change @@ -37,7 +37,8 @@ function Set-APIResponseTime {
37
37
38
38
function Invoke-RestMethodWithProgress {
39
39
param (
40
- [hashtable ] $Params
40
+ [hashtable ] $Params ,
41
+ $ProgressActivity = " Thinking..."
41
42
)
42
43
43
44
# 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 {
70
71
if ($logPercent -eq 100 ) {
71
72
$status = " API is taking longer than expected"
72
73
}
73
- Write-Progress - Id 1 - Activity " Invoking AI " - Status $status - PercentComplete $logPercent
74
+ Write-Progress - Id 1 - Activity $ProgressActivity - Status $status - PercentComplete $logPercent
74
75
Start-Sleep - Milliseconds 50
75
76
}
76
- Write-Progress - Id 1 - Activity " Invoking AI " - Completed
77
+ Write-Progress - Id 1 - Activity $ProgressActivity - Completed
77
78
78
79
# If Invoke-RestMethod failed in the job rethrow this up to the caller so it's like a normal web error
79
80
if ($job.State -eq " Failed" ) {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ function Get-ChatPersistence {
2
+ <#
3
+ . SYNOPSIS
4
+ Retrieves the chat persistence flag.
5
+ #>
6
+ $Script :ChatPersistence
7
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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
-
22
1
function Get-AzureOpenAIOptions {
23
2
[CmdletBinding ()]
24
3
param ()
@@ -197,6 +176,8 @@ function Reset-ChatSessionOptions {
197
176
' presence_penalty' = 0
198
177
' stop' = $null
199
178
}
179
+
180
+ Enable-ChatPersistence
200
181
}
201
182
202
183
function Clear-ChatMessages {
Original file line number Diff line number Diff line change @@ -181,6 +181,8 @@ function Export-ChatSession {
181
181
[CmdletBinding ()]
182
182
param ()
183
183
184
+ if ((Get-ChatPersistence ) -eq $false ) { return }
185
+
184
186
$sessionPath = Get-ChatSessionPath
185
187
if (-not (Test-Path $sessionPath )) {
186
188
New-Item - ItemType Directory - Path $sessionPath - Force | Out-Null
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -12,14 +12,10 @@ Describe "Session Management" -Tag SessionManagement {
12
12
}
13
13
}
14
14
15
- BeforeEach {
16
- # Set-ChatSessionPath "TestDrive:\PowerShell\ChatGPT"
17
- }
18
-
19
15
AfterEach {
20
16
Reset-ChatSessionPath
17
+ Reset-ChatSessionOptions
21
18
Clear-ChatMessages
22
- # Get-ChatSessionPath | Remove-Item -Recurse -force
23
19
}
24
20
25
21
AfterAll {
@@ -384,4 +380,43 @@ Describe "Session Management" -Tag SessionManagement {
384
380
$result [8 ].role | Should - BeExactly ' assistant'
385
381
$result [8 ].content | Should - BeExactly ' assistant test 3'
386
382
}
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
+ }
387
422
}
Original file line number Diff line number Diff line change
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
+
1
6
# v0.7.5
2
7
3
8
- Fixed spaces in property names [ #141 ] ( https://github.com/dfinke/PowerShellAI/issues/141 )
You can’t perform that action at this time.
0 commit comments