Skip to content

Commit

Permalink
Merge pull request #36 from dfinke/Integrate-my-PSAI-Agents-module
Browse files Browse the repository at this point in the history
Integrate my PSAI Agents module #35
  • Loading branch information
dfinke authored Oct 6, 2024
2 parents a51d58a + 616e2b0 commit 59ce870
Show file tree
Hide file tree
Showing 25 changed files with 1,160 additions and 26 deletions.
14 changes: 13 additions & 1 deletion PSAI.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
RootModule = 'PSAI.psm1'
ModuleVersion = '0.2.10'
ModuleVersion = '0.2.11'
GUID = '68662d19-a8f1-484f-b1b7-3bf0e8a436df'
Author = 'Douglas Finke'
CompanyName = 'Doug Finke'
Expand All @@ -21,7 +21,15 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in
'Get-MultiPartFormData'
'Invoke-OAIBeta'

# Agent Tools
'New-CalculatorTool'
'New-TavilyAITool'
'New-StockTickerTool'

# Public
'New-Agent'
'Get-AgentResponse'
'Invoke-InteractiveCLI'
'Add-OAIVectorStore'
'Clear-OAIAllItems'
'Clear-OAIAssistants'
Expand All @@ -31,6 +39,7 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in
'ConvertFrom-OAIAssistant'
'ConvertFrom-UnixTimestamp'
'ConvertTo-OAIAssistant'
'ConvertTo-OAIMessage'
'ConvertTo-OpenAIFunctionSpec'
'ConvertTo-OpenAIFunctionSpecDataType'
'ConvertTo-ToolFormat'
Expand Down Expand Up @@ -76,6 +85,7 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in
'Invoke-FilesToPrompt'
'Invoke-OAIChat'
'Invoke-OAIChatCompletion'
'Invoke-OAIFunctionCall'
'Invoke-OAIUploadFile'
'Invoke-QuickChat'
'Invoke-SimpleQuestion'
Expand All @@ -94,7 +104,9 @@ PSAI brings OpenAI ChatGPT to PowerShell, leveraging advanced AI capabilities in
'New-OAIVectorStore'
'New-OAIVectorStoreFile'
'New-OAIVectorStoreFileBatch'
'New-OpenAIChat'
'Out-OAIMessages'
'Register-Tool'
'Remove-OAIAssistant'
'Remove-OAIFile'
'Remove-OAIMessage'
Expand Down
13 changes: 13 additions & 0 deletions PSAI.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
. $PSScriptRoot/Private/Invoke-OAIBeta.ps1
. $PSScriptRoot/Private/Get-MultipartFormData.ps1

# Agent Tools
Import-Module $PSScriptRoot/Public/Tools/CalculatorTool.psm1
Import-Module $PSScriptRoot/Public/Tools/TavilyTool.psm1
Import-Module $PSScriptRoot/Public/Tools/StockTickerTool.psm1

# Public Functions
. $PSScriptRoot/Public/New-Agent.ps1
. $PSScriptRoot/Public/Get-AgentResponse.ps1
. $PSScriptRoot/Public/Invoke-InteractiveCLI.ps1
. $PSScriptRoot/Public/Add-OAIVectorStore.ps1
. $PSScriptRoot/Public/Clear-OAIAllItems.ps1
. $PSScriptRoot/Public/Clear-OAIAssistants.ps1
Expand All @@ -12,6 +21,7 @@
. $PSScriptRoot/Public/ConvertFrom-OAIAssistant.ps1
. $PSScriptRoot/Public/ConvertFrom-UnixTimestamp.ps1
. $PSScriptRoot/Public/ConvertTo-OAIAssistant.ps1
. $PSScriptRoot/Public/ConvertTo-OAIMessage.ps1
. $PSScriptRoot/Public/ConvertTo-OpenAIFunctionSpec.ps1
. $PSScriptRoot/Public/ConvertTo-OpenAIFunctionSpecDataType.ps1
. $PSScriptRoot/Public/ConvertTo-ToolFormat.ps1
Expand Down Expand Up @@ -53,9 +63,11 @@
. $PSScriptRoot/Public/Invoke-FilesToPrompt.ps1
. $PSScriptRoot/Public/Invoke-OAIChat.ps1
. $PSScriptRoot/Public/Invoke-OAIChatCompletion.ps1
. $PSScriptRoot/Public/Invoke-OAIFunctionCall.ps1
. $PSScriptRoot/Public/Invoke-OAIUploadFile.ps1
. $PSScriptRoot/Public/Invoke-QuickChat.ps1
. $PSScriptRoot/Public/Invoke-SimpleQuestion.ps1
. $PSScriptRoot/Public/llm/openai/New-OpenAIChat.ps1
. $PSScriptRoot/Public/New-ChatRequestAssistantMessage.ps1
. $PSScriptRoot/Public/New-ChatRequestSystemMessage.ps1
. $PSScriptRoot/Public/New-ChatRequestToolMessage.ps1
Expand All @@ -72,6 +84,7 @@
. $PSScriptRoot/Public/New-OAIVectorStoreFile.ps1
. $PSScriptRoot/Public/New-OAIVectorStoreFileBatch.ps1
. $PSScriptRoot/Public/Out-OAIMessages.ps1
. $PSScriptRoot/Public/Register-Tool.ps1
. $PSScriptRoot/Public/Remove-OAIAssistant.ps1
. $PSScriptRoot/Public/Remove-OAIFile.ps1
. $PSScriptRoot/Public/Remove-OAIMessage.ps1
Expand Down
28 changes: 28 additions & 0 deletions Public/ConvertTo-OAIMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<#
.SYNOPSIS
Converts a message to an OpenAPI message format.
.DESCRIPTION
The ConvertTo-OAIMessage function takes a message as input and converts it to an OpenAPI message format. It uses the ConvertTo-Json cmdlet to convert the message to JSON format and then uses the ConvertFrom-Json cmdlet with the -AsHashtable parameter to convert the JSON back to a hashtable.
.PARAMETER Message
The message to be converted.
.EXAMPLE
$m=[pscustomobject](New-ChatRequestAssistantMessage test)
$m
$m | ConvertTo-OAIMessage
#>

