Public/Assistants/Get-Assistant.ps1
function Get-Assistant { [CmdletBinding(DefaultParameterSetName = 'List')] [OutputType([pscustomobject])] param ( [Parameter(ParameterSetName = 'Get_Assistant', Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [Alias('InputObject')] # for backword compatibility [PSTypeName('PSOpenAI.Assistant')]$Assistant, [Parameter(ParameterSetName = 'Get_Id', Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [Alias('assistant_id')] [string][UrlEncodeTransformation()]$AssistantId, [Parameter(ParameterSetName = 'List')] [ValidateRange(1, 100)] [int]$Limit = 20, [Parameter(ParameterSetName = 'List')] [switch]$All, [Parameter(ParameterSetName = 'List', DontShow)] [string]$After, [Parameter(ParameterSetName = 'List', DontShow)] [string]$Before, [Parameter(ParameterSetName = 'List')] [ValidateSet('asc', 'desc')] [string][LowerCaseTransformation()]$Order = 'asc', [Parameter()] [int]$TimeoutSec = 0, [Parameter()] [ValidateRange(0, 100)] [int]$MaxRetryCount = 0, [Parameter()] [OpenAIApiType]$ApiType = [OpenAIApiType]::OpenAI, [Parameter()] [System.Uri]$ApiBase, [Parameter(DontShow)] [string]$ApiVersion, [Parameter()] [ValidateSet('openai', 'azure', 'azure_ad')] [string]$AuthType = 'openai', [Parameter()] [securestring][SecureStringTransformation()]$ApiKey, [Parameter()] [Alias('OrgId')] [string]$Organization, [Parameter()] [System.Collections.IDictionary]$AdditionalQuery, [Parameter()] [System.Collections.IDictionary]$AdditionalHeaders, [Parameter()] [object]$AdditionalBody ) begin { # Get API context $OpenAIParameter = Get-OpenAIAPIParameter -EndpointName 'Assistants' -Parameters $PSBoundParameters -ErrorAction Stop } process { # Get assistant_id if ($PSCmdlet.ParameterSetName -like '*_Assistant') { $AssistantId = $Assistant.id if (-not $AssistantId) { Write-Error -Exception ([System.ArgumentException]::new('Could not retrieve assistant id.')) return } } $UriBuilder = [System.UriBuilder]::new($OpenAIParameter.Uri) if ($PSCmdlet.ParameterSetName -like 'Get_*') { $UriBuilder.Path += "/$AssistantId" $QueryUri = $UriBuilder.Uri } else { $QueryParam = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query) if ($All) { $Limit = 100 } $QueryParam.Add('limit', $Limit) $QueryParam.Add('order', $Order) if ($After) { $QueryParam.Add('after', $After) } if ($Before) { $QueryParam.Add('before', $Before) } $UriBuilder.Query = $QueryParam.ToString() $QueryUri = $UriBuilder.Uri } #region Send API Request $params = @{ Method = 'Get' Uri = $QueryUri ContentType = $OpenAIParameter.ContentType TimeoutSec = $OpenAIParameter.TimeoutSec MaxRetryCount = $OpenAIParameter.MaxRetryCount ApiKey = $OpenAIParameter.ApiKey AuthType = $OpenAIParameter.AuthType Organization = $OpenAIParameter.Organization Headers = @{'OpenAI-Beta' = 'assistants=v2' } AdditionalQuery = $AdditionalQuery AdditionalHeaders = $AdditionalHeaders AdditionalBody = $AdditionalBody } $Response = Invoke-OpenAIAPIRequest @params # 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) { ParseAssistantsObject $res } #endregion #region Pagenation if ($Response.has_more) { if ($All) { # 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 { } } |