Skip to content

Commit 22f6b5b

Browse files
committed
自动创建的vector_store 也支持去重,只要file_ids是一样的情况下
Fixes #257
1 parent 5ee80c1 commit 22f6b5b

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

code365scripts.openai/Public/New-ChatGPTConversation.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function New-ChatGPTConversation {
303303
# if the parameter set is assistant_existing, then get the assistant from the existing assistant id
304304
if ($PSCmdlet.ParameterSetName -eq "assistant_existing") {
305305
$client = Get-OpenAIClient -api_key $api_key -model $model -endpoint $baseUrl
306-
$client.assistants.get($assistant_id).chat()
306+
$client.assistants.get($assistant_id).chat($false)
307307
return
308308
}
309309

@@ -316,7 +316,7 @@ function New-ChatGPTConversation {
316316
model = $model
317317
instructions = $system
318318
functions = $functions
319-
}).chat()
319+
}).chat($true)
320320
return
321321
}
322322

code365scripts.openai/Types/OpenAIClient.ps1

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class Assistant:AssistantResource {
280280

281281
if ($files) {
282282
# upload the files and create new vector store
283-
$file_ids = $this.client.files.create(@{ "files" = $files }) | Select-Object -ExpandProperty id
283+
$file_ids = $this.client.files.create(@{ "files" = $files }) | Select-Object -ExpandProperty id
284284
$body.Add("tools", @(
285285
@{
286286
"type" = "file_search"
@@ -294,6 +294,7 @@ class Assistant:AssistantResource {
294294
})
295295
}
296296
})
297+
297298
}
298299

299300
if ($vector_store_ids -and $vector_store_ids.Count -gt 0) {
@@ -355,26 +356,39 @@ class AssistantObject:AssistantResourceObject {
355356

356357
AssistantObject([psobject]$data):base($data) {}
357358

358-
[void]chat() {
359+
[void]chat($clean = $false) {
359360
if (-not $this.thread) {
360361
# create a thread, and associate the assistant id
361362
$this.thread = $this.client.threads.create($this.id)
362363
}
363364

364-
while ($true) {
365-
# ask use to input, until the user type 'q' or 'bye'
366-
$prompt = Read-Host ">"
367-
if ($prompt -eq "q" -or $prompt -eq "bye") {
368-
break
369-
}
365+
try {
366+
while ($true) {
367+
# ask use to input, until the user type 'q' or 'bye'
368+
$prompt = Read-Host ">"
369+
if ($prompt -eq "q" -or $prompt -eq "bye") {
370+
break
371+
}
370372

371-
# send the message to the thread
372-
$response = $this.thread.send($prompt).run().get_last_message()
373+
# send the message to the thread
374+
$response = $this.thread.send($prompt).run().get_last_message()
373375

374-
if ($response) {
375-
Write-Host $response -ForegroundColor Green
376+
if ($response) {
377+
Write-Host $response -ForegroundColor Green
378+
}
379+
}
380+
}
381+
finally {
382+
$this.client.threads.delete($this.thread.id)
383+
if ($clean) {
384+
Write-Host "clean up the thread, assistant, and vector_store..." -ForegroundColor Yellow
385+
# clean up the thread, assistant, and vector_store
386+
$vc_id = $this.tool_resources.file_search.vector_store_ids[0]
387+
$this.client.vector_stores.delete($vc_id)
388+
$this.client.assistants.delete($this.id)
376389
}
377390
}
391+
378392
}
379393
}
380394

@@ -476,8 +490,16 @@ class Thread:AssistantResource {
476490
}
477491
}
478492

493+
class Vector_storeObject:AssistantResourceObject {
494+
Vector_storeObject([psobject]$data):base($data) {}
495+
496+
[string[]]file_ids() {
497+
return $this.client.web("vector_stores/$($this.id)/files").data | Select-Object -ExpandProperty id
498+
}
499+
}
500+
479501
class Vector_store:AssistantResource {
480-
Vector_store([OpenAIClient]$client): base($client, "vector_stores", $null) {}
502+
Vector_store([OpenAIClient]$client): base($client, "vector_stores", "Vector_storeObject") {}
481503

482504
[psobject]create([hashtable]$body) {
483505
<#

0 commit comments

Comments
 (0)