diff --git a/Formatting/GPT-3.Result.Format.ps1 b/Formatting/GPT-3.Result.Format.ps1
new file mode 100644
index 0000000..7a79afc
--- /dev/null
+++ b/Formatting/GPT-3.Result.Format.ps1
@@ -0,0 +1,42 @@
+# The default formatter just prints the answer
+Write-FormatView -TypeName GPT-3.Result -Action {
+ Write-FormatViewExpression -Property Answer
+}
+
+# The view QuestionAnswer prints both the question and the answer
+Write-FormatView -TypeName GPT-3.Result -Action {
+ Write-FormatViewExpression -Text "You Asked:" -ForegroundColor Warning
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Property Question
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Text "It Answered:" -ForegroundColor Verbose
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Property Answer
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+} -Name 'QuestionAnswer'
+
+# The view 'QuestionAnswerTime' shows the question, answer, and times.
+Write-FormatView -TypeName GPT-3.Result -Action {
+ Write-FormatViewExpression -ScriptBlock { "@ $($_.QuestionTimeStamp), You Asked:" } -ForegroundColor Warning
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Property Question
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -ScriptBlock { "@ $($_.AnswerTimeStamp), It Answered:" } -ForegroundColor Verbose
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Property Answer
+ Write-FormatViewExpression -Newline
+ Write-FormatViewExpression -Newline
+} -Name 'QuestionAnswerTime'
+
+# Make a table view that shows question and answer
+Write-FormatView -TypeName 'GPT-3.Result' -Property Question, Answer -Width 20 -Wrap
+
+# And make a list view for good measure.
+Write-FormatView -TypeName 'GPT-3.Result' -Property Question, QuestionTime, Answer, AnswerTime, AnswerTook -AsList
diff --git a/PowerShellAI.ezout.ps1 b/PowerShellAI.ezout.ps1
new file mode 100644
index 0000000..2d39853
--- /dev/null
+++ b/PowerShellAI.ezout.ps1
@@ -0,0 +1,39 @@
+#requires -Module EZOut
+# Install-Module EZOut or https://github.com/StartAutomating/EZOut
+$myFile = $MyInvocation.MyCommand.ScriptBlock.File
+$myModuleName = 'PowerShellAI'
+$myRoot = $myFile | Split-Path
+Push-Location $myRoot
+$formatting = @(
+ # Add your own Write-FormatView here,
+ # or put them in a Formatting or Views directory
+ foreach ($potentialDirectory in 'Formatting','Views') {
+ Join-Path $myRoot $potentialDirectory |
+ Get-ChildItem -ea ignore |
+ Import-FormatView -FilePath {$_.Fullname}
+ }
+)
+
+$destinationRoot = $myRoot
+
+if ($formatting) {
+ $myFormatFile = Join-Path $destinationRoot "$myModuleName.format.ps1xml"
+ $formatting | Out-FormatData -Module $MyModuleName | Set-Content $myFormatFile -Encoding UTF8
+ Get-Item $myFormatFile
+}
+
+$types = @(
+ # Add your own Write-TypeView statements here
+ # or declare them in the 'Types' directory
+ Join-Path $myRoot Types |
+ Get-Item -ea ignore |
+ Import-TypeView
+
+)
+
+if ($types) {
+ $myTypesFile = Join-Path $destinationRoot "$myModuleName.types.ps1xml"
+ $types | Out-TypeData | Set-Content $myTypesFile -Encoding UTF8
+ Get-Item $myTypesFile
+}
+Pop-Location
diff --git a/PowerShellAI.format.ps1xml b/PowerShellAI.format.ps1xml
new file mode 100644
index 0000000..d7cfebd
--- /dev/null
+++ b/PowerShellAI.format.ps1xml
@@ -0,0 +1,619 @@
+
+
+
+
+
+ GPT-3.Result
+
+ GPT-3.Result
+
+
+
+
+
+
+ Answer
+
+
+
+
+
+
+
+ QuestionAnswer
+
+ GPT-3.Result
+
+
+
+
+
+
+ $moduleName = 'PowerShellAI'
+
+ do {
+ $lm = Get-Module -Name $moduleName -ErrorAction Ignore
+ if (-not $lm) { continue }
+ if ($lm.FormatPartsLoaded) { break }
+ $wholeScript = @(foreach ($formatFilePath in $lm.exportedFormatFiles) {
+ foreach ($partNodeName in Select-Xml -LiteralPath $formatFilePath -XPath "/Configuration/Controls/Control/Name[starts-with(., '$')]") {
+ $ParentNode = $partNodeName.Node.ParentNode
+ "$($ParentNode.Name)={
+ $($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBinding.ScriptBlock)}"
+ }
+ }) -join [Environment]::NewLine
+ New-Module -Name "${ModuleName}.format.ps1xml" -ScriptBlock ([ScriptBlock]::Create(($wholeScript + ';Export-ModuleMember -Variable *'))) |
+ Import-Module -Global
+ $onRemove = [ScriptBlock]::Create("Remove-Module '${ModuleName}.format.ps1xml'")
+
+ if (-not $lm.OnRemove) {
+ $lm.OnRemove = $onRemove
+ } else {
+ $lm.OnRemove = [ScriptBlock]::Create($onRemove.ToString() + '' + [Environment]::NewLine + $lm.OnRemove)
+ }
+ $lm | Add-Member NoteProperty FormatPartsLoaded $true -Force
+
+ } while ($false)
+
+
+@(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Warning' -NoClear) -join ''
+
+ You Asked:
+
+ @(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Warning' ) -join ''
+
+
+
+
+ Question
+
+
+
+
+ @(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Verbose' -NoClear) -join ''
+
+ It Answered:
+
+ @(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Verbose' ) -join ''
+
+
+
+
+ Answer
+
+
+
+
+
+
+
+
+
+ QuestionAnswerTime
+
+ GPT-3.Result
+
+
+
+
+
+
+ $moduleName = 'PowerShellAI'
+
+ do {
+ $lm = Get-Module -Name $moduleName -ErrorAction Ignore
+ if (-not $lm) { continue }
+ if ($lm.FormatPartsLoaded) { break }
+ $wholeScript = @(foreach ($formatFilePath in $lm.exportedFormatFiles) {
+ foreach ($partNodeName in Select-Xml -LiteralPath $formatFilePath -XPath "/Configuration/Controls/Control/Name[starts-with(., '$')]") {
+ $ParentNode = $partNodeName.Node.ParentNode
+ "$($ParentNode.Name)={
+ $($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBinding.ScriptBlock)}"
+ }
+ }) -join [Environment]::NewLine
+ New-Module -Name "${ModuleName}.format.ps1xml" -ScriptBlock ([ScriptBlock]::Create(($wholeScript + ';Export-ModuleMember -Variable *'))) |
+ Import-Module -Global
+ $onRemove = [ScriptBlock]::Create("Remove-Module '${ModuleName}.format.ps1xml'")
+
+ if (-not $lm.OnRemove) {
+ $lm.OnRemove = $onRemove
+ } else {
+ $lm.OnRemove = [ScriptBlock]::Create($onRemove.ToString() + '' + [Environment]::NewLine + $lm.OnRemove)
+ }
+ $lm | Add-Member NoteProperty FormatPartsLoaded $true -Force
+
+ } while ($false)
+
+
+@(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Warning' -NoClear) -join ''
+
+
+ "@ $($_.QuestionTimeStamp), You Asked:"
+
+
+ @(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Warning' ) -join ''
+
+
+
+
+ Question
+
+
+
+
+ @(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Verbose' -NoClear) -join ''
+
+
+ "@ $($_.AnswerTimeStamp), It Answered:"
+
+
+ @(& ${PowerShellAI_Format-RichText} -ForegroundColor 'Verbose' ) -join ''
+
+
+
+
+ Answer
+
+
+
+
+
+
+
+
+
+ GPT-3.Result
+
+ GPT-3.Result
+
+
+
+
+ left
+ 20
+
+
+ left
+
+
+
+
+
+
+
+ Question
+
+
+ Answer
+
+
+
+
+
+
+
+
+
+ ${PowerShellAI_Format-RichText}
+
+
+
+
+
+
+ <#
+ .Synopsis
+ Formats the text color of output
+ .Description
+ Formats the text color of output
+
+ * ForegroundColor
+ * BackgroundColor
+ * Bold
+ * Underline
+ .Notes
+ Stylized Output works in two contexts at present:
+ * Rich consoles (Windows Terminal, PowerShell.exe, Pwsh.exe) (when $host.UI.SupportsVirtualTerminal)
+ * Web pages (Based off the presence of a $Request variable, or when $host.UI.SupportsHTML (you must add this property to $host.UI))
+ #>
+ [Management.Automation.Cmdlet("Format","Object")]
+ [ValidateScript({
+ $canUseANSI = $host.UI.SupportsVirtualTerminal
+ $canUseHTML = $Request -or $host.UI.SupportsHTML -or $OutputMode -eq 'HTML'
+ if (-not ($canUseANSI -or $canUseHTML)) { return $false}
+ return $true
+ })]
+ [OutputType([string])]
+ param(
+ # The input object
+ [Parameter(ValueFromPipeline)]
+ [PSObject]
+ $InputObject,
+
+ # The foreground color
+ [string]$ForegroundColor,
+
+ # The background color
+ [string]$BackgroundColor,
+
+ # If set, will render as bold
+ [switch]$Bold,
+
+ # If set, will render as italic.
+ [Alias('Italics')]
+ [switch]$Italic,
+
+ # If set, will render as faint
+ [switch]$Faint,
+
+ # If set, will render as hidden text.
+ [switch]$Hide,
+
+ # If set, will render as blinking (not supported in all terminals or HTML)
+ [switch]$Blink,
+
+ # If set, will render as strikethru
+ [Alias('Strikethrough', 'Crossout')]
+ [switch]$Strikethru,
+
+ # If set, will underline text
+ [switch]$Underline,
+
+ # If set, will double underline text.
+ [switch]$DoubleUnderline,
+
+ # If set, will invert text
+ [switch]$Invert,
+
+ # If provided, will create a hyperlink to a given uri
+ [Alias('Hyperlink', 'Href')]
+ [uri]
+ $Link,
+
+ # If set, will not clear formatting
+ [switch]$NoClear,
+
+ # The alignment. Defaulting to Left.
+ # Setting an alignment will pad the remaining space on each line.
+ [ValidateSet('Left','Right','Center')]
+ [string]
+ $Alignment,
+
+ # The length of a line. By default, the buffer width
+ [int]$LineLength = $($host.UI.RawUI.BufferSize.Width)
+ )
+
+ begin {
+ $canUseANSI = $host.UI.SupportsVirtualTerminal
+ $canUseHTML = $Request -or $host.UI.SupportsHTML -or $OutputMode -eq 'HTML'
+ $knownStreams = @{
+ Output='';Error='BrightRed';Warning='BrightYellow';
+ Verbose='BrightCyan';Debug='Yellow';Progress='Cyan';
+ Success='BrightGreen';Failure='Red';Default=''}
+
+ $ansiCode = [Regex]::new(@'
+ (?<ANSI_Code>
+ (?-i)\e # An Escape
+ \[ # Followed by a bracket
+ (?<ParameterBytes>[\d\:\;\<\=\>\?]{0,}) # Followed by zero or more parameter
+ bytes
+ (?<IntermediateBytes>[\s\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/]{0,}) # Followed by zero or more
+ intermediate bytes
+ (?<FinalByte>[\@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_\`abcdefghijklmnopqrstuvwxyz\{\|\}\~]) # Followed by a final byte
+
+ )
+'@)
+ $esc = [char]0x1b
+ $standardColors = 'Black', 'Red', 'Green', 'Yellow', 'Blue','Magenta', 'Cyan', 'White'
+ $brightColors = 'BrightBlack', 'BrightRed', 'BrightGreen', 'BrightYellow', 'BrightBlue','BrightMagenta', 'BrightCyan', 'BrightWhite'
+
+ $allOutput = @()
+
+ $n =0
+ $cssClasses = @()
+ $colorAttributes =
+ @(:nextColor foreach ($hc in $ForegroundColor,$BackgroundColor) {
+ $n++
+ if (-not $hc) { continue }
+ if ($hc[0] -eq $esc) {
+ if ($canUseANSI) {
+ $hc; continue
+ }
+ }
+
+ $ansiStartPoint = if ($n -eq 1) { 30 } else { 40 }
+ if ($knownStreams.ContainsKey($hc)) {
+ $i = $brightColors.IndexOf($knownStreams[$hc])
+ if ($canUseHTML) {
+ $cssClasses += $hc
+ } else {
+ if ($i -ge 0 -and $canUseANSI) {
+ '' + $esc + "[1;$($ansiStartPoint + $i)m"
+ } else {
+ $i = $standardColors.IndexOf($knownStreams[$hc])
+ if ($i -ge 0 -and $canUseANSI) {
+ '' + $esc + "[1;$($ansiStartPoint + $i)m"
+ } elseif ($i -le 0 -and $canUseANSI) {
+ '' + $esc + "[$($ansistartpoint + 8):5m"
+ }
+ }
+ }
+ continue nextColor
+ }
+ elseif ($standardColors -contains $hc) {
+ for ($i = 0; $i -lt $standardColors.Count;$i++) {
+ if ($standardColors[$i] -eq $hc) {
+ if ($canUseANSI -and -not $canUseHTML) {
+ '' + $esc + "[$($ansiStartPoint + $i)m"
+ } else {
+ $cssClasses += $standardColors[$i]
+ }
+ continue nextColor
+ }
+ }
+ } elseif ($brightColors -contains $hc) {
+ for ($i = 0; $i -lt $brightColors.Count;$i++) {
+ if ($brightColors[$i] -eq $hc) {
+ if ($canUseANSI -and -not $canUseHTML) {
+ '' + $esc + "[1;$($ansiStartPoint + $i)m"
+ } else {
+ $cssClasses += $standardColors[$i]
+ }
+ continue nextColor
+ }
+ }
+ }
+ elseif ($psStyle -and $psStyle.Formatting.$hc -and
+ $psStyle.Formatting.$hc -match '^\e') {
+ if ($canUseANSI -and -not $canUseHTML) {
+ $psStyle.Formatting.$hc
+ } else {
+ $cssClasses += "formatting-$hc"
+ }
+ }
+ elseif (-not $n -and $psStyle -and $psStyle.Foreground.$hc -and
+ $psStyle.Foreground.$hc -match '^\e' ) {
+ if ($canUseANSI -and -not $canUseHTML) {
+ $psStyle.Foreground.$hc
+ } else {
+ $cssClasses += "foreground-$hc"
+ }
+ }
+ elseif ($n -and $psStyle -and $psStyle.Background.$hc -and
+ $psStyle.Background.$hc -match '^\e') {
+ if ($canUseANSI -and -not $canUseHTML) {
+ $psStyle.Background.$hc
+ } else {
+ $cssClasses += "background-$hc"
+ }
+ }
+
+
+
+ if ($hc -and $hc -notmatch '^[\#\e]') {
+ $placesToLook=
+ @(if ($hc.Contains('.')) {
+ $module, $setting = $hc -split '\.', 2
+ $theModule = Get-Module $module
+ $theModule.PrivateData.Color,
+ $theModule.PrivateData.Colors,
+ $theModule.PrivateData.Colour,
+ $theModule.PrivateData.Colours,
+ $theModule.PrivateData.EZOut,
+ $global:PSColors,
+ $global:PSColours
+ } else {
+ $setting = $hc
+ $moduleColorSetting = $theModule.PrivateData.PSColors.$setting
+ })
+
+ foreach ($place in $placesToLook) {
+ if (-not $place) { continue }
+ foreach ($propName in $setting -split '\.') {
+ $place = $place.$propName
+ if (-not $place) { break }
+ }
+ if ($place -and "$place".StartsWith('#') -and 4,7 -contains "$place".Length) {
+ $hc = $place
+ continue
+ }
+ }
+ if (-not $hc.StartsWith -or -not $hc.StartsWith('#')) {
+ continue
+ }
+ }
+ $r,$g,$b = if ($hc.Length -eq 7) {
+ [int]::Parse($hc[1..2]-join'', 'HexNumber')
+ [int]::Parse($hc[3..4]-join '', 'HexNumber')
+ [int]::Parse($hc[5..6] -join'', 'HexNumber')
+ }elseif ($hc.Length -eq 4) {
+ [int]::Parse($hc[1], 'HexNumber') * 16
+ [int]::Parse($hc[2], 'HexNumber') * 16
+ [int]::Parse($hc[3], 'HexNumber') * 16
+ }
+
+ if ($canUseHTML) {
+ if ($n -eq 1) { "color:$hc" }
+ elseif ($n -eq 2) { "background-color:$hc"}
+ }
+ elseif ($canUseANSI) {
+ if ($n -eq 1) { $esc+"[38;2;$r;$g;${b}m" }
+ elseif ($n -eq 2) { $esc+"[48;2;$r;$g;${b}m" }
+ }
+
+ })
+
+ $styleAttributes = @() + $colorAttributes
+
+ $styleAttributes += @(
+ if ($Bold) {
+ if ($canUseHTML) {"font-weight:bold"}
+ elseif ($canUseANSI) { '' + $esc + "[1m" }
+ }
+ if ($Faint) {
+ if ($canUseHTML) { "opacity:.5" }
+ elseif ($canUseANSI) { '' + $esc + "[2m" }
+ }
+ if ($Italic) {
+ if ($canUseHTML) { "font-weight:bold" }
+ elseif ($canUseANSI) {'' + $esc + "[3m" }
+ }
+
+ if ($Underline -and -not $doubleUnderline) {
+ if ($canUseHTML) { "text-decoration:underline"}
+ elseif ($canUseANSI) {'' +$esc + "[4m" }
+ }
+
+ if ($Blink) {
+ if ($canUseANSI) { '' +$esc + "[5m" }
+ }
+
+ if ($invert) {
+ if ($canUseHTML) {"filter:invert(100%)"}
+ elseif ($canUseANSI) { '' + $esc + "[7m"}
+ }
+
+ if ($hide) {
+ if ($canUseHTML) {"opacity:0"}
+ elseif ($canUseANSI) { '' + $esc + "[8m"}
+ }
+
+ if ($Strikethru) {
+ if ($canUseHTML) {"text-decoration: line-through"}
+ elseif ($canUseANSI) { '' +$esc + "[9m" }
+ }
+
+ if ($DoubleUnderline) {
+ if ($canUseHTML) { "border-bottom: 3px double;"}
+ elseif ($canUseANSI) {'' +$esc + "[21m" }
+ }
+
+ if ($Alignment -and $canUseHTML) {
+ "display:block;text-align:$($Alignment.ToLower())"
+ }
+
+ if ($Link) {
+ if ($canUseHTML) {
+ # Hyperlinks need to be a nested element
+ # so we will not add it to style attributes for HTML
+ }
+ elseif ($canUseANSI) {
+ # For ANSI,
+ '' + $esc + ']8;;' + $Link + $esc + '\'
+ }
+ }
+
+ )
+
+ $header =
+ if ($canUseHTML) {
+ "<span$(
+ if ($styleAttributes) { " style='$($styleAttributes -join ';')'"}
+ )$(
+ if ($cssClasses) { " class='$($cssClasses -join ' ')'"}
+ )>" + $(
+ if ($Link) {
+ "<a href='$link'>"
+ }
+ )
+ } elseif ($canUseANSI) {
+ $styleAttributes -join ''
+ }
+ }
+
+ process {
+ $inputObjectAsString =
+ "$(if ($inputObject) { $inputObject | Out-String})".Trim()
+
+ $inputObjectAsString =
+ if ($Alignment -and -not $canUseHTML) {
+ (@(foreach ($inputObjectLine in ($inputObjectAsString -split '(?>\r\n|\n)')) {
+ $inputObjectLength = $ansiCode.Replace($inputObjectLine, '').Length
+ if ($inputObjectLength -lt $LineLength) {
+ if ($Alignment -eq 'Left') {
+ $inputObjectLine
+ } elseif ($Alignment -eq 'Right') {
+ (' ' * ($LineLength - $inputObjectLength)) + $inputObjectLine
+ } else {
+ $half = ($LineLength - $inputObjectLength)/2
+ (' ' * [Math]::Floor($half)) + $inputObjectLine +
+ (' ' * [Math]::Ceiling($half))
+ }
+ }
+ else {
+ $inputObjectLine
+ }
+ }) -join [Environment]::NewLine) + [Environment]::newline
+ } else {
+ $inputObjectAsString
+ }
+
+ $allOutput +=
+ if ($header) {
+ "$header" + $inputObjectAsString
+ }
+ elseif ($inputObject) {
+ $inputObjectAsString
+ }
+ }
+
+ end {
+
+ if (-not $NoClear) {
+ $allOutput +=
+ if ($canUseHTML) {
+ if ($Link) {
+ "</a>"
+ }
+ "</span>"
+ }
+ elseif ($canUseANSI) {
+ if ($Bold -or $Faint -or $colorAttributes -match '\[1;') {
+ "$esc[22m"
+ }
+ if ($Italic) {
+ "$esc[23m"
+ }
+ if ($Underline -or $doubleUnderline) {
+ "$esc[24m"
+ }
+ if ($Blink) {
+ "$esc[25m"
+ }
+ if ($Invert) {
+ "$esc[27m"
+ }
+ if ($hide) {
+ "$esc[28m"
+ }
+ if ($Strikethru) {
+ "$esc[29m"
+ }
+ if ($ForegroundColor) {
+ "$esc[39m"
+ }
+ if ($BackgroundColor) {
+ "$esc[49m"
+ }
+
+ if ($Link) {
+ "$esc]8;;$esc\"
+ }
+
+ if (-not ($Underline -or $Bold -or $Invert -or $ForegroundColor -or $BackgroundColor)) {
+ '' + $esc + '[0m'
+ }
+ }
+ }
+
+ $allOutput -join ''
+ }
+
+
+
+
+
+
+
+
+
diff --git a/PowerShellAI.psd1 b/PowerShellAI.psd1
index ae666da..317fa38 100644
--- a/PowerShellAI.psd1
+++ b/PowerShellAI.psd1
@@ -16,6 +16,9 @@ PowerShell GPT AI module allows to integrate with OpenAI API and access GPT-3 mo
'Get-GPT3Completion'
)
+ FormatsToProcess = 'PowerShellAI.format.ps1xml'
+ TypesToProcess = 'PowerShellAI.types.ps1xml'
+
AliasesToExport = @(
'gpt'
)
diff --git a/PowerShellAI.types.ps1xml b/PowerShellAI.types.ps1xml
new file mode 100644
index 0000000..14642c3
--- /dev/null
+++ b/PowerShellAI.types.ps1xml
@@ -0,0 +1,28 @@
+
+
+
+
+ GPT-3.Result
+
+
+ ToString
+
+
+
+ Answer
+
+ $this.choices[0].text.trim()
+
+
+
+
+ AnswerTime
+
+ [DateTime]::UnixEpoch.AddSeconds($this.created).ToLocalTime()
+
+
+
+
+
diff --git a/Public/Get-GPT3Completion.ps1 b/Public/Get-GPT3Completion.ps1
index 522a6ed..71758b7 100644
--- a/Public/Get-GPT3Completion.ps1
+++ b/Public/Get-GPT3Completion.ps1
@@ -68,22 +68,18 @@ function Get-GPT3Completion {
stop = $stop
}
- $body = $body | ConvertTo-Json -Depth 5
- $body = [System.Text.Encoding]::UTF8.GetBytes($body)
$params = @{
Uri = "https://api.openai.com/v1/completions"
Method = 'Post'
Headers = @{Authorization = "Bearer $($env:OpenAIKey)" }
- ContentType = 'application/json'
- #body = $body | ConvertTo-Json -Depth 5
- body = $body
- }
-
- #$params["body"] = [System.Text.Encoding]::UTF8.GetBytes($json)
+ ContentType = 'application/json'
+ body = [Text.Encoding]::UTF8.GetBytes(
+ ($body | ConvertTo-Json -Depth 5)
+ )
+ }
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
if ($env:USERNAME -eq 'finke') { $exclude = 'Headers' }
-
$params |
ConvertTo-Json -Depth 10 |
ConvertFrom-Json |
@@ -94,12 +90,30 @@ function Get-GPT3Completion {
# Write-Progress -Activity 'PowerShellAI' -Status 'Processing GPT repsonse. Please wait...'
- $result = Invoke-RestMethod @params
-
- if ($Raw) {
- $result
- }
- else {
- $result.choices[0].text
- }
+ # Note the time right before we ask for the result.
+ $QuestionTime = [DateTime]::Now
+ $result = Invoke-RestMethod @params
+ $result.pstypenames.clear()
+ # Decorate the result as a GPT-3.Result (for default formatting)
+ $result.pstypenames.add("GPT-3.Result")
+ # and as a GPT-3.Result of that model (in case someone wanted to format results from a model )
+ $result.pstypenames.add("GPT-3.Result.$model")
+
+ # Add three properties to each returned object:
+
+ # * Our request body
+ $result.psobject.properties.add([psnoteproperty]::new(
+ "RequestBody", $body
+ ))
+ # * The Question we asked
+ $result.psobject.properties.add([psnoteproperty]::new(
+ "Question", $body.prompt
+ ))
+ # * The Question's Timestamp
+ $result.psobject.properties.add([psnoteproperty]::new(
+ "QuestionTime", $QuestionTime
+ ))
+
+ # Now, output the result.
+ $result
}
diff --git a/Types/GPT-3.Result/ToString.ps1 b/Types/GPT-3.Result/ToString.ps1
new file mode 100644
index 0000000..3a6258a
--- /dev/null
+++ b/Types/GPT-3.Result/ToString.ps1
@@ -0,0 +1 @@
+$this.Answer
\ No newline at end of file
diff --git a/Types/GPT-3.Result/get_Answer.ps1 b/Types/GPT-3.Result/get_Answer.ps1
new file mode 100644
index 0000000..bf0e722
--- /dev/null
+++ b/Types/GPT-3.Result/get_Answer.ps1
@@ -0,0 +1 @@
+$this.choices[0].text.trim()
diff --git a/Types/GPT-3.Result/get_AnswerTime.ps1 b/Types/GPT-3.Result/get_AnswerTime.ps1
new file mode 100644
index 0000000..79c672b
--- /dev/null
+++ b/Types/GPT-3.Result/get_AnswerTime.ps1
@@ -0,0 +1 @@
+[DateTime]::UnixEpoch.AddSeconds($this.created).ToLocalTime()
\ No newline at end of file