From 11f921396467457eea149026d01b0dcd53c29310 Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Sun, 18 Dec 2022 17:38:59 -0800 Subject: [PATCH 1/2] Added initial stab at IntelliTect.ChatGpt --- .../IntelliTect.ChatGpt.psd1 | 132 ++++++++++++++++++ .../IntelliTect.ChatGpt.psm1 | 48 +++++++ 2 files changed, 180 insertions(+) create mode 100644 Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 create mode 100644 Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psm1 diff --git a/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 b/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 new file mode 100644 index 0000000..3cafb7d --- /dev/null +++ b/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 @@ -0,0 +1,132 @@ +# +# Module manifest for module 'IntelliTect.Common' +# +# Generated by: Mark Michaelis +# +# Generated on: 6/24/2021 +# + +@{ + +# Script module or binary module file associated with this manifest. + RootModule = './IntelliTect.ChatGpt.psm1' + +# Version number of this module. +ModuleVersion = '0.1.0.1' + +# Supported PSEditions +# CompatiblePSEditions = @() + +# ID used to uniquely identify this module +GUID = 'ba706b1d-c6d3-439f-a1da-2973b4f9aa02' + +# Author of this module +Author = 'Mark Michaelis' + +# Company or vendor of this module +CompanyName = 'IntelliTect' + +# Copyright statement for this module +Copyright = '(c) 2023 IntelliTect. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'Provides PowerShell module for calling ChatGPT' + +# Minimum version of the PowerShell engine required by this module +# PowerShellVersion = '' + +# Name of the PowerShell host required by this module +# PowerShellHostName = '' + +# Minimum version of the PowerShell host required by this module +# PowerShellHostVersion = '' + +# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# DotNetFrameworkVersion = '' + +# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# ClrVersion = '' + +# Processor architecture (None, X86, Amd64) required by this module +# ProcessorArchitecture = '' + +# Modules that must be imported into the global environment prior to importing this module +# RequiredModules = @() + +# Assemblies that must be loaded prior to importing this module +# RequiredAssemblies = @() + +# Script files (.ps1) that are run in the caller's environment prior to importing this module. +# ScriptsToProcess = @() + +# Type files (.ps1xml) to be loaded when importing this module +# TypesToProcess = @() + +# Format files (.ps1xml) to be loaded when importing this module +# FormatsToProcess = @() + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +# NestedModules = @() + +# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. +FunctionsToExport = '*' + +# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. +CmdletsToExport = '*' + +# Variables to export from this module +VariablesToExport = '*' + +# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. +AliasesToExport = '*' + +# DSC resources to export from this module +# DscResourcesToExport = @() + +# List of all modules packaged with this module +# ModuleList = @() + +# List of all files packaged with this module +# FileList = @() + +# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + # Tags = @() + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + # ProjectUri = '' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + # Prerelease string of this module + # Prerelease = '' + + # Flag to indicate whether the module requires explicit user acceptance for install/update/save + # RequireLicenseAcceptance = $false + + # External dependent modules of this module + # ExternalModuleDependencies = @() + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +# HelpInfo URI of this module +# HelpInfoURI = '' + +# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. +# DefaultCommandPrefix = '' + +} + diff --git a/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psm1 b/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psm1 new file mode 100644 index 0000000..9c3c179 --- /dev/null +++ b/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psm1 @@ -0,0 +1,48 @@ + +$completionsUri = 'https://api.openai.com/v1/completions' + +Function Invoke-ChatGpt { + [CmdletBinding()] + param( + [parameter(Mandatory,ValueFromPipeline)][string]$Prompt, + [string]$ApiKey = $env:OpenAIApiKey, + [ValidateRange(0, 2)][float]$Temperature + ) + + if([string]::IsNullOrWhiteSpace($ApiKey)) { + $ApiKey = Read-Host -Prompt "What is the ApiKey (see https://openai.com/api/)?" -MaskInput + if([string]::IsNullOrWhiteSpace($ApiKey)) { + Write-Error "Missing an argument for parameter 'ApiKey'. Specify a parameter of type 'System.String' and try again." + } + } + if($Temperature) { + $body = "{ + ""model"": ""text-davinci-003"", + ""prompt"": ""$Prompt"", + ""temperature"": $Temperature + }" + } + else { + $body = "{ + ""model"": ""text-davinci-003"", + ""prompt"": ""$Prompt"" + }" + } + Write-Debug "`$body = $body" + + $result = Invoke-WebRequest -uri $completionsUri ` + -Headers @{ + 'Content-Type' = 'application/json'; ` + 'Authorization' = "Bearer $ApiKey" + } ` + -Method POST -body $body -ErrorVariable $LastError -ErrorAction Ignore + + # ToDo: Handle error (such as missing API key). + # if($LastError) { + # $result | ConvertFrom-Json + # } + + $result | Select-Object -ExpandProperty 'Content' ` + | ConvertFrom-Json | Select-Object -ExpandProperty 'Choices' ` + | Select-Object -ExpandProperty 'Text' | ForEach-Object { $_.Trim() } +} From 1955189e2cba27e0b0e9de255821311c77590446 Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Mon, 19 Dec 2022 16:18:36 -0800 Subject: [PATCH 2/2] Test version change. --- Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 b/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 index 3cafb7d..f441714 100644 --- a/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 +++ b/Modules/IntelliTect.ChatGpt/IntelliTect.ChatGpt.psd1 @@ -12,7 +12,7 @@ RootModule = './IntelliTect.ChatGpt.psm1' # Version number of this module. -ModuleVersion = '0.1.0.1' +ModuleVersion = '0.1.0.5' # Supported PSEditions # CompatiblePSEditions = @()