-
Notifications
You must be signed in to change notification settings - Fork 108
/
Get-OpenAIEdit.ps1
79 lines (67 loc) · 1.99 KB
/
Get-OpenAIEdit.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function Get-OpenAIEdit {
<#
.SYNOPSIS
Given a prompt and an instruction, the model will return an edited version of the prompt
.DESCRIPTION
Creates a new edit for the provided input, instruction, and parameters.
.PARAMETER InputText
Prompt text to evaluate
.PARAMETER Instruction
The instruction that tells the model how to edit the prompt
.PARAMETER model
ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001 model with this endpoint. Default is code-davinci-edit-001 model
.PARAMETER edits
How many edits to generate for the input and instruction
.EXAMPLE
Get-OpenAIEdit -InputText "What day of the wek is it?" -Instruction "Fix the spelling mistakes"
.NOTES
This function requires the 'OpenAIKey' environment variable to be defined before being invoked
Reference: https://platform.openai.com/docs/guides/edits/quickstart
Reference: https://platform.openai.com/docs/api-reference/edits
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$InputText,
[Parameter(Mandatory = $true, Position = 1)]
$Instruction,
[Parameter()]
[ValidateSet('text-davinci-edit-001', 'code-davinci-edit-001')]
$model = 'code-davinci-edit-001',
[Parameter()]
$numberOfEdits = 1,
[ValidateRange(0, 2)]
[decimal]$temperature = 0.0,
[ValidateRange(0, 1)]
[decimal]$top_p = 1.0,
[Switch]$Raw
)
begin {
$pipelineInput = [System.Collections.Generic.List[Object]]::new()
}
process {
$pipelineInput.Add($_)
}
end {
if (-not $pipelineInput){
$inputBody = $InputText
} else {
$inputBody = $pipelineInput | Out-String
}
$body = @{
"model" = $model
"temperature" = $temperature
"top_p" = $top_p
"input" = $inputBody
"instruction" = $Instruction
"n" = $numberOfEdits
} | ConvertTo-Json
$response = Invoke-OpenAIAPI -Uri (Get-OpenAIEditsURI) -Method Post -Body $body
if ($Raw) {
$response
}
else {
$response.choices | Select-Object text
}
}
}