From 22f6b5b700e105b54d9fae72868c84e60d112873 Mon Sep 17 00:00:00 2001 From: chenxizhang Date: Mon, 17 Jun 2024 18:22:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E7=9A=84ve?= =?UTF-8?q?ctor=5Fstore=20=E4=B9=9F=E6=94=AF=E6=8C=81=E5=8E=BB=E9=87=8D?= =?UTF-8?q?=EF=BC=8C=E5=8F=AA=E8=A6=81file=5Fids=E6=98=AF=E4=B8=80?= =?UTF-8?q?=E6=A0=B7=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=20Fixes=20#257?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Public/New-ChatGPTConversation.ps1 | 4 +- code365scripts.openai/Types/OpenAIClient.ps1 | 48 ++++++++++++++----- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/code365scripts.openai/Public/New-ChatGPTConversation.ps1 b/code365scripts.openai/Public/New-ChatGPTConversation.ps1 index 96162f4..3cb1acd 100644 --- a/code365scripts.openai/Public/New-ChatGPTConversation.ps1 +++ b/code365scripts.openai/Public/New-ChatGPTConversation.ps1 @@ -303,7 +303,7 @@ function New-ChatGPTConversation { # if the parameter set is assistant_existing, then get the assistant from the existing assistant id if ($PSCmdlet.ParameterSetName -eq "assistant_existing") { $client = Get-OpenAIClient -api_key $api_key -model $model -endpoint $baseUrl - $client.assistants.get($assistant_id).chat() + $client.assistants.get($assistant_id).chat($false) return } @@ -316,7 +316,7 @@ function New-ChatGPTConversation { model = $model instructions = $system functions = $functions - }).chat() + }).chat($true) return } diff --git a/code365scripts.openai/Types/OpenAIClient.ps1 b/code365scripts.openai/Types/OpenAIClient.ps1 index 0863ea4..9ab7106 100644 --- a/code365scripts.openai/Types/OpenAIClient.ps1 +++ b/code365scripts.openai/Types/OpenAIClient.ps1 @@ -280,7 +280,7 @@ class Assistant:AssistantResource { if ($files) { # upload the files and create new vector store - $file_ids = $this.client.files.create(@{ "files" = $files }) | Select-Object -ExpandProperty id + $file_ids = $this.client.files.create(@{ "files" = $files }) | Select-Object -ExpandProperty id $body.Add("tools", @( @{ "type" = "file_search" @@ -294,6 +294,7 @@ class Assistant:AssistantResource { }) } }) + } if ($vector_store_ids -and $vector_store_ids.Count -gt 0) { @@ -355,26 +356,39 @@ class AssistantObject:AssistantResourceObject { AssistantObject([psobject]$data):base($data) {} - [void]chat() { + [void]chat($clean = $false) { if (-not $this.thread) { # create a thread, and associate the assistant id $this.thread = $this.client.threads.create($this.id) } - while ($true) { - # ask use to input, until the user type 'q' or 'bye' - $prompt = Read-Host ">" - if ($prompt -eq "q" -or $prompt -eq "bye") { - break - } + try { + while ($true) { + # ask use to input, until the user type 'q' or 'bye' + $prompt = Read-Host ">" + if ($prompt -eq "q" -or $prompt -eq "bye") { + break + } - # send the message to the thread - $response = $this.thread.send($prompt).run().get_last_message() + # send the message to the thread + $response = $this.thread.send($prompt).run().get_last_message() - if ($response) { - Write-Host $response -ForegroundColor Green + if ($response) { + Write-Host $response -ForegroundColor Green + } + } + } + finally { + $this.client.threads.delete($this.thread.id) + if ($clean) { + Write-Host "clean up the thread, assistant, and vector_store..." -ForegroundColor Yellow + # clean up the thread, assistant, and vector_store + $vc_id = $this.tool_resources.file_search.vector_store_ids[0] + $this.client.vector_stores.delete($vc_id) + $this.client.assistants.delete($this.id) } } + } } @@ -476,8 +490,16 @@ class Thread:AssistantResource { } } +class Vector_storeObject:AssistantResourceObject { + Vector_storeObject([psobject]$data):base($data) {} + + [string[]]file_ids() { + return $this.client.web("vector_stores/$($this.id)/files").data | Select-Object -ExpandProperty id + } +} + class Vector_store:AssistantResource { - Vector_store([OpenAIClient]$client): base($client, "vector_stores", $null) {} + Vector_store([OpenAIClient]$client): base($client, "vector_stores", "Vector_storeObject") {} [psobject]create([hashtable]$body) { <#