mkht / PSOpenAI

PowerShell module for OpenAI API.
MIT License
35 stars 10 forks source link

Add-ThreadMessage - Wait for run complete #15

Closed potatoqualitee closed 4 months ago

potatoqualitee commented 4 months ago

To handle this error I encountered when reusing threads

OpenAI API returned an 400 (BadRequest) Error: Cant add messages to
thread_abc while a run run_xyz is active.
At C:\github\psopenai\Public\Threads\Add-ThreadMessage.ps1:149 char:21
+         $Response = Invoke-OpenAIAPIRequest @params

Edit: need to fix to account for more

image

k, done!

mkht commented 4 months ago

What situation does this error occur? Can you indicate the steps to reproduce it?

potatoqualitee commented 4 months ago

i cache my threads to make things go faster and keep track of convos. this happens when a run uhhh i dunno, maybe takes too long? but ever since adding this it hasnt happened.

https://github.com/potatoqualitee/dbatools.ai/blob/main/public/Invoke-DbaiQuery.ps1#L73

So just talk a bit too fast on this thread, I suppose.

mkht commented 4 months ago

Implicitly waiting for or canceling a Run may not be the user's intention. Sometimes it is better to output an error that is easy to understand than to try to address all errors in the code. Users can write their own error handling in their scripts.

If this is to be incorporated, it would be better to add an optional switch such as -WaitForRunComplete to wait for Run to complete only when the user wants it to. Also, if Run does not complete within the time specified by Timeout, it should return a timeout error rather than canceling the Run.

It is better for the code that waits for Run to complete to call the already existing Wait-ThreadRun function rather than implementing its own loop.

potatoqualitee commented 4 months ago

Thank you. How's that?

potatoqualitee commented 4 months ago

Thanks! Btw, I accepted Copilots suggestion for Write-Host by accident. Changed it to Verbose, which is my preference. if you prefer Write-Host, let me know.

mkht commented 4 months ago

Thanks! Btw, I accepted Copilots suggestion for Write-Host by accident. Changed it to Verbose, which is my preference. if you prefer Write-Host, let me know.

I like Verbose better too. 😊

mkht commented 4 months ago

Please give me a day or two before merging as I want to do some testing.

potatoqualitee commented 4 months ago

No problem, I keep my branch set to this one so it does not interfere with my testing.

mkht commented 4 months ago

LGTM 👍