Skip to content

Commit b7a99c0

Browse files
committed
Add Container endpoint
1 parent ae7bc2d commit b7a99c0

10 files changed

Lines changed: 1092 additions & 5 deletions

PSOpenAI.psd1

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
'Clear-OpenAIContext',
5050
'Set-OpenAIContext',
5151
### OpenAI ###
52+
#### Assistant ####
5253
'Get-Assistant',
5354
'New-Assistant',
5455
'Remove-Assistant',
5556
'Set-Assistant',
57+
#### Thread & Run ####
5658
'Get-Thread',
5759
'New-Thread',
5860
'Remove-Thread',
@@ -67,35 +69,51 @@
6769
'Receive-ThreadRun',
6870
'Submit-ToolOutput',
6971
'Get-ThreadRunStep',
70-
'New-ChatCompletionFunction',
71-
'Enter-ChatGPT',
72+
#### Model ####
7273
'Get-OpenAIModels',
74+
#### Audio ####
7375
'Request-AudioSpeech',
7476
'Request-AudioTranscription',
7577
'Request-AudioTranslation',
78+
#### Completions ####
79+
'Request-TextCompletion',
7680
'Request-ChatCompletion',
7781
'Get-ChatCompletion',
7882
'Set-ChatCompletion',
7983
'Remove-ChatCompletion',
84+
#### Embedding ####
8085
'Request-Embeddings',
86+
#### Image ####
8187
'Request-ImageEdit',
8288
'Request-ImageGeneration',
8389
'Request-ImageVariation',
90+
#### Moderation ####
8491
'Request-Moderation',
92+
#### Responses ####
8593
'Request-Response',
8694
'Get-Response',
8795
'Get-ResponseInputItem',
8896
'Remove-Response',
89-
'Request-TextCompletion',
97+
#### Files ####
9098
'Add-OpenAIFile',
9199
'Remove-OpenAIFile',
92100
'Get-OpenAIFile',
93101
'Get-OpenAIFileContent',
102+
#### Containers ####
103+
'New-Container',
104+
'Get-Container',
105+
'Remove-Container',
106+
'Add-ContainerFile',
107+
'Get-ContainerFile',
108+
'Get-ContainerFileContent',
109+
'Remove-ContainerFile',
110+
#### Batch ####
94111
'Start-Batch',
95112
'Get-Batch',
96113
'Wait-Batch',
97114
'Stop-Batch',
98115
'Get-BatchOutput',
116+
#### VectorStore ####
99117
'New-VectorStore',
100118
'Get-VectorStore',
101119
'Set-VectorStore',
@@ -108,7 +126,7 @@
108126
'Start-VectorStoreFileBatch',
109127
'Stop-VectorStoreFileBatch',
110128
'Wait-VectorStoreFileBatch',
111-
### Realtime ###
129+
#### Realtime ####
112130
'Connect-RealtimeSession',
113131
'Connect-RealtimeTranscriptionSession',
114132
'Disconnect-RealtimeSession',
@@ -122,7 +140,10 @@
122140
'Start-RealtimeSessionAudioOutput',
123141
'Stop-RealtimeSessionAudioOutput',
124142
'Start-RealtimeSessionAudioInput',
125-
'Stop-RealtimeSessionAudioInput'
143+
'Stop-RealtimeSessionAudioInput',
144+
#### Misc ####
145+
'New-ChatCompletionFunction',
146+
'Enter-ChatGPT'
126147
)
127148

128149
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.

Private/Get-OpenAIAPIEndpoint.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@ function Get-OpenAIAPIEndpoint {
222222
}
223223
continue
224224
}
225+
'Containers' {
226+
$UriBuilder.Path += 'containers'
227+
@{
228+
Name = 'containers'
229+
Method = 'Post'
230+
Uri = $UriBuilder.Uri
231+
ContentType = 'application/json'
232+
}
233+
continue
234+
}
235+
'Container.Files' {
236+
$UriBuilder.Path += 'containers/{0}/files'
237+
@{
238+
Name = 'container_files'
239+
Method = 'Post'
240+
Uri = $UriBuilder.Uri
241+
ContentType = 'application/json'
242+
}
243+
continue
244+
}
225245
'Realtime' {
226246
$UriBuilder.Path += 'realtime'
227247
if ($UriBuilder.Scheme -eq 'https') {

Private/ObjectParser.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,37 @@ function ParseChatCompletionMessageObject {
469469

470470
Write-Output $OutputObject
471471
}
472+
473+
function ParseContainerObject {
474+
[CmdletBinding()]
475+
param (
476+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
477+
[PSCustomObject]$InputObject
478+
)
479+
# Add custom type name and properties to output object.
480+
$InputObject.PSObject.TypeNames.Insert(0, 'PSOpenAI.Container')
481+
('created_at', 'last_active_at') | ForEach-Object {
482+
if ($null -ne $InputObject.$_ -and ($unixtime = $InputObject.$_ -as [long])) {
483+
# convert unixtime to [DateTime] for read suitable
484+
$InputObject | Add-Member -MemberType NoteProperty -Name $_ -Value ([System.DateTimeOffset]::FromUnixTimeSeconds($unixtime).LocalDateTime) -Force
485+
}
486+
}
487+
Write-Output $InputObject
488+
}
489+
490+
function ParseContainerFileObject {
491+
[CmdletBinding()]
492+
param (
493+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
494+
[PSCustomObject]$InputObject
495+
)
496+
# Add custom type name and properties to output object.
497+
$InputObject.PSObject.TypeNames.Insert(0, 'PSOpenAI.Container.File')
498+
('created_at') | ForEach-Object {
499+
if ($null -ne $InputObject.$_ -and ($unixtime = $InputObject.$_ -as [long])) {
500+
# convert unixtime to [DateTime] for read suitable
501+
$InputObject | Add-Member -MemberType NoteProperty -Name $_ -Value ([System.DateTimeOffset]::FromUnixTimeSeconds($unixtime).LocalDateTime) -Force
502+
}
503+
}
504+
Write-Output $InputObject
505+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)