Skip to content

Commit f508210

Browse files
authored
中文文档似乎无法索引,报错 (#263)
Fixes #261
2 parents 5292d4f + 41f01f7 commit f508210

File tree

1 file changed

+55
-72
lines changed

1 file changed

+55
-72
lines changed

code365scripts.openai/Types/OpenAIClient.ps1

Lines changed: 55 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ class AssistantResource {
123123

124124
if ($this.objTypeName) {
125125
return $this.client.web($this.urifragment).data | ForEach-Object {
126+
$temp = "{0}/{1}" -f $this.urifragment, $_.id
126127
$result = New-Object -TypeName $this.objTypeName -ArgumentList $_
127128
$result | Add-Member -MemberType NoteProperty -Name client -Value $this.client
129+
$result | Add-Member -MemberType NoteProperty -Name urifragment -Value $temp
128130
$result
129131
}
130132
}
@@ -134,8 +136,10 @@ class AssistantResource {
134136

135137
[psobject]get([string]$id) {
136138
if ($this.objTypeName) {
137-
$result = New-Object -TypeName $this.objTypeName -ArgumentList $this.client.web("$($this.urifragment)/$id")
139+
$temp = "{0}/{1}" -f $this.urifragment, $id
140+
$result = New-Object -TypeName $this.objTypeName -ArgumentList $this.client.web($temp)
138141
$result | Add-Member -MemberType NoteProperty -Name client -Value $this.client
142+
$result | Add-Member -MemberType NoteProperty -Name urifragment -Value $temp
139143
return $result
140144
}
141145

@@ -150,6 +154,7 @@ class AssistantResource {
150154
if ($this.objTypeName) {
151155
$result = New-Object -TypeName $this.objTypeName -ArgumentList $this.client.web("$($this.urifragment)", "POST", $body)
152156
$result | Add-Member -MemberType NoteProperty -Name client -Value $this.client
157+
$result | Add-Member -MemberType NoteProperty -Name urifragment -Value "$($this.urifragment)/$($result.id)"
153158
return $result
154159
}
155160
return $this.client.web("$($this.urifragment)", "POST", $body)
@@ -169,12 +174,37 @@ class AssistantResource {
169174
# get all the instances and remove it
170175
$this.list() | ForEach-Object {
171176
$this.delete($_.id)
177+
Write-Host "remove the instance: $($_.id)"
172178
}
173179
}
174180
}
175181

182+
class AssistantResourceObject {
183+
AssistantResourceObject([psobject]$data) {
184+
# check all the properties and assign it to the object
185+
$data.PSObject.Properties | ForEach-Object {
186+
$this | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value
187+
}
188+
}
189+
190+
[AssistantResourceObject]update([hashtable]$data) {
191+
$this.client.web($this.urifragment, "PATCH", $data)
192+
return $this
193+
}
194+
}
195+
196+
197+
class FileObject:AssistantResourceObject {
198+
FileObject([psobject]$data):base($data) {}
199+
[AssistantResourceObject]update([hashtable]$data) {
200+
Write-Host "You can't update the file object."
201+
return $this
202+
}
203+
204+
}
205+
176206
class File:AssistantResource {
177-
File([OpenAIClient]$client): base($client, "files", $null) {}
207+
File([OpenAIClient]$client): base($client, "files", "FileObject") {}
178208

179209
[psobject]create([hashtable]$body) {
180210
if ($body.files) {
@@ -187,7 +217,12 @@ class File:AssistantResource {
187217

188218

189219
[System.Management.Automation.HiddenAttribute()]
190-
[psobject]upload([string[]]$fullname) {
220+
[FileObject[]]upload([string[]]$fullname) {
221+
222+
$PSVersion = Get-Variable -Name PSVersionTable -ValueOnly
223+
if ($PSVersion.PSVersion.Major -lt 6) {
224+
throw "The upload file feature is only supported in PowerShell 6 or later."
225+
}
191226

192227
# process the input, if it is a wildcard or a folder, then get all the files based on this pattern
193228
$fullname = $fullname | Get-ChildItem | Select-Object -ExpandProperty FullName
@@ -197,14 +232,15 @@ class File:AssistantResource {
197232
$result = @(
198233
$existing_files | Where-Object {
199234
$_.hash -in $localfiles.hash
235+
} | ForEach-Object {
236+
[FileObject]::new($_)
200237
}
201238
)
202239

203240
$fullname = $localfiles | Where-Object {
204241
$_.hash -notin $existing_files.hash
205242
} | Select-Object -ExpandProperty fullname
206243

207-
208244
if ($fullname.Count -gt 0) {
209245
# confirm if user want to upload those files to openai
210246
$confirm = Read-Host "Are you sure you want to upload the $($fullname.Count) files? (yes/no)"
@@ -218,74 +254,29 @@ class File:AssistantResource {
218254
$url = "{0}?api-version=2024-05-01-preview" -f $url
219255
}
220256

221-
222257

223258
foreach ($file in $fullname) {
224259
Write-Host "process file: $file"
225-
# Define the purpose (e.g., "assistants", "vision", "batch", or "fine-tune")
226-
$purpose = "assistants"
227-
# Create a new web request
228-
$request = [System.Net.WebRequest]::Create($url)
229-
$request.Method = "POST"
230-
231-
# add the item of headers to request.Headers
232-
$this.client.headers.GetEnumerator() | Where-Object {
233-
$_.Key -ne "Content-Type"
234-
} | ForEach-Object {
235-
$request.Headers.Add($_.Key, $_.Value)
236-
}
237-
238-
# Create a boundary for the multipart/form-data content
239-
$boundary = [System.Guid]::NewGuid().ToString()
240-
241-
# Set the content type and boundary
242-
$request.ContentType = "multipart/form-data; boundary=$boundary"
243-
244260
$name = "{0}-{1}" -f (Get-FileHash $file).Hash, (Split-Path $file -Leaf)
261+
# rename the file to the new name
262+
Rename-Item -Path $file -NewName $name
263+
$temppath = Join-Path -Path (Split-Path $file) -ChildPath $name
264+
try{
265+
$form = @{
266+
file = Get-Item -Path $temppath
267+
purpose = "assistants"
268+
}
245269

246-
# Create the request body
247-
$body = @"
248-
--$boundary
249-
Content-Disposition: form-data; name="file"; filename="$name"
250-
Content-Type: application/octet-stream
251-
252-
$(Get-Content -Path $file)
253-
254-
--$boundary
255-
Content-Disposition: form-data; name="purpose"
256-
257-
$purpose
258-
--$boundary--
259-
"@
260-
261-
262-
# Convert the body to bytes
263-
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($body)
264-
265-
# Set the content length
266-
$request.ContentLength = $bodyBytes.Length
267-
268-
# Get the request stream and write the body
269-
$requestStream = $request.GetRequestStream()
270-
$requestStream.Write($bodyBytes, 0, $bodyBytes.Length)
271-
$requestStream.Close()
272-
273-
# Get the response
274-
$response = $request.GetResponse()
275-
276-
# Read the response content
277-
$responseStream = $response.GetResponseStream()
278-
$reader = [System.IO.StreamReader]::new($responseStream)
279-
$responseContent = $reader.ReadToEnd()
280-
$reader.Close()
281-
282-
# Print the response content
283-
$result += ($responseContent | ConvertFrom-Json)
270+
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $this.client.headers -Form $form
271+
$result += [FileObject]::new($response)
272+
}
273+
finally{
274+
# rename the file back to the original name
275+
Rename-Item -Path $temppath -NewName (Split-Path $file -Leaf)
276+
}
284277
}
285278
}
286279

287-
288-
289280
return $result
290281
}
291282

@@ -375,14 +366,6 @@ class Assistant:AssistantResource {
375366
}
376367

377368

378-
class AssistantResourceObject {
379-
AssistantResourceObject([psobject]$data) {
380-
# check all the properties and assign it to the object
381-
$data.PSObject.Properties | ForEach-Object {
382-
$this | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value
383-
}
384-
}
385-
}
386369

387370
class AssistantObject:AssistantResourceObject {
388371
[ThreadObject]$thread

0 commit comments

Comments
 (0)