Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

| Date | Version | Description |
|------------|----------|-----------------------------------------------------------------------------|
| 2024-05-11 | v3.0.0.4 | Fixed encoding bug for PowerShell 5.1 [#206](https://github.com/chenxizhang/openai-powershell/issues/206) |
| 2024-05-05 | v3.0.0.3 | Update the help content (both English and Chinese),and minor bug fixes |
| 2024-05-05 | v3.0.0.2 | Update the help content (both English and Chinese) |
| 2024-05-04 | v3.0.0.1 | Added function support [#172](https://github.com/chenxizhang/openai-powershell/issues/172) |
Expand Down
26 changes: 26 additions & 0 deletions code365scripts.openai/Private/Invoke-UniWebRequest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function Invoke-UniWebRequest {
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[hashtable]$params
)

$response = Invoke-WebRequest @params -ContentType "application/json;charset=utf-8"

if (($PSVersionTable['PSVersion'].Major -ge 6) -or ($response.Headers['Content-Type'] -match 'charset=utf-8')) {
return $response.Content | ConvertFrom-Json
}
else {
$response = $response.Content | ConvertFrom-Json
$result = $response.choices[0].message.content
if ($null -eq $result) {
return $response
}
else {
$dstEncoding = [System.Text.Encoding]::GetEncoding('iso-8859-1')
$srcEncoding = [System.Text.Encoding]::UTF8
$result = $srcEncoding.GetString([System.Text.Encoding]::Convert($srcEncoding, $dstEncoding, $srcEncoding.GetBytes($result)))
$response.choices[0].message.content = $result
return $response
}
}
}
34 changes: 6 additions & 28 deletions code365scripts.openai/Public/New-ChatGPTConversation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function New-ChatGPTConversation {
}

# if use local model, and api_key is not specify, then generate a random key
if ($endpoint -eq "http://localhost:11434/v1/chat/completions" -and !$api_key){
if ($endpoint -eq "http://localhost:11434/v1/chat/completions" -and !$api_key) {
$api_key = "local"
}

Expand Down Expand Up @@ -248,7 +248,6 @@ function New-ChatGPTConversation {
Uri = $endpoint
Method = "POST"
Headers = $header
ContentType = "application/json;charset=utf-8"
}

if ($json) {
Expand All @@ -263,7 +262,7 @@ function New-ChatGPTConversation {

Write-Verbose ($resources.verbose_prepare_params -f ($params | ConvertTo-Json -Depth 10))

$response = Invoke-RestMethod @params
$response = Invoke-UniWebRequest $params

# if return the tool_calls, then execute the tool locally and add send the message again.

Expand Down Expand Up @@ -291,20 +290,9 @@ function New-ChatGPTConversation {
}

$params.Body = ($body | ConvertTo-Json -Depth 10)
$response = Invoke-RestMethod @params
$response = Invoke-UniWebRequest $params
}

if ($PSVersionTable['PSVersion'].Major -eq 5) {
Write-Verbose ($resources.verbose_powershell_5_utf8)

$dstEncoding = [System.Text.Encoding]::GetEncoding('iso-8859-1')
$srcEncoding = [System.Text.Encoding]::UTF8

$response.choices | ForEach-Object {
$_.message.content = $srcEncoding.GetString([System.Text.Encoding]::Convert($srcEncoding, $dstEncoding, $srcEncoding.GetBytes($_.message.content)))
}

}
Write-Verbose ($resources.verbose_response_utf8 -f ($response | ConvertTo-Json -Depth 10))

$result = $response.choices[0].message.content
Expand Down Expand Up @@ -417,7 +405,6 @@ function New-ChatGPTConversation {
Uri = $endpoint
Method = "POST"
Headers = $header
ContentType = "application/json;charset=utf-8"
}

if ($json) {
Expand Down Expand Up @@ -446,7 +433,7 @@ function New-ChatGPTConversation {
$request.Headers.Clear()
$request.Content = [System.Net.Http.StringContent]::new(($body), [System.Text.Encoding]::UTF8)
$request.Content.Headers.Clear()
$request.Content.Headers.Add("Content-Type", "application/json;charset=utf-8")
$request.Content.Headers.Add("Content-Type", "application/json;chatset=utf-8")

foreach ($k in $header.Keys) {
$request.Headers.Add($k, $header[$k])
Expand Down Expand Up @@ -486,7 +473,7 @@ function New-ChatGPTConversation {
else {

Write-Verbose ($resources.verbose_chat_not_stream_mode)
$response = Invoke-RestMethod @params
$response = Invoke-UniWebRequest $params
Write-Verbose ($resources.verbose_chat_response_received -f ($response | ConvertTo-Json -Depth 10))

# TODO #175 将工具作为外部模块加载,而不是直接调用
Expand Down Expand Up @@ -514,19 +501,10 @@ function New-ChatGPTConversation {
}

$params.Body = ($body | ConvertTo-Json -Depth 10)
$response = Invoke-RestMethod @params
$response = Invoke-UniWebRequest $params
}

$result = $response.choices[0].message.content

if ($PSVersionTable['PSVersion'].Major -le 5) {
Write-Verbose ($resources.verbose_powershell_5_utf8)
$dstEncoding = [System.Text.Encoding]::GetEncoding('iso-8859-1')
$srcEncoding = [System.Text.Encoding]::UTF8
$result = $srcEncoding.GetString([System.Text.Encoding]::Convert($srcEncoding, $dstEncoding, $srcEncoding.GetBytes($result)))
Write-Verbose ($resouces.verbose_response_utf8 -f $result)
}

$messages += [PSCustomObject]@{
role = "assistant"
content = $result
Expand Down
2 changes: 1 addition & 1 deletion code365scripts.openai/code365scripts.openai.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '.\code365scripts.openai.psm1'

# Version number of this module.
ModuleVersion = '3.0.0.3'
ModuleVersion = '3.0.0.4'

# Supported PSEditions
CompatiblePSEditions = @("Desktop")
Expand Down