Skip to content

Commit

Permalink
Merge pull request #83 from Agazoth/EnhanceToolParameter
Browse files Browse the repository at this point in the history
EnhanceToolParameter
  • Loading branch information
dfinke authored Dec 31, 2024
2 parents 4fde007 + c1acee9 commit 45ebf03
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 10 deletions.
4 changes: 3 additions & 1 deletion PSAI.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
RootModule = 'PSAI.psm1'
ModuleVersion = '0.3.0'
ModuleVersion = '0.4.0'
GUID = '68662d19-a8f1-484f-b1b7-3bf0e8a436df'
Author = 'Douglas Finke'
CompanyName = 'Doug Finke'
Expand Down Expand Up @@ -35,6 +35,7 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in
'Get-YouTubeTop10'

# Public
'Invoke-QuickPrompt'
'New-Agent'
'Get-AgentResponse'
'Invoke-InteractiveCLI'
Expand Down Expand Up @@ -153,6 +154,7 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in
'noait'
'roaia'
'uoaia'
'q'
)

PrivateData = @{
Expand Down
2 changes: 2 additions & 0 deletions PSAI.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Import-Module $PSScriptRoot/Public/Tools/YouTubeTool.psm1
Import-Module $PSScriptRoot/Public/Tools/YouTubeAssistant.psm1

# Public Functions
. $PSScriptRoot/Public/Invoke-QuickPrompt.ps1
. $PSScriptRoot/Public/New-Agent.ps1
. $PSScriptRoot/Public/Get-AgentResponse.ps1
. $PSScriptRoot/Public/Invoke-InteractiveCLI.ps1
Expand Down Expand Up @@ -145,6 +146,7 @@ Set-Alias noaia New-OAIAssistant
Set-Alias noait New-OAIThread
Set-Alias uoaia Update-OAIAssistant
Set-Alias ai Invoke-OAIChat
Set-Alias q Invoke-QuickPrompt

$targetTypes = "System.String", "System.Array"

Expand Down
2 changes: 1 addition & 1 deletion Private/Get-ToolProperty.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Get-ToolProperty {
"System.Object" { $property.Add("type", "object") }
"psobject[]" { $property.Add("type", "object"), $property.Add("items", @{type = "object" }) }
"System.Object[]" { $property.Add("type", "object"), $property.Add("items", @{type = "object" }) }
{ $_ -match 'decimal|float|single|int|long' } { $property.Add("type", "number") }
{ $_ -match 'decimal|float|single|int|long|double' } { $property.Add("type", "number") }
{ $_ -match 'switch|bool|boolean' } { $property.Add("type", "boolean") }
default { $property.Add("type", "object") ; Write-Verbose "Unknown type: $_ - added as object" }
}
Expand Down
77 changes: 77 additions & 0 deletions Public/Invoke-QuickPrompt.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function Invoke-QuickPrompt {
[CmdletBinding()]
param(
[string]$targetPrompt,
[Parameter(ValueFromPipeline = $true)]
[object]$pipelineInput,
[switch]$OutputOnly
)

Begin {
$additionalInstructions = @()

}

Process {
$additionalInstructions += $pipelineInput
}

End {

$prompt = "work it"
if ($targetPrompt) {
$prompt = $targetPrompt
}

$instructions += @"
<date>$(Get-Date)</date>
<current directory>$($pwd)</current directory>
- You are a terminal assistant. You are a software and data science expert. Your preference is PowerShell, unless otherwise directed.
for code answers:
- do not include fence blocks around the code `````` ``````
- do not include explanation
- do not include usage information
- just code
"@


if ($additionalInstructions) {
$prompt += @"
Here are the additional instructions Fthe user piped in:
<additional instructions>
$($additionalInstructions -join "`n")
</additional instructions>
"@
}

Write-Verbose @"
Instructions: $instructions
Prompt: $prompt
"@

$agent = New-Agent -Instructions $instructions

if($OutputOnly) {
$agent | Get-AgentResponse -Prompt $prompt
return
}

While ($true) {
$agentResponse = $agent | Get-AgentResponse $prompt

Format-SpectrePanel -Data (Get-SpectreEscapedText -Text $agentResponse) -Title "Agent Response" -Border "Rounded" -Color "Blue"
Format-SpectrePanel -Data "Follow up, Enter to copy & quit, Ctrl+C to quit." -Title "Next Steps" -Border "Rounded" -Color "Cyan1"

$prompt = Read-Host '> '
if ([string]::IsNullOrEmpty($prompt)) {
Format-SpectrePanel -Data "Copied to clipboard." -Title "Information" -Border "Rounded" -Color "Green"

$agentResponse | Set-Clipboard
break
}
}
}
}
14 changes: 13 additions & 1 deletion Public/New-Agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,25 @@ function New-Agent {
)

$script:messages = @()

if ($null -ne $Tools) {
$ReadyTools = @()
foreach ($t in $Tools) {
if ($t.GetType().Name -eq "string") {
$ReadyTools += Register-Tool $t
}
else {
$ReadyTools += $t
}
}
}

#Write-Verbose "[$((Get-Date).ToString("yyMMdd:hhmmss"))] New-Agent was called"
Write-Verbose ("{0} New-Agent was called" -f (Get-LogDate))
Write-Verbose ("{0} Tools: {1}" -f (Get-LogDate), (ConvertTo-Json -Depth 10 -InputObject $Tools))

$agent = [PSCustomObject]@{
Tools = $Tools
Tools = $ReadyTools
ShowToolCalls = $ShowToolCalls
LLM = $LLM
}
Expand Down
16 changes: 9 additions & 7 deletions __tests__/Get-ToolProperty.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Describe "Get-ToolProperty" -Tag Get-ToolProperty {
[switch]$isHappy,
[decimal]$money,
[float]$float,
[single]$single,
[single]$single,
[double]$double,
[bool]$bool,
[string[]]$stringArray,
[psobject]$targetObject,
Expand Down Expand Up @@ -52,15 +53,16 @@ Describe "Get-ToolProperty" -Tag Get-ToolProperty {
3 { $properties.type | Should -Be 'number' }
4 { $properties.type | Should -Be 'number' }
5 { $properties.type | Should -Be 'number' }
6 { $properties.type | Should -Be 'boolean' }
7 { $properties.type | Should -Be 'array' }
8 { $properties.type | Should -Be 'object' }
6 { $properties.type | Should -Be 'number' }
7 { $properties.type | Should -Be 'boolean' }
8 { $properties.type | Should -Be 'array' }
9 { $properties.type | Should -Be 'object' }
10 { $properties.type | Should -Be 'object' }
11 { $properties.type | Should -Be 'number' }
12 { $properties.type | Should -Be 'string' }
13 { $properties.type | Should -Be 'object' }
11 { $properties.type | Should -Be 'object' }
12 { $properties.type | Should -Be 'number' }
13 { $properties.type | Should -Be 'string' }
14 { $properties.type | Should -Be 'object' }
15 { $properties.type | Should -Be 'object' }
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions __tests__/Invoke-QuickPrompt.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Describe 'Test Invoke-QuickPrompt' -Tag Invoke-QuickPrompt {
BeforeAll {
Import-Module "$PSScriptRoot/../PSAI.psd1" -Force
}

It "Test if it Invoke-QuickPrompt exists" {
$actual = Get-Command Invoke-QuickPrompt -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}

It "Test if it q alias exists" {
$actual = Get-Command q -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}
}
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v0.4.0

- Big thank you to [Axel Andersen](https://x.com/Agazoth)

Axel dove into PSAI, understood the codebase and the direction, and then added a ton of value. This is one of many PRs to come from Axel.

- Updated the `Register-Tool` function to support function-calling schemas and work with built-in PowerShell functions.

## v0.3.0

- Fix multi line description transpiled to Json
Expand Down

0 comments on commit 45ebf03

Please sign in to comment.