Skip to content

Commit 98b2f71

Browse files
committed
Fix reusable prompt handling
1 parent 285ac08 commit 98b2f71

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

Public/Responses/Request-Response.ps1

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,27 @@ function Request-Response {
395395
#region Construct parameters for API request
396396
$Response = $null
397397
$PostBody = [System.Collections.Specialized.OrderedDictionary]::new()
398-
$PostBody.model = $Model
398+
399+
# Reusable prompts
400+
$IsReusablePromptSpecified = $false
401+
if ($PromptId) {
402+
$PromptOptions = @{id = $PromptId }
403+
if ($PSBoundParameters.ContainsKey('PromptVariables')) {
404+
$PromptOptions.variables = $PromptVariables
405+
}
406+
if ($PSBoundParameters.ContainsKey('PromptVersion')) {
407+
$PromptOptions.version = $PromptVersion
408+
}
409+
$PostBody.prompt = $PromptOptions
410+
411+
## When specifying a reusable prompt, normally required parameters such as model and input become optional.
412+
$IsReusablePromptSpecified = $true
413+
}
414+
415+
# Specify model
416+
if (-not $IsReusablePromptSpecified -or $PSBoundParameters.ContainsKey('Model')) {
417+
$PostBody.model = $Model
418+
}
399419

400420
if ($PreviousResponseId) {
401421
$PostBody.previous_response_id = $PreviousResponseId
@@ -462,18 +482,6 @@ function Request-Response {
462482
$PostBody.stream = [bool]$Stream
463483
}
464484

465-
# Reusable prompts
466-
if ($PromptId) {
467-
$PromptOptions = @{id = $PromptId }
468-
if ($PSBoundParameters.ContainsKey('PromptVariables')) {
469-
$PromptOptions.variables = $PromptVariables
470-
}
471-
if ($PSBoundParameters.ContainsKey('PromptVersion')) {
472-
$PromptOptions.version = $PromptVersion
473-
}
474-
$PostBody.prompt = $PromptOptions
475-
}
476-
477485
# Reasoning
478486
$ReasoningOptions = @{}
479487
if ($PSBoundParameters.ContainsKey('ReasoningEffort')) {
@@ -870,7 +878,7 @@ function Request-Response {
870878
#endregion
871879

872880
# Error if message is empty.
873-
if ($Messages.Count -eq 0) {
881+
if (-not $IsReusablePromptSpecified -and $Messages.Count -eq 0) {
874882
Write-Error 'No message is specified. You must specify one or more messages.'
875883
return
876884
}

Tests/Responses/Request-Response.tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,27 @@ Describe 'Request-Response' {
606606
$Result.body.model | Should -Be 'gpt-4o-mini'
607607
$Result.body.input[0].content[0].text | Should -Be 'Hello!'
608608
}
609+
610+
It 'Input Message is required' {
611+
Mock -Verifiable -ModuleName $script:ModuleName Invoke-OpenAIAPIRequest {}
612+
{ Request-Response -Model 'gpt-4o-mini' -ea Stop } | Should -Throw 'No message is specified. You must specify one or more messages.'
613+
Should -Not -InvokeVerifiable
614+
}
615+
616+
It 'When specifying a reusable prompt, input message is not required' {
617+
Mock -Verifiable -ModuleName $script:ModuleName Invoke-OpenAIAPIRequest { @'
618+
{
619+
"id": "resp_7e5a5",
620+
"object": "response",
621+
"created_at": 1743938395,
622+
"status": "completed",
623+
"model": "gpt-4o-mini-2024-07-18"
624+
}
625+
'@
626+
}
627+
{ Request-Response -PromptId 'pmpt_123' -PromptVersion 4 -PromptVariables @{'city' = 'Tokyo' } -ea Stop } | Should -Not -Throw
628+
Should -InvokeVerifiable
629+
}
609630
}
610631

611632
Context 'Integration tests (online)' -Tag 'Online' {

0 commit comments

Comments
 (0)