Skip to content

Commit 9c5ded7

Browse files
authored
Merge pull request #115 from IntelliTect/AddChatGptModle
Added initial stab at IntelliTect.ChatGpt
2 parents 555d550 + 1955189 commit 9c5ded7

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#
2+
# Module manifest for module 'IntelliTect.Common'
3+
#
4+
# Generated by: Mark Michaelis
5+
#
6+
# Generated on: 6/24/2021
7+
#
8+
9+
@{
10+
11+
# Script module or binary module file associated with this manifest.
12+
RootModule = './IntelliTect.ChatGpt.psm1'
13+
14+
# Version number of this module.
15+
ModuleVersion = '0.1.0.5'
16+
17+
# Supported PSEditions
18+
# CompatiblePSEditions = @()
19+
20+
# ID used to uniquely identify this module
21+
GUID = 'ba706b1d-c6d3-439f-a1da-2973b4f9aa02'
22+
23+
# Author of this module
24+
Author = 'Mark Michaelis'
25+
26+
# Company or vendor of this module
27+
CompanyName = 'IntelliTect'
28+
29+
# Copyright statement for this module
30+
Copyright = '(c) 2023 IntelliTect. All rights reserved.'
31+
32+
# Description of the functionality provided by this module
33+
Description = 'Provides PowerShell module for calling ChatGPT'
34+
35+
# Minimum version of the PowerShell engine required by this module
36+
# PowerShellVersion = ''
37+
38+
# Name of the PowerShell host required by this module
39+
# PowerShellHostName = ''
40+
41+
# Minimum version of the PowerShell host required by this module
42+
# PowerShellHostVersion = ''
43+
44+
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
45+
# DotNetFrameworkVersion = ''
46+
47+
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
48+
# ClrVersion = ''
49+
50+
# Processor architecture (None, X86, Amd64) required by this module
51+
# ProcessorArchitecture = ''
52+
53+
# Modules that must be imported into the global environment prior to importing this module
54+
# RequiredModules = @()
55+
56+
# Assemblies that must be loaded prior to importing this module
57+
# RequiredAssemblies = @()
58+
59+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
60+
# ScriptsToProcess = @()
61+
62+
# Type files (.ps1xml) to be loaded when importing this module
63+
# TypesToProcess = @()
64+
65+
# Format files (.ps1xml) to be loaded when importing this module
66+
# FormatsToProcess = @()
67+
68+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
69+
# NestedModules = @()
70+
71+
# 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.
72+
FunctionsToExport = '*'
73+
74+
# 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.
75+
CmdletsToExport = '*'
76+
77+
# Variables to export from this module
78+
VariablesToExport = '*'
79+
80+
# 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.
81+
AliasesToExport = '*'
82+
83+
# DSC resources to export from this module
84+
# DscResourcesToExport = @()
85+
86+
# List of all modules packaged with this module
87+
# ModuleList = @()
88+
89+
# List of all files packaged with this module
90+
# FileList = @()
91+
92+
# 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.
93+
PrivateData = @{
94+
95+
PSData = @{
96+
97+
# Tags applied to this module. These help with module discovery in online galleries.
98+
# Tags = @()
99+
100+
# A URL to the license for this module.
101+
# LicenseUri = ''
102+
103+
# A URL to the main website for this project.
104+
# ProjectUri = ''
105+
106+
# A URL to an icon representing this module.
107+
# IconUri = ''
108+
109+
# ReleaseNotes of this module
110+
# ReleaseNotes = ''
111+
112+
# Prerelease string of this module
113+
# Prerelease = ''
114+
115+
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
116+
# RequireLicenseAcceptance = $false
117+
118+
# External dependent modules of this module
119+
# ExternalModuleDependencies = @()
120+
121+
} # End of PSData hashtable
122+
123+
} # End of PrivateData hashtable
124+
125+
# HelpInfo URI of this module
126+
# HelpInfoURI = ''
127+
128+
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
129+
# DefaultCommandPrefix = ''
130+
131+
}
132+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
$completionsUri = 'https://api.openai.com/v1/completions'
3+
4+
Function Invoke-ChatGpt {
5+
[CmdletBinding()]
6+
param(
7+
[parameter(Mandatory,ValueFromPipeline)][string]$Prompt,
8+
[string]$ApiKey = $env:OpenAIApiKey,
9+
[ValidateRange(0, 2)][float]$Temperature
10+
)
11+
12+
if([string]::IsNullOrWhiteSpace($ApiKey)) {
13+
$ApiKey = Read-Host -Prompt "What is the ApiKey (see https://openai.com/api/)?" -MaskInput
14+
if([string]::IsNullOrWhiteSpace($ApiKey)) {
15+
Write-Error "Missing an argument for parameter 'ApiKey'. Specify a parameter of type 'System.String' and try again."
16+
}
17+
}
18+
if($Temperature) {
19+
$body = "{
20+
""model"": ""text-davinci-003"",
21+
""prompt"": ""$Prompt"",
22+
""temperature"": $Temperature
23+
}"
24+
}
25+
else {
26+
$body = "{
27+
""model"": ""text-davinci-003"",
28+
""prompt"": ""$Prompt""
29+
}"
30+
}
31+
Write-Debug "`$body = $body"
32+
33+
$result = Invoke-WebRequest -uri $completionsUri `
34+
-Headers @{
35+
'Content-Type' = 'application/json'; `
36+
'Authorization' = "Bearer $ApiKey"
37+
} `
38+
-Method POST -body $body -ErrorVariable $LastError -ErrorAction Ignore
39+
40+
# ToDo: Handle error (such as missing API key).
41+
# if($LastError) {
42+
# $result | ConvertFrom-Json
43+
# }
44+
45+
$result | Select-Object -ExpandProperty 'Content' `
46+
| ConvertFrom-Json | Select-Object -ExpandProperty 'Choices' `
47+
| Select-Object -ExpandProperty 'Text' | ForEach-Object { $_.Trim() }
48+
}

0 commit comments

Comments
 (0)