Skip to content

Instantly share code, notes, and snippets.

@Agazoth
Last active November 13, 2024 14:59
Show Gist options
  • Save Agazoth/f4c08fd7a0385dde597065d389be8582 to your computer and use it in GitHub Desktop.
Save Agazoth/f4c08fd7a0385dde597065d389be8582 to your computer and use it in GitHub Desktop.
psai
$table = @{
"UPPER" = "UPPER";
"lower" = "lower";
}
#$table["UPPER"]
#$table["upper"]
$obj= New-Object PSObject @{ Tables = $table }
Add-Member -InputObject $obj -MemberType ScriptMethod -Name 'GetEntry' -Value {
param([string]$Name)
return $this.Tables[$Name]
}
$obj.GetEntry("UPPER")
$obj.GetEntry("upper")
<#
def instructions(context_variables):
name = context_variables.get("name", "User")
return f"You are a helpful agent. Greet the user by name ({name})."
def print_account_details(context_variables: dict):
user_id = context_variables.get("user_id", None)
name = context_variables.get("name", None)
print(f"Account Details: {name} {user_id}")
return "Success"
agent = Agent(
name="Agent",
instructions=instructions,
functions=[print_account_details],
)
context_variables = {"name": "James", "user_id": 123}
response = client.run(
messages=[{"role": "user", "content": "Hi!"}],
agent=agent,
context_variables=context_variables,
)
print(response.messages[-1]["content"])
response = client.run(
messages=[{"role": "user", "content": "Print my account details!"}],
agent=agent,
context_variables=context_variables,
)
print(response.messages[-1]["content"])
#>
Import-AIProvider -Provider OpenAI -ApiKey (Get-Secret OpenAI)
function instructions {
param($context_variables)
$name = $context_variables["name"]
return "You are a helpful agent. Greet the user by name ($name)."
}
function print_account_details {
param($context_variables)
$user_id = $context_variables["user_id"]
$name = $context_variables["name"]
Write-Output "Account Details: $name $user_id"
return "Success"
}
$Agent = New-Agent -Name "Agent" -Instructions instructions -Tools @(Register-Tool "print_account_details")
$context_variables = @{"name"= "James"; "user_id"= 123}
function Get-CommandFromFile {
[CmdletBinding()]
param (
$FilePath
)
. $FilePath
$Content = Get-Content $FilePath
$regex = [regex]'(?<=function\s+)[\w-]+(?=\s*[\{\(])'
$functions = $regex.Matches($content).Value
foreach ($function in $functions) {
$scriptBlock = Get-Command $function | Select-Object -ExpandProperty ScriptBlock
Set-Item -Path "function:global:$function" -Value $scriptBlock
}
$functions
}
Import-AIProvider -Provider AzureOpenAI -ApiKey $($secrets.apiKEY | ConvertTo-SecureString -AsPlainText) -BaseUri $secrets.apiURI
$AzureAssessment = New-AIAgent -Instructions "You are an Azure Cloud Architect that analyzes and comments on Azure Governance Vizualizer data. Provided with a Path, you read the assessment files and delivers a report with comments on risks ranked between High, Medium or Low" -Tools @((Register-Tool gc), (Register-Tool ls))
$AzureAssessment.Prompt('Please provide an assessment of the AzGovViz analysis found here at Path = C:\temp\AzGovViz-Output')
function Get-Something {
[CmdletBinding()]
param (
[Parameter(HelpMessage = "Please provide a name")]
[string]$Name,
$Surname
)
Write-Output "Hello, $Name $Surname"
}
$HelpParameters = $(Get-Command Get-Something).ParameterSets[0].parameters | Select-Object Name, ParameterType, IsMandatory, HelpMessage
$HelpParameters | Format-Table
Get-OAIFunctionCallSpec -CmdletName Get-Something -ReturnJson -Verbose
<#
The above command returns:
{
"function": {
"name": "Get-Something",
"parameters": {
"type": "object",
"properties": {
"Name": {
"type": "string",
"description": "Please provide a name"
}
},
"required": [
"Name"
]
}
},
"type": "function"
}
Surname is not in the list. That is because Surname is not typed in the function. In such cases, Get-Command types parameters as System.Object.
At the moment, there is no translation for System.Object in Get-ToolProperty. It needs some consideration before translating System.Object to something specific.
Toolmakers are sometimes lazy, sometimes untyped parameters can be useful. Maybe the readme should explain this bahaviour and how to handle it.
Maybe it should be possible to add and remove parameters on a Tool info object.
#>
# Test on experimental P+M AzureOpenAI
function Get-AgentB {$AgentB}
$AgentA = New-Agent -Instructions "You are a helpful agent" -Tools (Register-Tool Get-AgentB) -ShowToolCalls
$AgentB = New-Agent -Instructions "You only speak like a pirate"
$AgentA | Invoke-InteractiveCLI
<#
😎 : What is the largest country in the world
Arr, matey! The largest country in the world be Russia, spreadin' across vast lands from Europe to Asia. It be a mighty expanse, indeed!
#>
Import-AIProvider -Provider OpenAI -ApiKey (Get-Secret OpenAI)
$english_agent = New-Agent -Name EnglishAgent -Instructions "You only speak english"
$spanish_agent = New-Agent -Name SpanishAgent -Instructions "You only speak spanish"
function TransferToSpanishAgent {
return $spanish_agent
}
$english_agent.Tools = (Register-Tool TransferToSpanishAgent)
$english_agent | Invoke-InteractiveCLI
<#
😎 : Hola. ¿Como estás
¡Hola! Estoy bien, gracias. ¿Y tú? ¿En qué puedo ayudarte hoy?
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment