Skip to content

Commit 3d77c05

Browse files
committed
如何启用了函数,则get stream response总是会报错
Fixes #272
1 parent 01dab54 commit 3d77c05

File tree

1 file changed

+65
-71
lines changed

1 file changed

+65
-71
lines changed

code365scripts.openai/Public/New-ChatGPTConversation.ps1

+65-71
Original file line numberDiff line numberDiff line change
@@ -548,18 +548,25 @@ function New-ChatGPTConversation {
548548
Write-Verbose ("FCFR - {0} ms " -f ($stopwatch.ElapsedMilliseconds))
549549
$stopwatch.Stop()
550550

551-
552-
# otherwise, get the read of the result
553-
$reader = $callapi.reader
554-
# check if tools_call is null, if not, then execute the tools_call
555-
$line = $reader.ReadLine()
556-
$delta = ($line -replace "data: ", "" | ConvertFrom-Json).choices.delta
551+
while ($true) {
552+
# otherwise, get the read of the result
553+
$reader = $callapi.reader
554+
# check if tools_call is null, if not, then execute the tools_call
555+
$line = $reader.ReadLine()
556+
Write-Verbose "first line: $line"
557+
$tool_calls = @()
558+
$result = ""
559+
$firstChunk = $true
557560

561+
while ($line -ne "data: [DONE]") {
562+
# read the stream response line by line
563+
$delta = ($line -replace "data: ", "" | ConvertFrom-Json).choices.delta
558564

559-
while ($delta -and ($null -eq $delta.content)) {
560-
$tool_calls = @()
565+
if (-not $delta) {
566+
$line = $reader.ReadLine()
567+
continue
568+
}
561569

562-
while ($true) {
563570
if ($delta.tool_calls) {
564571
$temp = $delta.tool_calls
565572
if ($temp.id -and $temp.function) {
@@ -581,76 +588,65 @@ function New-ChatGPTConversation {
581588
}
582589
}
583590

584-
$line = $reader.ReadLine()
585-
if ($line -eq "data: [DONE]") { break }
586-
$delta = ($line -replace "data: ", "" | ConvertFrom-Json).choices.delta
587-
}
588591

589-
# execute functions
590-
$messages += [pscustomobject]@{
591-
role = "assistant"
592-
content = ""
593-
tool_calls = @($tool_calls)
594-
}
595-
596-
foreach ($tool in $tool_calls) {
597-
Write-Host ("`r$($resources.function_call): $($tool.function.name)" + (" " * 50)) -NoNewline
598-
$function_args = $tool.function.arguments | ConvertFrom-Json
599-
$tool_response = Invoke-Expression ("{0} {1}" -f $tool.function.name, (
600-
$function_args.PSObject.Properties | ForEach-Object {
601-
"-{0} {1}" -f $_.Name, $_.Value
602-
}
603-
) -join " ")
592+
if ($delta.content) {
593+
# return the content directly
594+
if ($firstChunk) {
595+
$firstChunk = $false
596+
Write-Host ("`r" + (" " * 50)) -ForegroundColor Green -NoNewline
597+
Write-Host "`r[$current] $result" -ForegroundColor Green -NoNewline
598+
}
604599

605-
$messages += @{
606-
role = "tool"
607-
name = $tool.function.name
608-
tool_call_id = $tool.id
609-
content = $tool_response
610-
}
611-
}
612600

613-
$body.messages = $messages
614-
$params.Body = ($body | ConvertTo-Json -Depth 10)
615-
$callapi = Invoke-StreamWebRequest -uri $params.Uri -body $params.Body -header $header
601+
$chunk = $delta.content
602+
Write-Host $chunk -NoNewline -ForegroundColor Green
603+
$result += $chunk
604+
}
616605

617-
if ($callapi.status -ne "ok") {
618-
Write-Host "`r[$current] $($callapi.message)" -NoNewline -ForegroundColor Red
619-
Write-Host ""
620-
break
606+
$line = $reader.ReadLine()
621607
}
622608

623-
$reader = $callapi.reader
624-
$line = $reader.ReadLine()
625-
$delta = ($line -replace "data: ", "" | ConvertFrom-Json).choices.delta
626-
}
627609

628-
# if the callapi status is not ok, then write the error message to host and continue
629-
if ($callapi.status -ne "ok") {
630-
continue
631-
}
610+
if ($tool_calls -and $tool_calls.Count -gt 0) {
611+
# execute functions
612+
$messages += [pscustomobject]@{
613+
role = "assistant"
614+
content = ""
615+
tool_calls = @($tool_calls)
616+
}
617+
618+
foreach ($tool in $tool_calls) {
619+
Write-Host ("`r$($resources.function_call): $($tool.function.name)" + (" " * 50)) -NoNewline
620+
$function_args = $tool.function.arguments | ConvertFrom-Json
621+
$tool_response = Invoke-Expression ("{0} {1}" -f $tool.function.name, (
622+
$function_args.PSObject.Properties | ForEach-Object {
623+
"-{0} {1}" -f $_.Name, $_.Value
624+
}
625+
) -join " ")
626+
627+
628+
$messages += @{
629+
role = "tool"
630+
name = $tool.function.name
631+
tool_call_id = $tool.id
632+
content = $tool_response
633+
}
634+
}
632635

633-
Write-Host ("`r" + (" " * 50)) -ForegroundColor Green -NoNewline
634-
Write-Host "`r[$current] " -NoNewline -ForegroundColor Red
635-
$result = $delta.content
636-
Write-Host $result -NoNewline -ForegroundColor Green
637-
while ($true) {
638-
$line = $reader.ReadLine()
639-
if ($line -eq "data: [DONE]") { break }
640-
$chunk = ($line -replace "data: ", "" | ConvertFrom-Json).choices.delta.content
641-
Write-Host $chunk -NoNewline -ForegroundColor Green
642-
$result += $chunk
643-
}
636+
$body.messages = $messages
637+
$params.Body = ($body | ConvertTo-Json -Depth 10)
638+
$callapi = Invoke-StreamWebRequest -uri $params.Uri -body $params.Body -header $header
639+
}
644640

645-
Write-Host ""
646-
$messages += [PSCustomObject]@{
647-
role = "assistant"
648-
content = $result
641+
if ($result) {
642+
$messages += [PSCustomObject]@{
643+
role = "assistant"
644+
content = $result
645+
}
646+
Write-Host ""
647+
break
648+
}
649649
}
650-
651-
652-
653-
Write-Verbose ($resources.verbose_chat_message_combined -f ($messages | ConvertTo-Json -Depth 10))
654650

655651
}
656652
else {
@@ -659,8 +655,6 @@ function New-ChatGPTConversation {
659655
$response = Invoke-UniWebRequest $params
660656

661657
Write-Verbose ($resources.verbose_chat_response_received -f ($response | ConvertTo-Json -Depth 10))
662-
663-
# TODO #175 将工具作为外部模块加载,而不是直接调用
664658
while ($response.choices -and $response.choices[0].message.tool_calls) {
665659
# add the assistant message
666660
$this_message = $response.choices[0].message

0 commit comments

Comments
 (0)