function ConvertTo-OAIMessage {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
$Message
)

Process {
$Message | ConvertTo-Json -Depth 5 | ConvertFrom-Json -AsHashtable -Depth 5
}
}
39 changes: 39 additions & 0 deletions Public/Get-AgentResponse.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<#
.SYNOPSIS
Retrieves a response from a PSAIAgent based on a given prompt.
.DESCRIPTION
The Get-AgentResponse function takes a prompt string and an agent object, validates the agent, and retrieves a response from the agent. The agent must be of type 'PSAIAgent'.
.PARAMETER Prompt
The prompt string to be sent to the agent.
.PARAMETER Agent
The agent object from which the response is to be retrieved. This parameter accepts input from the pipeline.
.EXAMPLE
PS C:\> Get-AgentResponse -Prompt "Hello, how are you?" -Agent $myAgent
.EXAMPLE
PS C:\> $myAgent | Get-AgentResponse -Prompt "What is the weather today?"
.NOTES
The agent object must be of type 'PSAIAgent'. If the agent is not valid, an error will be thrown.
#>
function Get-AgentResponse {
[CmdletBinding()]
param(
[string]$Prompt,
[Parameter(ValueFromPipeline)]
$Agent
)

Process {
if (!($agent.psobject.TypeNames -contains 'PSAIAgent')) {
Write-Error "Agent is not a valid PSAIAgent"
return
}

$agent.PrintResponse($Prompt)
}
}
53 changes: 53 additions & 0 deletions Public/Invoke-InteractiveCLI.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<#
.SYNOPSIS
Invokes an interactive command-line interface (CLI) for a given PSAIAgent.
.DESCRIPTION
The Invoke-InteractiveCLI function initiates an interactive CLI session for a specified PSAIAgent object.
It allows the user to send a message, specify a user, and optionally include an emoji and an exit condition.
.PARAMETER Agent
The PSAIAgent object that will be used to invoke the interactive CLI. This parameter accepts input from the pipeline.
.PARAMETER Message
The message to be displayed in the interactive CLI.
.PARAMETER User
The user associated with the interactive CLI session.
.PARAMETER Emoji
An optional emoji to be displayed in the interactive CLI. The default value is '😎'.
.PARAMETER ExitOn
An optional parameter that specifies the condition on which the interactive CLI should exit.
.EXAMPLE
PS C:\> $agent = Get-PSAIAgent
PS C:\> Invoke-InteractiveCLI -Agent $agent -Message "Hello, World!" -User "Admin"
This example invokes the interactive CLI for the specified PSAIAgent with the message "Hello, World!" and user "Admin".
.NOTES
The function checks if the provided Agent is a valid PSAIAgent object before invoking the interactive CLI.
If the Agent is not valid, an error is written and the function returns without further execution.
#>
function Invoke-InteractiveCLI {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
$Agent,
$Message,
$User,
$Emoji = '😎',
$ExitOn
)

Process {
if (!($agent.psobject.TypeNames -contains 'PSAIAgent')) {
Write-Error "Agent is not a valid PSAIAgent"
return
}

$agent.InteractiveCLI($Message, $User, $Emoji, $ExitOn)
}
}
37 changes: 37 additions & 0 deletions Public/Invoke-OAIFunctionCall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<#
.SYNOPSIS
Invokes the specified tool functions based on the response received.
.DESCRIPTION
The Invoke-OAIFunctionCall function is used to invoke the specified tool functions based on the response received. It iterates through the tool calls in the response and executes each tool function.
.PARAMETER Response
The response object containing the tool calls.
#>
function Invoke-OAIFunctionCall {
[CmdletBinding()]
param(
$Response
)

$toolCalls = $Response.choices[0].message.tool_calls

# Write-Verbose ($toolCalls | dumpJson)

foreach ($toolCall in $toolCalls) {
$toolCallId = $toolCall.id
$toolFunctionName = $toolCall.function.name
$toolFunctionArgs = $toolCall.function.arguments | ConvertFrom-Json -AsHashtable -Depth 5

Write-Verbose "$toolFunctionName $($toolFunctionArgs | ConvertTo-Json -Compress)"

if (Get-Command $toolFunctionName -ErrorAction SilentlyContinue) {
$result = & $toolFunctionName @toolFunctionArgs
}
else {
$result = "Function $toolFunctionName not found"
}

New-ChatRequestToolMessage $toolCallId $toolFunctionName $result
}
}
Loading

0 comments on commit 59ce870

Please sign in to comment.