-
Notifications
You must be signed in to change notification settings - Fork 17
/
openai.ps1
569 lines (504 loc) · 20.6 KB
/
openai.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<#
.SYNOPSIS
This script is a wrapper for the OpenAI API. It sends a message to the API and returns the response.
.DESCRIPTION
This script is a wrapper for the OpenAI API. It sends a message to the API and returns the response.
The script requires an API key to be set in the environment variable OPENAI_API_KEY or passed as a parameter.
The script also requires the message to be sent to the API to be passed as a parameter.
The script uses the Invoke-RestMethod cmdlet to make the API request.
The response from the API is then output to the console.
The script also logs the response to a file if a log file is specified.
.NOTES
File Name : openai.ps1
Author : Jagilber
version: 240518
https://platform.openai.com/docs/api-reference/models
https://platform.openai.com/docs/guides/prompt-engineering
Tactics:
Include details in your query to get more relevant answers
Ask the model to adopt a persona
Use delimiters to clearly indicate distinct parts of the input
Specify the steps required to complete a task
Provide examples
Specify the desired length of the output
Instruct the model to answer using a reference text
Instruct the model to answer with citations from a reference text
Instruct the model to work out its own solution before rushing to a conclusion
Use inner monologue or a sequence of queries to hide the model's reasoning process
Ask the model if it missed anything on previous passes
When using the OpenAI API chat completion, you can use various message roles to structure the conversation. The choice of roles depends on the context and your specific use case. However, here are ten commonly used message roles:
1. system: Used for initial instructions or guidance for the assistant.
2. user: Represents user input, questions, or instructions.
3. assistant: Represents the assistant's responses or actions.
4. developer: Used for presenting high-level instructions to the assistant.
5. customer: Represents a customer or end-user in a customer support scenario.
6. support: Represents a support agent in a customer support scenario.
7. manager: Represents a manager or team lead providing instructions or guidance.
8. reviewer: Used for providing feedback on the assistant's responses or behavior.
9. colleague: Represents a colleague or team member in a collaboration scenario.
10. expert: Represents a subject matter expert providing specific domain knowledge.
response:
{
"id": "chatcmpl-....",
"object": "chat.completion",
"created": 1706976614,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": "@{role=assistant; content=I'm sorry, I am an AI and do not have the capability to know the current time. Please check your device or a reliable source for the accurate time.}",
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 33,
"total_tokens": 45
},
"system_fingerprint": null
}
.EXAMPLE
.\openai.ps1 -prompts 'can you help me with a question?'
.EXAMPLE
.\openai.ps1 -prompts 'can you help me with a question?' -apiKey '<your-api-key>'
.EXAMPLE
.\openai.ps1 -prompts 'can you help me with a question?' -apiKey '<your-api-key>' -promptRole 'user'
.EXAMPLE
.\openai.ps1 -prompts 'can you help me with a question?' -apiKey '<your-api-key>' -promptRole 'user' -model 'gpt-4'
.PARAMETER prompts
The message to send to the OpenAI API.
.PARAMETER apiKey
The API key to use for the OpenAI API. If not specified, the script will attempt to use the environment variable OPENAI_API_KEY.
.PARAMETER promptRole
The role of the message to send to the OpenAI API. This can be either 'system' or 'user'. The default is 'system'.
.PARAMETER model
The model to use for the OpenAI API. This can be either 'gpt-3.5-turbo', 'gpt-3.5-turbo-0613', 'gpt-4-turbo', or 'gpt-4'. The default is 'gpt-3.5-turbo'.
.PARAMETER logFile
The log file to write the response from the OpenAI API to. If not specified, the response will not be logged.
.PARAMETER promptsFile
The file to store the conversation history. If not specified, the conversation history will not be stored.
.PARAMETER seed
The seed to use for the OpenAI API. The default is the process ID of the script.
.PARAMETER newConversation
If specified, the conversation history will be reset.
.PARAMETER completeConversation
If specified, the conversation history will not be saved.
.PARAMETER logProbabilities
If specified, the log probabilities will be included in the response.
.PARAMETER systemPrompts
The system prompts to use for the OpenAI API. If not specified, the default system prompts will be used.
.LINK
[net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/openai.ps1" -outFile "$pwd\openai.ps1";
write-host 'set api key in environment variable OPENAI_API_KEY or pass as parameter'
.\openai.ps1 'can you help me with a question?'
#>
[cmdletbinding()]
param(
[string[]]$prompts = @(),
[string]$apiKey = "$env:OPENAI_API_KEY",
[ValidateSet('user', 'system', 'assistant', 'user', 'function', 'tool')]
[string]$promptRole = 'user',
[ValidateSet('https://api.openai.com/v1/chat/completions', 'https://api.openai.com/v1/images/completions', 'https://api.openai.com/v1/davinci-codex/completions')]
[string]$endpoint = '',
[ValidateSet('gpt-3.5-turbo-1106', 'gpt-4-turbo', 'dall-e-2', 'dall-e-3', 'davinci-codex-003', 'gpt-4o', 'gpt-4o-2024-05-13')]
[string]$model = 'gpt-4o',
[string]$logFile = "$psscriptroot\openai.log",
[string]$promptsFile = "$psscriptroot\openaiMessages.json",
[int]$seed = $pid,
[switch]$continueConversation,
[switch]$newConversation = !$continueConversation,
[switch]$completeConversation,
[bool]$logProbabilities = $false,
[string]$imageQuality = 'hd',
[int]$imageCount = 1, # n
[switch]$imageEdit, # edit image
[string]$imageFilePng = "$psscriptroot\downloads\openai.png",
[ValidateSet('256x256', '512x512', '1024x1024', '1792x1024', '1024x1792')]
[string]$imageSize = '1024x1024', # dall-e 2 only supports up to 512x512
[ValidateSet('vivid', 'natural')]
[string]$imageStyle = 'vivid',
[int]$maxTokens = 4096,
[string]$outputPath = "$psscriptroot\output",
[string]$user = 'default',
[ValidateSet('url', 'b64_json')]
[string]$imageResponseFormat = 'url',
[ValidateSet('json', 'markdown')]
[string]$responseFileFormat = 'markdown',
[ValidateSet('json_object', 'text')]
[string]$responseFormat = 'json_object',
[string[]]$systemPrompts = @(
'use chain of thought reasoning to break down and step through the prompts thoroughly, reiterating for precision when generating a response.',
'prefer accurate and complete responses including any references and citations',
'use github.com, stackoverflow.com, microsoft.com, azure.com, openai.com, grafana.com, wikipedia.com, associatedpress.com, reuters.com, referencesource.microsoft.com and other reliable sources for the response'
),
[switch]$listAssistants,
[switch]$listModels,
[switch]$quiet,
[switch]$whatIf,
[switch]$init
)
[ValidateSet('chat', 'images', 'davinci-codex', 'custom')]
[string]$script:endpointType = 'chat'
$script:messageRequests = [collections.arraylist]::new()
$script:systemPromptsList = [collections.arraylist]::new($systemPrompts)
$variableExclusions = @('apiKey', 'init', '.*Variable', '.*Action', '.*Buffer', 'Debug', 'Verbose')
$parameterNames = $psCmdlet.myInvocation.myCommand.parameters.values.name | sort-object
$boundParameters = $PSBoundParameters
function main() {
$endpoint = get-endpoint
if (!(set-variables)) {
return
}
$startTime = Get-Date
write-log "===================================="
write-log ">>>>starting openAI chat request $startTime<<<<" -color White
if (!$apiKey) {
write-log "API key not found. Please set the OPENAI_API_KEY environment variable or pass the API key as a parameter." -color Red
return
}
if ($responseFormat -imatch 'json') {
[void]$script:systemPromptsList.add(' always reply in json format.')
}
if ($responseFileFormat -ieq 'markdown') {
[void]$script:systemPromptsList.add(' format reply message content in github markdown format.')
$markdownJsonSchema = convert-toJson @{
markdown = @{
content = '<markdown content>'
name = '<github compliant markdown file name with dashes and extension>'
references = @(
@{
name = '<reference name>'
url = '<reference url>'
}
)
}
} -compress
[void]$script:systemPromptsList.add(' json_object response schema:' + $markdownJsonSchema)
[void]$script:systemPromptsList.add(' include the markdown content directly ready for presentation.')
[void]$script:systemPromptsList.add(' include flow diagrams to visually describe topic. use mermaid for creation of figures and flow diagrams.')
}
if ($imageFilePng -and !(test-path ([io.path]::GetDirectoryName($imageFilePng)))) {
write-log "creating directory: [io.path]::GetDirectoryName($imageFilePng)" -color Yellow
mkdir -Force ([io.path]::GetDirectoryName($imageFilePng))
}
if ($newConversation -and (Test-Path $promptsFile)) {
write-log "resetting context" -color Yellow
write-log "deleting messages file: $promptsFile" -color Yellow
Remove-Item $promptsFile
}
if (Test-Path $promptsFile) {
write-log "reading messages from file: $promptsFile" -color Yellow
[void]$script:messageRequests.AddRange(@(Get-Content $promptsFile | ConvertFrom-Json))
}
$headers = @{
'Authorization' = "Bearer $apiKey"
'Content-Type' = 'application/json'
'OpenAI-Beta' = 'assistants=v1'
}
if ($endpointType -eq 'images') {
$headers.'Content-Type' = 'multipart/form-data'
#$headers.Add('Accept', 'image/png')
}
$requestBody = build-requestBody $script:messageRequests $script:systemPromptsList
# Convert the request body to JSON
$jsonBody = convert-toJson $requestBody -compress
if ($listModels) {
write-log "listing models" -color Yellow
$response = invoke-rest 'https://api.openai.com/v1/models' $headers
write-log "models: $(convert-toJson $response)" -color Yellow
return
}
if ($listAssistants) {
write-log "listing assistants" -color Yellow
$response = invoke-rest 'https://api.openai.com/v1/assistants?limit=100' $headers
write-log "assistants: $(convert-toJson $response)" -color Yellow
return
}
# Make the API request using Invoke-RestMethod
$response = invoke-rest $endpoint $headers $jsonBody
$message = read-messageResponse $response $script:messageRequests
open-withCode (save-MessageResponse $message.content)
$global:openaiResponse = $response
$global:message = $message
write-log "api response stored in global variables: `$global:openaiResponse and `$global:message" -ForegroundColor Cyan
if ($logFile) {
write-log "result appended to logfile: $logFile"
}
# Write the assistant response to the log file for future reference
if (!$completeConversation -and $promptsFile) {
# $script:messageRequests += $message
convert-toJson $script:messageRequests | Out-File $promptsFile
write-log "messages stored in: $promptsFile" -ForegroundColor Cyan
}
write-log "response:$(convert-toJson ($message.content | convertfrom-json))" -color Green
write-log ($global:openaiResponse | out-string) -color DarkGray
write-log "use alias 'ai' to run script with new prompt. example:ai '$($prompts[0])'" -color DarkCyan
write-log ">>>>ending openAI chat request $(((get-date) - $startTime).TotalSeconds.ToString("0.0")) seconds<<<<" -color White
write-log "===================================="
return #$message.content
}
function build-requestBody($messageRequests, $systemPrompts) {
switch -Wildcard ($script:endpointType) {
'chat' {
$requestBody = build-chatRequestBody $messageRequests $systemPrompts
}
'images' {
$requestBody = build-imageRequestBody $messageRequests $systemPrompts
}
'davinci-codex' {
$requestBody = build-codexRequestBody $messageRequests $systemPrompts
}
}
write-log "request body: $(convert-toJson $requestBody)" -color Yellow
return $requestBody
}
function build-chatRequestBody($messageRequests, $systemPrompts) {
if (!$messageRequests) {
foreach ($message in $systemPrompts) {
[void]$messageRequests.Add(@{
role = 'system'
content = $message
})
}
}
foreach ($message in $prompts) {
[void]$messageRequests.Add(@{
role = $promptRole
content = $message
})
}
$requestBody = @{
response_format = @{
type = $responseFormat
}
model = $model
seed = $seed
logprobs = $logProbabilities
messages = $messageRequests.toArray()
user = $user
max_tokens = $maxTokens
}
return $requestBody
}
function build-codexRequestBody($messageRequests) {
throw "model $model not supported"
$requestBody = @{
model = $model
seed = $seed
logprobs = $logProbabilities
messages = $script:messageRequests.toArray()
user = $user
max_tokens = $maxTokens
}
return $requestBody
}
function build-imageRequestBody($messageRequests) {
[void]$messageRequests.AddRange($prompts)
if ($imageEdit) {
if (!(Test-Path $imageFilePng)) {
throw "image file not found: $imageFilePng"
}
$requestBody = @{
model = $model
prompt = [string]::join('. ', $messageRequests.ToArray())
n = $imageCount
response_format = $imageResponseFormat
size = $imageSize
user = $user
max_tokens = $maxTokens
image = $imageFilePng # to-base64StringFromFile $imageFilePng
}
}
else {
$requestBody = @{
model = $model
prompt = [string]::join('. ', $messageRequests.ToArray())
quality = $imageQuality
n = $imageCount
response_format = $imageResponseFormat
size = $imageSize
style = $imageStyle
user = $user
max_tokens = $maxTokens
}
}
return $requestBody
}
function convert-toJson([object]$object, [int]$depth = 5, [switch]$compress = $false) {
return convertto-json -InputObject $object -depth $depth -WarningAction SilentlyContinue -compress:$compress
}
function get-endpoint() {
switch -Wildcard ($model) {
'gpt-*' {
$endpoint = 'https://api.openai.com/v1/chat/completions'
$script:endpointType = 'chat'
}
'dall-e-*' {
$endpoint = 'https://api.openai.com/v1/images/generations'
$script:endpointType = 'images'
if ($imageEdit) {
$endpoint = 'https://api.openai.com/v1/images/edits'
}
}
'codex-*' {
$endpoint = 'https://api.openai.com/v1/davinci-codex/completions'
$script:endpointType = 'davinci-codex'
}
default {
#$endpoint = 'https://api.openai.com/v1/chat/completions'
$script:endpointType = 'custom'
}
}
write-log "using endpoint: $endpoint" -color Yellow
return $endpoint
}
function invoke-rest($endpoint, $headers, $jsonBody = $null) {
if (!$whatIf -and $jsonBody) {
write-log "invoke-restMethod -Uri $endpoint -Headers $(convert-toJson $headers) -Method Post -Body $jsonBody" -color Cyan
$response = invoke-restMethod -Uri $endpoint -Headers $headers -Method Post -Body $jsonBody
}
elseif (!$whatIf) {
write-log "invoke-restMethod -Uri $endpoint -Headers $(convert-toJson $headers) -Method Get" -color Cyan
$response = invoke-restMethod -Uri $endpoint -Headers $headers -Method Get
}
write-log (convert-toJson $response) -color Magenta
$global:openaiResponse = $response
return $response
}
function open-withCode($file) {
if ($file) {
write-log "opening file with code: $file" -color Yellow
code $file
}
}
function read-messageResponse($response, [collections.arraylist]$messageRequests) {
# Extract the response from the API request
write-log $response
switch ($script:endpointType) {
'chat' {
$message = $response.choices.message
$messageRequests += $message
if ($message.content) {
$error.Clear()
if (($messageObject = convertfrom-json $message.content -AsHashtable) -and !$error) {
write-log "converting message content from json to compressed json" -color Yellow
$message.content = (convert-toJson $messageObject -depth 99)
}
}
}
'images' {
$message = $response.data
if ($response.data.revised_prompt) {
write-log "revised prompt: $($response.data.revised_prompt)" -color Yellow
$messageRequests.Clear()
[void]$messageRequests.Add($response.data.revised_prompt)
}
if ($response.data.url) {
write-log "downloading image: $($response.data.url)" -color Yellow
write-log "invoke-webRequest -Uri $($response.data.url) -OutFile $imageFilePng"
invoke-webRequest -Uri $response.data.url -OutFile $imageFilePng
$tempImageFile = $imageFilePng.replace(".png", "$(get-date -f 'yyMMdd-HHmmss').png")
writ-log "copying image to $tempImageFile" -color Yellow
Copy-Item $imageFilePng $tempImageFile
code $tempImageFile
}
$message | add-member -MemberType NoteProperty -Name 'content' -Value $message.url
}
'davinci-codex' {
throw "model $model not supported"
}
default {
write-log "unknown endpoint type: $script:endpointType" -color Red
}
}
write-log "message: $(convert-toJson $message)" -color Yellow
return $message
}
function save-MessageResponse($message) {
if (!(test-path $outputPath)) {
write-log "creating directory: $outputPath" -color Yellow
mkdir -Force $outputPath
}
$responseExtension = '.json'
$baseFileName = "$outputPath\openai"
$responseFile = "$baseFileName-$(get-date -f 'yyMMddHHmmss')"
if ($responseFileFormat -ieq 'markdown') {
$responseExtension = '.md'
$response = convertfrom-json $message -AsHashtable
$message = $response.markdown.content
if ($response.markdown.name) {
$responseFile = "$responseFile-$($response.markdown.name.trimend($responseExtension))"
}
}
write-log "saving markdown response to $responseFile$responseExtension" -color Magenta
$message | out-file -FilePath "$responseFile$responseExtension"
copy-item "$responseFile$responseExtension" "$baseFileName$responseExtension" -force
return "$baseFileName$responseExtension"
}
function set-variables() {
# Enumerate all parameters
if (!$global:ai -or $init) {
$global:ai = [ordered]@{}
}
write-log "set-alias ai $($MyInvocation.ScriptName)"
set-alias ai $MyInvocation.ScriptName -scope global
set-alias openai $MyInvocation.ScriptName -scope global
write-debug ($boundParameters | convertto-Json)
foreach ($name in $parameterNames) {
write-debug "checking variable: $name"
if ($name -imatch [string]::join('|', $variableExclusions)) {
write-debug "excluded variable: $name"
continue
}
$variable = get-variable -name $name -erroraction SilentlyContinue
if (!$boundParameters[$name] -and ![string]::IsNullOrEmpty($global:ai[$name])) {
$value = $global:ai[$name]
}
elseif ($boundParameters[$name]) {
$value = $boundParameters[$name]
}
elseif ($variable) {
$value = $variable.Value
}
else {
write-debug "variable not found: $name"
$value = $null
}
$variable.value = $value
write-debug "$name = $value"
$global:ai[$name] = $value
}
if ($init) {
write-debug "variables: $(convert-toJson $global:ai -depth 1)"
write-log "ai initialized" -color Green
return $false
}
return $true
}
function to-FileFromBase64String($base64) {
$bytes = [convert]::FromBase64String($base64)
$file = [io.path]::GetTempFileName()
[io.file]::WriteAllBytes($file, $bytes)
return $file
}
function to-base64StringFromFile($file) {
$bytes = [io.file]::ReadAllBytes($file)
$base64 = [convert]::ToBase64String($bytes)
return $base64
}
function write-log($message, [switch]$verbose, [ConsoleColor]$color = 'White') {
$message = "$(get-date) $message"
if ($logFile) {
# Write the message to a log file
$message | out-file -FilePath $logFile -Append
}
if ($verbose) {
write-verbose $message
}
elseif (!$quiet) {
write-host $message -ForegroundColor $color
}
}
main