Public/Get-OAIGPT.ps1

Function Get-OAIGPT {
    <#
        .SYNOPSIS
        Retrieves GPTs from the OpenAI Compliance API.
 
        .DESCRIPTION
        Retrieves workspace GPTs from the ChatGPT Enterprise compliance API. Can retrieve all GPTs,
        limit results, or get a specific GPT by ID.
 
        .PARAMETER All
        Retrieves all workspace GPTs.
 
        .PARAMETER Top
        Limits the number of GPTs to retrieve.
 
        .PARAMETER GPTId
        Retrieves a specific GPT by ID.
 
        .INPUTS
        System.String
         
        .OUTPUTS
        System.Object[]
 
        .EXAMPLE
        Get-OAIGPT -All
 
        .EXAMPLE
        Get-OAIGPT -Top 25
 
        .EXAMPLE
        Get-OAIGPT -GPTId "gpt-123456789"
 
    #>

    [CmdletBinding(DefaultParameterSetName="All")]
    [OutputType([System.Object[]])]
    param(
        [Parameter(Mandatory=$true, Position=0, ParameterSetName="All")]
        [switch]$All,
        [Parameter(Mandatory=$true, Position=0, ParameterSetName="Top")]
        [ValidateRange(1, [int]::MaxValue)]
        [int]$Top,
        [Parameter(Mandatory=$true, Position=0, ParameterSetName="ById")]
        [string]$GPTId
    
    )
    Begin {
        Write-Debug "Validating OpenAI Compliance client initialization"
        If (!$script:client) {
            Write-Error "OpenAI Compliance client not initialized. Please run Initialize-OAICompliance first." -ErrorAction Stop
            
        }
        Write-Debug "Creating OAI GPT manager"
        $gpt_manager = [OAIGPT]::new($script:client)

    } Process {
        Write-Debug "Retrieving workspace GPTs with parameter set: $($PSCmdlet.ParameterSetName)"
        Try {
            Switch ($PSCmdlet.ParameterSetName) {
                "All" {
                    $response = $gpt_manager.GetGPTs($null)

                } "Top" {
                    $response = $gpt_manager.GetGPTs($top)

                } "ById" {
                    $response = $gpt_manager.GetGPT($gptId)

                }
            }
            Write-Debug "Response retrieved successfully"
                
        } Catch {
            Write-Error "Error retrieving workspace GPTs: $($_.Exception.Message)" -ErrorAction Stop
        
        }
    } End {
        Write-Debug "Successfully retrieved workspace GPTs"
        $response
    
    }
}