Skip to content

Commit

Permalink
Code format
Browse files Browse the repository at this point in the history
  • Loading branch information
mkht committed Apr 21, 2024
1 parent a6c801a commit 017c7ee
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 154 deletions.
6 changes: 3 additions & 3 deletions PSOpenAI.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ $PrivateFunctions = Get-ChildItem -LiteralPath $PrivateDirectory -Recurse -Filte
$PublicFunctions = Get-ChildItem -LiteralPath $PublicDirectory -Recurse -Filter '*.ps1' -File

# Include Private functions
$PrivateFunctions | % {
$PrivateFunctions | ForEach-Object {
. $_.FullName
}

# Include Public functions
$PublicFunctions | % {
$PublicFunctions | ForEach-Object {
. $_.FullName
}

# Export public functions
$ExportFunctions = [string[]]@()
$PublicFunctions | % {
$PublicFunctions | ForEach-Object {
if (Test-Path -LiteralPath "Function:/$($_.BaseName)") {
$ExportFunctions += $_.BaseName
}
Expand Down
1 change: 1 addition & 0 deletions Private/Azure/Get-AzureOpenAIAPIEndpoint.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function Get-AzureOpenAIAPIEndpoint {
[CmdletBinding()]
[OutputType([hashtable])]
param (
[Parameter(Mandatory, Position = 0)]
[string]$EndpointName,
Expand Down
12 changes: 7 additions & 5 deletions Private/ChatCompletionFunction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using namespace System.Management.Automation

function New-ChatCompletionFunctionFromHashTable {
[CmdletBinding()]
[OutputType([ordered])]
param (
[Parameter(Mandatory, Position = 0)]
[ValidatePattern('^[a-zA-Z0-9_-]{1,64}$')]
Expand Down Expand Up @@ -37,6 +38,7 @@ function New-ChatCompletionFunctionFromHashTable {

function New-ChatCompletionFunctionFromPSCommand {
[CmdletBinding()]
[OutputType([ordered])]
param (
[Parameter(Mandatory, Position = 0)]
[ValidateScript({ (Get-Command $_ -ea Ignore) -is [CommandInfo] })]
Expand Down Expand Up @@ -76,22 +78,22 @@ function New-ChatCompletionFunctionFromPSCommand {
$CommandHelp = Get-Help $CommandInfo -ErrorAction Ignore

if ($ParameterSetName) {
$TargetParameterSet = $CommandInfo.ParameterSets | ? { $_.Name -eq $ParameterSetName }
$TargetParameterSet = $CommandInfo.ParameterSets | Where-Object { $_.Name -eq $ParameterSetName }
if (-not $TargetParameterSet) {
Write-Error "$ParameterSetName does not exist."
return
}
}
else {
$TargetParameterSet = $CommandInfo.ParameterSets | ? { $_.IsDefault }
$TargetParameterSet = $CommandInfo.ParameterSets | Where-Object { $_.IsDefault }
if (-not $TargetParameterSet) {
$TargetParameterSet = $CommandInfo.ParameterSets[0]
}
}

$TargetParameters = $TargetParameterSet.Parameters | ? { $_.Name -notin $ExcludeParamNames }
$TargetParameters = $TargetParameterSet.Parameters | Where-Object { $_.Name -notin $ExcludeParamNames }
if ($IncludeParameters.Count -gt 0) {
$TargetParameters = $TargetParameters | ? { $_.Name -in $IncludeParameters }
$TargetParameters = $TargetParameters | Where-Object { $_.Name -in $IncludeParameters }
}

$FunctionDefinition.Add('name', $CommandInfo.Name)
Expand All @@ -116,7 +118,7 @@ function New-ChatCompletionFunctionFromPSCommand {

$propHash = ParseParameterType($param.ParameterType)

$helpmsg = (($CommandHelp.parameters.parameter | ? { $_.name -eq $pName }).description.text -join "`n") -replace "`r", ''
$helpmsg = (($CommandHelp.parameters.parameter | Where-Object { $_.name -eq $pName }).description.text -join "`n") -replace "`r", ''
if ([string]::IsNullOrWhiteSpace($helpmsg)) {
$helpmsg = [string]$param.HelpMessage
}
Expand Down
12 changes: 6 additions & 6 deletions Private/Get-CultureInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ function Get-CultureInfo {
$LanguageName = $LanguageName.Trim()
[cultureinfo]$CultureInfo = [cultureinfo]::GetCultures([System.Globalization.CultureTypes]::AllCultures) |`
Where-Object {
$_.Name -eq $LanguageName `
-or $_.EnglishName -eq $LanguageName `
-or $_.DisplayName -eq $LanguageName `
-or $_.NativeName -eq $LanguageName `
-or $_.TwoLetterISOLanguageName -eq $LanguageName `
-or $_.ThreeLetterISOLanguageName -eq $LanguageName
$_.Name -eq $LanguageName -or
$_.EnglishName -eq $LanguageName -or
$_.DisplayName -eq $LanguageName -or
$_.NativeName -eq $LanguageName -or
$_.TwoLetterISOLanguageName -eq $LanguageName -or
$_.ThreeLetterISOLanguageName -eq $LanguageName
} | Select-Object -First 1

if ($CultureInfo -is [cultureinfo]) {
Expand Down
1 change: 1 addition & 0 deletions Private/Get-OpenAIAPIEndpoint.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function Get-OpenAIAPIEndpoint {
[CmdletBinding()]
[OutputType([hashtable])]
param (
[Parameter(Mandatory, Position = 0)]
[string]$EndpointName,
Expand Down
18 changes: 9 additions & 9 deletions Private/Invoke-OpenAIAPIRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,15 @@ function Invoke-OpenAIAPIRequest {

# Verbose / Debug output
$verboseMessage = "$ServiceName API response: " + ($Response |
Format-List StatusCode,
@{
name = 'processing_ms'
expression = { $_.Headers['openai-processing-ms'] }
},
@{
name = 'request_id'
expression = { $_.Headers['X-Request-Id'] }
} | Out-String).TrimEnd()
Format-List StatusCode,
@{
name = 'processing_ms'
expression = { $_.Headers['openai-processing-ms'] }
},
@{
name = 'request_id'
expression = { $_.Headers['X-Request-Id'] }
} | Out-String).TrimEnd()
Write-Verbose -Message $verboseMessage

# Don't read the whole stream for debug logging unless necessary.
Expand Down
19 changes: 8 additions & 11 deletions Private/Invoke-OpenAIAPIRequestSSE.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,11 @@ function Invoke-OpenAIAPIRequestSSE {
if ($IsDebug) {
$startIdx = $lastIdx = 2
if ($AuthType -eq 'openai') { $startIdx += 4 } # 'org-'
Write-Debug -Message (Get-MaskedString `
('Request parameters: ' + ($RequestMessage | fl `
Method, `
RequestUri, `
@{name = 'Headers'; expression = { $_.Headers.ToString() } } `
| Out-String)).TrimEnd() `
-Target ($ApiKey, $Organization) -First $startIdx -Last $lastIdx -MaxNumberOfAsterisks 45)
$MaskedMessage = Get-MaskedString `
('Request parameters: ' + ($RequestMessage | Format-List Method, RequestUri, @{name = 'Headers'; expression = { $_.Headers.ToString() } } | Out-String)).TrimEnd() `
-Target ($ApiKey, $Organization) -First $startIdx -Last $lastIdx -MaxNumberOfAsterisks 45
Write-Debug -Message $MaskedMessage
$MaskedMessage = $null
}

# Send API Request
Expand Down Expand Up @@ -182,17 +180,16 @@ function Invoke-OpenAIAPIRequestSSE {

# Verbose / Debug output
Write-Verbose -Message ('Received HTTP/{0} response of content type {1}' -f $HttpResponse.Version, $HttpResponse.Content.Headers.ContentType.MediaType)
Write-Verbose -Message ("$ServiceName API response: " + ($HttpResponse | fl `
Write-Verbose -Message ("$ServiceName API response: " + ($HttpResponse | Format-List `
StatusCode, `
@{name = 'processing_ms'; expression = { $_.Headers.GetValues('openai-processing-ms')[0] } }, `
@{name = 'request_id'; expression = { $_.Headers.GetValues('X-Request-Id')[0] } } `
| Out-String)).TrimEnd()
@{name = 'request_id'; expression = { $_.Headers.GetValues('X-Request-Id')[0] } } | Out-String)).TrimEnd()
# Don't read the whole stream for debug logging unless necessary.
if ($IsDebug) {
$startIdx = $lastIdx = 2
if ($AuthType -eq 'openai') { $startIdx += 4 } # 'org-'
Write-Debug -Message (Get-MaskedString `
('API response header: ' + ($HttpResponse.Headers | ft -Hide | Out-String)).TrimEnd() `
('API response header: ' + ($HttpResponse.Headers | Format-Table -Hide | Out-String)).TrimEnd() `
-Target ($ApiKey, $Organization) -First $startIdx -Last $lastIdx -MaxNumberOfAsterisks 45)
}

Expand Down
2 changes: 1 addition & 1 deletion Private/Runs/ParseThreadRunStepObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function ParseThreadRunStepObject {
)

$simplecontent =
if ($InputObject.type -eq 'message_creation') {
if ($InputObject.type -eq 'message_creation') {
if ($msgid = $InputObject.step_details.message_creation.message_id) {
$GetThreadMessageParams = $CommonParams
$GetThreadMessageParams.InputObject = $InputObject
Expand Down
34 changes: 17 additions & 17 deletions Private/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,34 @@ function ObjectToContent {

if (
$null -eq $InputObject -or
$InputObject -eq [System.Management.Automation.Internal.AutomationNull]::Value -or
$InputObject -eq [System.DBNull]::Value -or
$InputObject -eq [NullString]::Value
$InputObject -eq [System.Management.Automation.Internal.AutomationNull]::Value -or
$InputObject -eq [System.DBNull]::Value -or
$InputObject -eq [NullString]::Value

) {
$null
}
elseif (
$InputObject -is [string] -or
$InputObject -is [System.Collections.IDictionary] -or
$InputObject -is [char] -or
$InputObject -is [bool] -or
$InputObject -is [datetime] -or
$InputObject -is [System.DateTimeOffset] -or
$InputObject -is [uri] -or
$InputObject -is [double] -or
$InputObject -is [float] -or
$InputObject -is [decimal] -or
$InputObject -is [double]
$InputObject -is [System.Collections.IDictionary] -or
$InputObject -is [char] -or
$InputObject -is [bool] -or
$InputObject -is [datetime] -or
$InputObject -is [System.DateTimeOffset] -or
$InputObject -is [uri] -or
$InputObject -is [double] -or
$InputObject -is [float] -or
$InputObject -is [decimal] -or
$InputObject -is [double]
) {
$InputObject
}
elseif (
$InputObject -is [timespan] -or
$InputObject -is [guid] -or
$InputObject -is [regex] -or
$InputObject -is [ipaddress] -or
$InputObject -is [mailaddress]
$InputObject -is [guid] -or
$InputObject -is [regex] -or
$InputObject -is [ipaddress] -or
$InputObject -is [mailaddress]
) {
$InputObject.ToString()
}
Expand Down
4 changes: 2 additions & 2 deletions Public/Enter-ChatGPT.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Enter-ChatGPT {
#region Display header
$ConsoleWidth = [Math]::Min(46, ($Host.UI.RawUI.WindowSize.Width - 4))
if (-not $NoHeader) {
(1..$ConsoleWidth) | % { Write-Host '/' -NoNewline }
(1..$ConsoleWidth) | ForEach-Object { Write-Host '/' -NoNewline }
Write-Host ''
Write-Host @'
________ __ __________ ______
Expand All @@ -108,7 +108,7 @@ function Enter-ChatGPT {
\____/_/ /_/\__,_/\__/\____/_/ /_/
'@
(1..$ConsoleWidth) | % { Write-Host '/' -NoNewline }
(1..$ConsoleWidth) | ForEach-Object { Write-Host '/' -NoNewline }
Write-Host ''
Write-Host ''
}
Expand Down
2 changes: 1 addition & 1 deletion Public/Request-AudioSpeech.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function Request-AudioSpeech {
}
else {
$PostBody.response_format =
switch -Wildcard ($OutFile) {
switch -Wildcard ($OutFile) {
'*.mp3' { 'mp3'; break }
'*.opus' { 'opus'; break }
'*.aac' { 'aac'; break }
Expand Down
52 changes: 26 additions & 26 deletions Public/Request-ChatCompletion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -413,34 +413,34 @@ function Request-ChatCompletion {
}
Invoke-OpenAIAPIRequest @splat |
Where-Object {
-not [string]::IsNullOrEmpty($_)
} | ForEach-Object {
if ($Format -eq 'raw_response') {
$_
}
else {
try {
$_ | ConvertFrom-Json -ErrorAction Stop
-not [string]::IsNullOrEmpty($_)
} | ForEach-Object {
if ($Format -eq 'raw_response') {
$_
}
catch {
Write-Error -Exception $_.Exception
else {
try {
$_ | ConvertFrom-Json -ErrorAction Stop
}
catch {
Write-Error -Exception $_.Exception
}
}
} | Where-Object {
$Format -eq 'raw_response' -or ($null -ne $_.choices -and ($_.choices[0].delta.content -is [string]))
} | ForEach-Object -Process {
if ($Format -eq 'raw_response') {
Write-Output $_
}
else {
# Writes content to both the Information stream(6>) and the Standard output stream(1>).
$InfoMsg = [System.Management.Automation.HostInformationMessage]::new()
$InfoMsg.Message = $_.choices[0].delta.content
$InfoMsg.NoNewLine = $true
Write-Information $InfoMsg
Write-Output $InfoMsg.Message
}
}
} | Where-Object {
$Format -eq 'raw_response' -or ($null -ne $_.choices -and ($_.choices[0].delta.content -is [string]))
} | ForEach-Object -Process {
if ($Format -eq 'raw_response') {
Write-Output $_
}
else {
# Writes content to both the Information stream(6>) and the Standard output stream(1>).
$InfoMsg = [System.Management.Automation.HostInformationMessage]::new()
$InfoMsg.Message = $_.choices[0].delta.content
$InfoMsg.NoNewLine = $true
Write-Information $InfoMsg
Write-Output $InfoMsg.Message
}
}

return
}
Expand Down Expand Up @@ -580,7 +580,7 @@ function Request-ChatCompletion {
}
$LastUserMessage = ($Messages.Where({ $_.role -eq 'user' })[-1].content)
if ($LastUserMessage -isnot [string]) {
$LastUserMessage = [string]($LastUserMessage | ? { $_.type -eq 'text' } | select -Last 1).text
$LastUserMessage = [string]($LastUserMessage | Where-Object { $_.type -eq 'text' } | Select-Object -Last 1).text
}
$Response | Add-Member -MemberType NoteProperty -Name 'Message' -Value $LastUserMessage
$Response | Add-Member -MemberType NoteProperty -Name 'Answer' -Value ([string[]]$Response.choices.message.content)
Expand Down
2 changes: 1 addition & 1 deletion Public/Request-ImageEdit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function Request-ImageEdit {
Write-Output ($ResponseContent | Select-Object -ExpandProperty 'b64_json')
}
elseif ($Format -eq 'byte') {
[byte[]]$b = [Convert]::FromBase64String(($ResponseContent | Select-Object -ExpandProperty 'b64_json' | select -First 1))
[byte[]]$b = [Convert]::FromBase64String(($ResponseContent | Select-Object -ExpandProperty 'b64_json' | Select-Object -First 1))
Write-Output (, $b)
}
#endregion
Expand Down
34 changes: 17 additions & 17 deletions Public/Request-TextCompletion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,24 @@ function Request-TextCompletion {
}
Invoke-OpenAIAPIRequest @params |
Where-Object {
-not [string]::IsNullOrEmpty($_)
} | ForEach-Object {
try {
$_ | ConvertFrom-Json -ErrorAction Stop
-not [string]::IsNullOrEmpty($_)
} | ForEach-Object {
try {
$_ | ConvertFrom-Json -ErrorAction Stop
}
catch {
Write-Error -Exception $_.Exception
}
} | Where-Object {
$null -ne $_.choices -and $_.choices[0].text -is [string]
} | ForEach-Object {
# Writes content to both the Information stream(6>) and the Standard output stream(1>).
$InfoMsg = [System.Management.Automation.HostInformationMessage]::new()
$InfoMsg.Message = $_.choices[0].text
$InfoMsg.NoNewLine = $true
Write-Information $InfoMsg
Write-Output $InfoMsg.Message
}
catch {
Write-Error -Exception $_.Exception
}
} | Where-Object {
$null -ne $_.choices -and $_.choices[0].text -is [string]
} | ForEach-Object {
# Writes content to both the Information stream(6>) and the Standard output stream(1>).
$InfoMsg = [System.Management.Automation.HostInformationMessage]::new()
$InfoMsg.Message = $_.choices[0].text
$InfoMsg.NoNewLine = $true
Write-Information $InfoMsg
Write-Output $InfoMsg.Message
}
return
}
#endregion
Expand Down
Loading

0 comments on commit 017c7ee

Please sign in to comment.