|
| 1 | +function Add-ContainerFile { |
| 2 | + [CmdletBinding(DefaultParameterSetName = 'ContainerId_FileId')] |
| 3 | + [OutputType([pscustomobject])] |
| 4 | + param ( |
| 5 | + [Parameter(ParameterSetName = 'Container_FileId', Mandatory, Position = 0, ValueFromPipeline)] |
| 6 | + [Parameter(ParameterSetName = 'Container_File', Mandatory, Position = 0, ValueFromPipeline)] |
| 7 | + [PSTypeName('PSOpenAI.Container')]$Container, |
| 8 | + |
| 9 | + [Parameter(ParameterSetName = 'ContainerId_FileId', Mandatory, Position = 0)] |
| 10 | + [Parameter(ParameterSetName = 'ContainerId_File', Mandatory, Position = 0)] |
| 11 | + [ValidateNotNullOrEmpty()] |
| 12 | + [Alias('container_id')] |
| 13 | + [string][UrlEncodeTransformation()]$ContainerId, |
| 14 | + |
| 15 | + [Parameter(ParameterSetName = 'Container_FileId', Mandatory, Position = 1)] |
| 16 | + [Parameter(ParameterSetName = 'ContainerId_FileId', Mandatory, Position = 1)] |
| 17 | + [ValidateNotNullOrEmpty()] |
| 18 | + [string][UrlEncodeTransformation()]$FileId, |
| 19 | + |
| 20 | + [Parameter(ParameterSetName = 'Container_File', Mandatory, Position = 1, ValueFromPipeline)] |
| 21 | + [Parameter(ParameterSetName = 'ContainerId_File', Mandatory, Position = 1, ValueFromPipeline)] |
| 22 | + [string]$File, |
| 23 | + |
| 24 | + [Parameter()] |
| 25 | + [int]$TimeoutSec = 0, |
| 26 | + |
| 27 | + [Parameter()] |
| 28 | + [ValidateRange(0, 100)] |
| 29 | + [int]$MaxRetryCount = 0, |
| 30 | + |
| 31 | + [Parameter()] |
| 32 | + [OpenAIApiType]$ApiType = [OpenAIApiType]::OpenAI, |
| 33 | + |
| 34 | + [Parameter()] |
| 35 | + [System.Uri]$ApiBase, |
| 36 | + |
| 37 | + [Parameter(DontShow)] |
| 38 | + [string]$ApiVersion, |
| 39 | + |
| 40 | + [Parameter()] |
| 41 | + [ValidateSet('openai', 'azure', 'azure_ad')] |
| 42 | + [string]$AuthType = 'openai', |
| 43 | + |
| 44 | + [Parameter()] |
| 45 | + [securestring][SecureStringTransformation()]$ApiKey, |
| 46 | + |
| 47 | + [Parameter()] |
| 48 | + [Alias('OrgId')] |
| 49 | + [string]$Organization, |
| 50 | + |
| 51 | + [Parameter()] |
| 52 | + [System.Collections.IDictionary]$AdditionalQuery, |
| 53 | + |
| 54 | + [Parameter()] |
| 55 | + [System.Collections.IDictionary]$AdditionalHeaders, |
| 56 | + |
| 57 | + [Parameter()] |
| 58 | + [object]$AdditionalBody |
| 59 | + ) |
| 60 | + |
| 61 | + begin { |
| 62 | + # Get API context |
| 63 | + $OpenAIParameter = Get-OpenAIAPIParameter -EndpointName 'Container.Files' -Parameters $PSBoundParameters -ErrorAction Stop |
| 64 | + } |
| 65 | + |
| 66 | + process { |
| 67 | + # Get ids |
| 68 | + if ($PSCmdlet.ParameterSetName -like 'Container_*') { |
| 69 | + $ContainerId = $Container.id |
| 70 | + } |
| 71 | + if (-not $ContainerId) { |
| 72 | + Write-Error -Exception ([System.ArgumentException]::new('Could not retrieve container id.')) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + #region Construct parameters for API request |
| 77 | + $QueryUri = $OpenAIParameter.Uri.ToString() -f $ContainerId |
| 78 | + $PostBody = [System.Collections.Specialized.OrderedDictionary]::new() |
| 79 | + |
| 80 | + if ($PSCmdlet.ParameterSetName -like '_FileId') { |
| 81 | + $PostBody.file_id = $FileId |
| 82 | + $OpenAIParameter.ContentType = 'application/json' |
| 83 | + } |
| 84 | + elseif (Test-Path -LiteralPath $File -PathType Leaf) { |
| 85 | + $PostBody.file = Resolve-FileInfo $File |
| 86 | + $OpenAIParameter.ContentType = 'multipart/form-data' |
| 87 | + } |
| 88 | + else { |
| 89 | + $PostBody.file_id = [string]$File |
| 90 | + $OpenAIParameter.ContentType = 'application/json' |
| 91 | + } |
| 92 | + #endregion |
| 93 | + |
| 94 | + #region Send API Request |
| 95 | + $params = @{ |
| 96 | + Method = 'Post' |
| 97 | + Uri = $QueryUri |
| 98 | + ContentType = $OpenAIParameter.ContentType |
| 99 | + TimeoutSec = $OpenAIParameter.TimeoutSec |
| 100 | + MaxRetryCount = $OpenAIParameter.MaxRetryCount |
| 101 | + ApiKey = $OpenAIParameter.ApiKey |
| 102 | + AuthType = $OpenAIParameter.AuthType |
| 103 | + Organization = $OpenAIParameter.Organization |
| 104 | + Body = $PostBody |
| 105 | + AdditionalQuery = $AdditionalQuery |
| 106 | + AdditionalHeaders = $AdditionalHeaders |
| 107 | + AdditionalBody = $AdditionalBody |
| 108 | + } |
| 109 | + $Response = Invoke-OpenAIAPIRequest @params |
| 110 | + |
| 111 | + # error check |
| 112 | + if ($null -eq $Response) { |
| 113 | + return |
| 114 | + } |
| 115 | + #endregion |
| 116 | + |
| 117 | + #region Parse response object |
| 118 | + try { |
| 119 | + $Response = $Response | ConvertFrom-Json -ErrorAction Stop |
| 120 | + } |
| 121 | + catch { |
| 122 | + Write-Error -Exception $_.Exception |
| 123 | + } |
| 124 | + #endregion |
| 125 | + |
| 126 | + #region Output |
| 127 | + ParseContainerFileObject -InputObject $Response |
| 128 | + #endregion |
| 129 | + } |
| 130 | + |
| 131 | + end { |
| 132 | + |
| 133 | + } |
| 134 | +} |
0 commit comments