Public/Assistants/Get-Assistant.ps1
function Get-Assistant { [CmdletBinding(DefaultParameterSetName = 'List')] [OutputType([pscustomobject])] param ( [Parameter(ParameterSetName = 'Get', Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias('assistant_id')] [Alias('Assistant')] [ValidateScript({ ($_ -is [string] -and $_.StartsWith('asst_')) -or ` ($_.id -is [string] -and $_.id.StartsWith('asst_')) -or ` ($_.assistant_id -is [string] -and $_.assistant_id.StartsWith('asst_')) })] [Object]$InputObject, [Parameter(ParameterSetName = 'List')] [ValidateRange(1, 100)] [int]$Limit = 20, [Parameter(ParameterSetName = 'ListAll')] [switch]$All, [Parameter(ParameterSetName = 'ListAll', DontShow = $true)] [string]$After, [Parameter(ParameterSetName = 'ListAll', DontShow = $true)] [string]$Before, [Parameter(ParameterSetName = 'List')] [Parameter(ParameterSetName = 'ListAll')] [ValidateSet('asc', 'desc')] [string][LowerCaseTransformation()]$Order = 'asc', [Parameter()] [int]$TimeoutSec = 0, [Parameter()] [ValidateRange(0, 100)] [int]$MaxRetryCount = 0, [Parameter(DontShow = $true)] [OpenAIApiType]$ApiType = [OpenAIApiType]::OpenAI, [Parameter()] [System.Uri]$ApiBase, [Parameter(DontShow = $true)] [string]$ApiVersion, [Parameter(DontShow = $true)] [string]$AuthType = 'openai', [Parameter()] [securestring][SecureStringTransformation()]$ApiKey, [Parameter()] [Alias('OrgId')] [string]$Organization ) begin { # Initialize API Key [securestring]$SecureToken = Initialize-APIKey -ApiKey $ApiKey # Initialize API Base $ApiBase = Initialize-APIBase -ApiBase $ApiBase -ApiType $ApiType # Initialize Organization ID $Organization = Initialize-OrganizationID -OrgId $Organization # Get API endpoint if ($ApiType -eq [OpenAIApiType]::Azure) { $OpenAIParameter = Get-AzureOpenAIAPIEndpoint -EndpointName 'Assistants' -Engine $Model -ApiBase $ApiBase -ApiVersion $ApiVersion } else { $OpenAIParameter = Get-OpenAIAPIEndpoint -EndpointName 'Assistants' -ApiBase $ApiBase } } process { # Get assistant_id $AssistantId = '' if ($InputObject -is [string]) { $AssistantId = $InputObject } elseif ($InputObject.id -is [string] -and $InputObject.id.StartsWith('asst_')) { $AssistantId = $InputObject.id } elseif ($InputObject.assistant_id -is [string] -and $InputObject.assistant_id.StartsWith('asst_')) { $AssistantId = $InputObject.assistant_id } if ($AssistantId) { $QueryUri = $OpenAIParameter.Uri.ToString() + "/$AssistantId" } elseif ($PSCmdlet.ParameterSetName -eq 'List') { $QueryUri = $OpenAIParameter.Uri.ToString() + "?limit=$Limit&order=$Order" } else { $QueryParam = [System.Web.HttpUtility]::ParseQueryString([string]::Empty) $QueryParam.Add('limit', '100'); $QueryParam.Add('order', $Order); if ($After) { $QueryParam.Add('after', $After); } if ($Before) { $QueryParam.Add('before', $Before); } $QueryUri = $OpenAIParameter.Uri.ToString() + '?' + $QueryParam.ToString() } #region Send API Request $Response = Invoke-OpenAIAPIRequest ` -Method 'Get' ` -Uri $QueryUri ` -ContentType $OpenAIParameter.ContentType ` -TimeoutSec $TimeoutSec ` -MaxRetryCount $MaxRetryCount ` -ApiKey $SecureToken ` -AuthType $AuthType ` -Organization $Organization ` -Headers (@{'OpenAI-Beta' = 'assistants=v1' }) # error check if ($null -eq $Response) { return } #endregion #region Parse response object try { $Response = $Response | ConvertFrom-Json -ErrorAction Stop } catch { Write-Error -Exception $_.Exception } #endregion #region Output if ($Response.object -eq 'list' -and ($null -ne $Response.data)) { # List of object $Responses = @($Response.data) } else { # Single object $Responses = @($Response) } # parse objects foreach ($res in $Responses) { # Add custom type name and properties to output object. $res.PSObject.TypeNames.Insert(0, 'PSOpenAI.Assistant') if ($null -ne $res.created_at -and ($unixtime = $res.created_at -as [long])) { # convert unixtime to [DateTime] for read suitable $res | Add-Member -MemberType NoteProperty -Name 'created_at' -Value ([System.DateTimeOffset]::FromUnixTimeSeconds($unixtime).LocalDateTime) -Force } Write-Output $res } #endregion #region Pagenation if ($Response.has_more) { if ($PSCmdlet.ParameterSetName -eq 'ListAll') { # pagenate $PagenationParam = $PSBoundParameters $PagenationParam.After = $Response.last_id PSOpenAI\Get-Assistant @PagenationParam } else { Write-Warning 'There is more data that has not been retrieved.' } } #endregion } end { } } |