Skip to content

Commit 1289f0e

Browse files
committed
chat和gpt在处理endpoint上规则不一致,导致歧义
Fixes #279
1 parent 6dbe6f0 commit 1289f0e

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
| Date | Version | Description |
55
|------------|----------|-----------------------------------------------------------------------------|
6+
| 2025-01-05 | v4.0.0.6 | Bug fixes! [#279](https://github.com/chenxizhang/openai-powershell/issues/279) |
67
| 2024-09-02 | v4.0.0.5 | Bug fixes! [#275](https://github.com/chenxizhang/openai-powershell/issues/275) |
78
| 2024-09-01 | v4.0.0.4 | Bug fixes! [#271](https://github.com/chenxizhang/openai-powershell/issues/271) [#272](https://github.com/chenxizhang/openai-powershell/issues/272) |
89
| 2024-08-07 | v4.0.0.3 | Bug fixes! [#265](https://github.com/chenxizhang/openai-powershell/issues/265) |

code365scripts.openai/Public/New-ChatCompletions.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function New-ChatCompletions {
132132
Confirm-DependencyModule -ModuleName "MSAL.ps"
133133

134134
$aad = $parsed_env_config.auth.aad
135-
if($aad.clientsecret){
135+
if ($aad.clientsecret) {
136136
$aad.clientsecret = ConvertTo-SecureString $aad.clientsecret -AsPlainText -Force
137137
}
138138
$accesstoken = (Get-MsalToken @aad).AccessToken
@@ -153,17 +153,17 @@ function New-ChatCompletions {
153153

154154
$api_key = ($api_key, [System.Environment]::GetEnvironmentVariable("OPENAI_API_KEY") | Where-Object { $_.Length -gt 0 } | Select-Object -First 1)
155155
$model = ($model, [System.Environment]::GetEnvironmentVariable("OPENAI_API_MODEL"), "gpt-3.5-turbo" | Where-Object { $_.Length -gt 0 } | Select-Object -First 1)
156-
$endpoint = ($endpoint, [System.Environment]::GetEnvironmentVariable("OPENAI_API_ENDPOINT"), "https://api.openai.com/v1/chat/completions" | Where-Object { $_.Length -gt 0 } | Select-Object -First 1)
156+
$endpoint = ($endpoint, [System.Environment]::GetEnvironmentVariable("OPENAI_API_ENDPOINT"), "https://api.openai.com/v1/" | Where-Object { $_.Length -gt 0 } | Select-Object -First 1)
157157

158158
$endpoint = switch ($endpoint) {
159-
{ $_ -in ("ollama", "local") } { "http://localhost:11434/v1/chat/completions" }
160-
"kimi" { "https://api.moonshot.cn/v1/chat/completions" }
161-
"zhipu" { "https://open.bigmodel.cn/api/paas/v4/chat/completions" }
159+
{ $_ -in ("ollama", "local") } { "http://localhost:11434/v1/" }
160+
"kimi" { "https://api.moonshot.cn/v1/" }
161+
"zhipu" { "https://open.bigmodel.cn/api/paas/v4/" }
162162
default { $endpoint }
163163
}
164164

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

@@ -191,10 +191,18 @@ function New-ChatCompletions {
191191
}
192192

193193
# if endpoint contains ".openai.azure.com", then people wants to use azure openai service, try to concat the endpoint with the model
194+
195+
if(-not $endpoint.EndsWith("/")) {
196+
$endpoint += "/"
197+
}
198+
194199
if ($endpoint.EndsWith("openai.azure.com/")) {
195200
$version = Get-AzureAPIVersion
196201
$endpoint += "openai/deployments/$model/chat/completions?api-version=$version"
197202
}
203+
else {
204+
$endpoint += "chat/completions"
205+
}
198206

199207
# add databricks support, it will use the basic authorization method, not the bearer token
200208
$azure = $endpoint.Contains("openai.azure.com")
@@ -348,11 +356,11 @@ function New-ChatCompletions {
348356
Write-Verbose ($resources.verbose_outfile_specified -f $outFile)
349357
$result | Out-File -FilePath $outFile -Encoding utf8
350358

351-
if($passthru){
359+
if ($passthru) {
352360
Write-Output $result
353361
}
354362
}
355-
else{
363+
else {
356364
# support passthru, even though user specify the outfile, we still return the result to the pipeline
357365
Write-Output $result
358366
}

code365scripts.openai/Public/New-ChatGPTConversation.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,17 @@ function New-ChatGPTConversation {
242242
}
243243

244244
$baseUrl = $endpoint
245-
$chatEndpoint = $baseUrl + "chat/completions"
246-
245+
if(-not $baseUrl.EndsWith("/")) {
246+
$baseUrl = $baseUrl + "/"
247+
}
247248
# if endpoint contains ".openai.azure.com", then people wants to use azure openai service, try to concat the endpoint with the model
248249
if ($baseUrl.EndsWith("openai.azure.com/")) {
249250
$version = Get-AzureAPIVersion
250251
$chatEndpoint = "$($baseUrl)openai/deployments/$model/chat/completions?api-version=$version"
251252
}
253+
else{
254+
$chatEndpoint = $baseUrl + "chat/completions"
255+
}
252256

253257
# add databricks support, it will use the basic authorization method, not the bearer token
254258
$azure = $chatEndpoint.Contains("openai.azure.com")

code365scripts.openai/code365scripts.openai.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = '.\code365scripts.openai.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '4.0.0.5'
15+
ModuleVersion = '4.0.0.6'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = @("Desktop", "Core")

0 commit comments

Comments
 (0)