From ef61137f4566b40881ac86440b18ae160f74c9de Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Sat, 29 Jun 2024 12:00:08 +0200 Subject: [PATCH] TEST --- finetuna.psm1 | 14 ------- private/Get-OpenAIAPIParameter.ps1 | 62 ++++++++++++++++++++++++++++++ tests/finetuna.Tests.ps1 | 2 - 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 private/Get-OpenAIAPIParameter.ps1 diff --git a/finetuna.psm1 b/finetuna.psm1 index b297933..f34b38d 100644 --- a/finetuna.psm1 +++ b/finetuna.psm1 @@ -81,18 +81,4 @@ if ((-not (Get-OpenAIContext).ApiKey) -and (Test-Path -Path $configFile)) { if ($persisted.AuthType) { $splat.AuthType = $persisted.AuthType } if ($persisted.Organization) { $splat.Organization = $persisted.Organization } $null = Set-TuneProvider @splat -} - -# get context values to pass to Get-OpenAIAPIParameter -$context = Get-OpenAIContext -if ($context.ApiKey) { - $PSDefaultParameterValues["Get-OpenAIAPIParameter:Parameters"] = @{ - ApiKey = $context.ApiKey - AuthType = $context.AuthType - Organization = $context.Organization - ApiBase = $context.ApiBase - ApiVersion = $context.ApiVersion - TimeoutSec = $context.TimeoutSec - MaxRetryCount = $context.MaxRetryCount - } } \ No newline at end of file diff --git a/private/Get-OpenAIAPIParameter.ps1 b/private/Get-OpenAIAPIParameter.ps1 new file mode 100644 index 0000000..6a611cd --- /dev/null +++ b/private/Get-OpenAIAPIParameter.ps1 @@ -0,0 +1,62 @@ +function Get-OpenAIAPIParameter { + param( + [Parameter(Mandatory)] + [string]$EndpointName, + + [Parameter()] + [AllowEmptyString()] + [string]$Engine = '', + + [Parameter()] + [hashtable] + $Parameters + ) + if (-not $Parameters) { + Write-Warning "NO PARAM" + # get context values to pass to Get-OpenAIAPIParameter + $context = Get-OpenAIContext + if ($context.ApiKey) { + $Parameters = @{ + ApiKey = $context.ApiKey + AuthType = $context.AuthType + Organization = $context.Organization + ApiBase = $context.ApiBase + ApiVersion = $context.ApiVersion + TimeoutSec = $context.TimeoutSec + MaxRetryCount = $context.MaxRetryCount + } + } else { + throw "No API key found in the context. Please set the API key using Set-TuneProvider." + } + } + # Get common params from session context + $OpenAIParameter = ParseCommonParams $Parameters + + # Get params from environment variables + $OpenAIParameter.ApiKey = Initialize-APIKey -ApiKey $OpenAIParameter.ApiKey -ErrorAction Stop + $OpenAIParameter.ApiBase = Initialize-APIBase -ApiBase $OpenAIParameter.ApiBase -ApiType $OpenAIParameter.ApiType -ErrorAction Stop + $OpenAIParameter.Organization = Initialize-OrganizationID -OrgId $OpenAIParameter.Organization + + # Get API endpoint and etc. + if ($OpenAIParameter.ApiType -eq [OpenAIApiType]::Azure) { + # When the user wants to use Azure, the AuthType is also set to Azure. + if ($OpenAIParameter.AuthType -notlike 'azure*') { + $OpenAIParameter.AuthType = 'azure' + } + $ApiParam = Get-AzureOpenAIAPIEndpoint -EndpointName $EndpointName -ApiBase $OpenAIParameter.ApiBase -ApiVersion $OpenAIParameter.ApiVersion -Engine $Engine + } + else { + # It looks like the API base URL is Azure even though the API type is OpenAI. In this case, ignore the base URL. + if (([string]([uri]$OpenAIParameter.ApiBase).IdnHost).EndsWith('.azure.com', [System.StringComparison]::OrdinalIgnoreCase)) { + $OpenAIParameter.ApiBase = $null + } + $OpenAIParameter.AuthType = 'openai' + $ApiParam = Get-OpenAIAPIEndpoint -EndpointName $EndpointName -ApiBase $OpenAIParameter.ApiBase + } + + foreach ($key in $ApiParam.Keys) { + $OpenAIParameter.$key = $ApiParam.$key + } + + $OpenAIParameter +} diff --git a/tests/finetuna.Tests.ps1 b/tests/finetuna.Tests.ps1 index d4a81d3..d38a741 100644 --- a/tests/finetuna.Tests.ps1 +++ b/tests/finetuna.Tests.ps1 @@ -17,8 +17,6 @@ Describe "finetuna Module Tests" { $provider = Set-TuneProvider @splat $provider.ApiKey | Should -Not -BeNullOrEmpty $provider.ApiType | Should -Be 'openai' - $PSDefaultParameterValues["Get-OpenAIAPIParameter:Parameters"] | Out-String | Write-Warning - $PSDefaultParameterValues["Get-OpenAIAPIParameter:Parameters"] | Should -Not -BeNullOrEmpty } }