Skip to content

Commit 9313639

Browse files
committed
文件去重
Fixes #253
1 parent 83939ff commit 9313639

File tree

1 file changed

+69
-50
lines changed

1 file changed

+69
-50
lines changed

code365scripts.openai/Types/OpenAIClient.ps1

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -158,46 +158,60 @@ class File:AssistantResource {
158158

159159
# process the input, if it is a wildcard or a folder, then get all the files based on this pattern
160160
$fullname = $fullname | Get-ChildItem | Select-Object -ExpandProperty FullName
161+
# read all the files and check the filename, compute
162+
$existing_files = $this.list() | Select-Object id, @{l = "hash"; e = { $_.filename.split("-")[0] } }
163+
$localfiles = $fullname | Select-Object @{l = "fullname"; e = { $_ } }, @{l = "hash"; e = { (Get-FileHash $_).Hash } }
164+
$result = @(
165+
$existing_files | Where-Object {
166+
$_.hash -in $localfiles.hash
167+
}
168+
)
161169

162-
# confirm if user want to upload those files to openai
163-
$confirm = Read-Host "Are you sure you want to upload the $($fullname.Count) files? (yes/no)"
164-
if ($confirm -ne "yes" -and $confirm -ne "y") {
165-
throw "The user canceled the operation."
166-
}
170+
$fullname = $localfiles | Where-Object {
171+
$_.hash -notin $result.hash
172+
} | Select-Object -ExpandProperty fullname
167173

168174

169-
$url = "{0}{1}" -f $this.client.baseUri, $this.urifragment
170-
if ($this.client.baseUri -match "azure") {
171-
$url = "{0}?api-version=2024-05-01-preview" -f $url
172-
}
175+
if ($fullname.Count -gt 0) {
176+
# confirm if user want to upload those files to openai
177+
$confirm = Read-Host "Are you sure you want to upload the $($fullname.Count) files? (yes/no)"
178+
if ($confirm -ne "yes" -and $confirm -ne "y") {
179+
throw "The user canceled the operation."
180+
}
181+
173182

174-
$result = @()
175-
176-
foreach ($file in $fullname) {
177-
Write-Host "process file: $file"
178-
# Define the purpose (e.g., "assistants", "vision", "batch", or "fine-tune")
179-
$purpose = "assistants"
180-
# Create a new web request
181-
$request = [System.Net.WebRequest]::Create($url)
182-
$request.Method = "POST"
183-
184-
# add the item of headers to request.Headers
185-
$this.client.headers.GetEnumerator() | Where-Object {
186-
$_.Key -ne "Content-Type"
187-
} | ForEach-Object {
188-
$request.Headers.Add($_.Key, $_.Value)
183+
$url = "{0}{1}" -f $this.client.baseUri, $this.urifragment
184+
if ($this.client.baseUri -match "azure") {
185+
$url = "{0}?api-version=2024-05-01-preview" -f $url
189186
}
190187

191-
# Create a boundary for the multipart/form-data content
192-
$boundary = [System.Guid]::NewGuid().ToString()
188+
193189

194-
# Set the content type and boundary
195-
$request.ContentType = "multipart/form-data; boundary=$boundary"
190+
foreach ($file in $fullname) {
191+
Write-Host "process file: $file"
192+
# Define the purpose (e.g., "assistants", "vision", "batch", or "fine-tune")
193+
$purpose = "assistants"
194+
# Create a new web request
195+
$request = [System.Net.WebRequest]::Create($url)
196+
$request.Method = "POST"
197+
198+
# add the item of headers to request.Headers
199+
$this.client.headers.GetEnumerator() | Where-Object {
200+
$_.Key -ne "Content-Type"
201+
} | ForEach-Object {
202+
$request.Headers.Add($_.Key, $_.Value)
203+
}
196204

197-
$name = "{0}-{1}" -f (Get-FileHash $file).Hash, (Split-Path $file -Leaf)
205+
# Create a boundary for the multipart/form-data content
206+
$boundary = [System.Guid]::NewGuid().ToString()
198207

199-
# Create the request body
200-
$body = @"
208+
# Set the content type and boundary
209+
$request.ContentType = "multipart/form-data; boundary=$boundary"
210+
211+
$name = "{0}-{1}" -f (Get-FileHash $file).Hash, (Split-Path $file -Leaf)
212+
213+
# Create the request body
214+
$body = @"
201215
--$boundary
202216
Content-Disposition: form-data; name="file"; filename="$name"
203217
Content-Type: application/octet-stream
@@ -212,29 +226,33 @@ $purpose
212226
"@
213227

214228

215-
# Convert the body to bytes
216-
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($body)
229+
# Convert the body to bytes
230+
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($body)
217231

218-
# Set the content length
219-
$request.ContentLength = $bodyBytes.Length
232+
# Set the content length
233+
$request.ContentLength = $bodyBytes.Length
220234

221-
# Get the request stream and write the body
222-
$requestStream = $request.GetRequestStream()
223-
$requestStream.Write($bodyBytes, 0, $bodyBytes.Length)
224-
$requestStream.Close()
235+
# Get the request stream and write the body
236+
$requestStream = $request.GetRequestStream()
237+
$requestStream.Write($bodyBytes, 0, $bodyBytes.Length)
238+
$requestStream.Close()
225239

226-
# Get the response
227-
$response = $request.GetResponse()
240+
# Get the response
241+
$response = $request.GetResponse()
228242

229-
# Read the response content
230-
$responseStream = $response.GetResponseStream()
231-
$reader = [System.IO.StreamReader]::new($responseStream)
232-
$responseContent = $reader.ReadToEnd()
233-
$reader.Close()
243+
# Read the response content
244+
$responseStream = $response.GetResponseStream()
245+
$reader = [System.IO.StreamReader]::new($responseStream)
246+
$responseContent = $reader.ReadToEnd()
247+
$reader.Close()
234248

235-
# Print the response content
236-
$result += ($responseContent | ConvertFrom-Json)
249+
# Print the response content
250+
$result += ($responseContent | ConvertFrom-Json)
251+
}
237252
}
253+
254+
255+
238256
return $result
239257
}
240258

@@ -366,10 +384,11 @@ class ThreadObject:AssistantResourceObject {
366384

367385
[ThreadObject]send([string]$message) {
368386
# send a message
369-
$obj = [AssistantResource]::new($this.client, ("threads/{0}/messages" -f $this.id), $null ).create(@{
387+
[AssistantResource]::new($this.client, ("threads/{0}/messages" -f $this.id), $null ).create(@{
370388
role = "user"
371389
content = $message
372-
})
390+
}) | Out-Null
391+
373392
return $this
374393
}
375394

0 commit comments

Comments
 (0)