Skip to content

Commit 12de322

Browse files
authored
assistant 支持中文,unicode (#260)
Fixes #251
2 parents 57d254e + 8bd0fb9 commit 12de322

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

code365scripts.openai/Types/OpenAIClient.ps1

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ class OpenAIClient {
2727
[void]init() {
2828
# check the apikey, endpoint and model, if empty, then return error
2929
$this.headers = @{
30-
"Content-Type" = "application/json"
31-
"OpenAI-Beta" = "assistants=v2"
30+
"OpenAI-Beta" = "assistants=v2"
3231
}
3332

3433
if ($this.baseUri -match "azure") {
@@ -61,10 +60,44 @@ class OpenAIClient {
6160
}
6261

6362
if ($method -eq "GET" -or $null -eq $body) {
64-
return Invoke-RestMethod -Method $method -Uri $url -Headers $this.headers
63+
$params = @{
64+
Method = $method
65+
Uri = $url
66+
Headers = $this.headers
67+
}
68+
return $this.unicodeiwr($params)
69+
}
70+
else {
71+
72+
$params = @{
73+
Method = $method
74+
Uri = $url
75+
Headers = $this.headers
76+
Body = ($body | ConvertTo-Json -Depth 10)
77+
}
78+
return $this.unicodeiwr($params)
79+
}
80+
}
81+
82+
[System.Management.Automation.HiddenAttribute()]
83+
[psobject]unicodeiwr([hashtable]$params) {
84+
$oldProgressPreference = Get-Variable -Name ProgressPreference -ValueOnly
85+
Set-Variable -Name ProgressPreference -Value "SilentlyContinue" -Scope Script -Force
86+
$response = Invoke-WebRequest @params -ContentType "application/json;charset=utf-8"
87+
Set-Variable -Name ProgressPreference -Value $oldProgressPreference -Scope Script -Force
88+
89+
$contentType = $response.Headers["Content-Type"]
90+
$version = Get-Variable -Name PSVersionTable -ValueOnly
91+
if ($version.PSVersion.Major -gt 5 -or $contentType -match 'charset=utf-8') {
92+
return $response.Content | ConvertFrom-Json
6593
}
6694
else {
67-
return Invoke-RestMethod -Method $method -Uri $url -Headers $this.headers -Body ($body | ConvertTo-Json -Depth 10)
95+
$response = $response.Content
96+
$charset = if ($contentType -match "charset=([^;]+)") { $matches[1] } else { "ISO-8859-1" }
97+
$dstEncoding = [System.Text.Encoding]::GetEncoding($charset)
98+
$srcEncoding = [System.Text.Encoding]::UTF8
99+
$result = $srcEncoding.GetString([System.Text.Encoding]::Convert($srcEncoding, $dstEncoding, $srcEncoding.GetBytes($response)))
100+
return $result | ConvertFrom-Json
68101
}
69102
}
70103

@@ -168,7 +201,7 @@ class File:AssistantResource {
168201
)
169202

170203
$fullname = $localfiles | Where-Object {
171-
$_.hash -notin $result.hash
204+
$_.hash -notin $existing_files.hash
172205
} | Select-Object -ExpandProperty fullname
173206

174207

@@ -356,7 +389,7 @@ class AssistantObject:AssistantResourceObject {
356389

357390
AssistantObject([psobject]$data):base($data) {}
358391

359-
[void]chat($clean = $false) {
392+
[void]chat([bool]$clean = $false) {
360393
if (-not $this.thread) {
361394
# create a thread, and associate the assistant id
362395
$this.thread = $this.client.threads.create($this.id)
@@ -461,7 +494,7 @@ class ThreadObject:AssistantResourceObject {
461494

462495
}
463496

464-
Start-Sleep -Seconds 1
497+
Start-Sleep -Milliseconds 500
465498
$run = [AssistantResource]::new($this.client, ("threads/{0}/runs" -f $this.id), $null ).get($this.last_run_id)
466499
}
467500

0 commit comments

Comments
 (0)