· 2 years ago · May 04, 2023, 06:10 PM
1# Originally written by, https://github.com/yzwijsen
2# https://raw.githubusercontent.com/yzwijsen/chatgpt-powershell/main/ChatGPT-Conversation.ps1
3<#
4This script will let you have a conversation with ChatGPT.
5It shows how to keep a history of all previous messages and feed them into the REST API in order to have an ongoing conversation.
6#>
7
8# Define API key and endpoint
9# $ApiKey = Read-Host -Prompt 'Your API Key Here' # Removed this to manually define the variable for ease of calling inside of manually setting this every iteration.
10$ApiEndpoint = "https://api.openai.com/v1/chat/completions"
11
12<#
13System message.
14You can use this to give the AI instructions on what to do, how to act or how to respond to future prompts.
15Default value for ChatGPT = "You are a helpful assistant."
16#>
17$AiSystemMessage = "You are a helpful assistant"
18
19# we use this list to store the system message and will add any user prompts and ai responses as the conversation evolves.
20[System.Collections.Generic.List[Hashtable]]$MessageHistory = @()
21
22# Clears the message history and fills it with the system message (and allows us to reset the history and start a new conversation)
23Function Initialize-MessageHistory ($message){
24 $script:MessageHistory.Clear()
25 $script:MessageHistory.Add(@{"role" = "system"; "content" = $message}) | Out-Null
26}
27
28# Function to send a message to ChatGPT. (We need to pass the entire message history in each request since we're using a RESTful API)
29function Invoke-ChatGPT ($MessageHistory) {
30 # Set the request headers
31 $headers = @{
32 "Content-Type" = "application/json"
33 "Authorization" = "Bearer $ApiKey"
34 }
35
36 # Set the request body
37 $requestBody = @{
38 "model" = "gpt-3.5-turbo"
39 "messages" = $MessageHistory
40 "max_tokens" = 1000 # Max amount of tokens the AI will respond with
41 "temperature" = 0.7 # Lower is more coherent and conservative, higher is more creative and diverse.
42 }
43
44 # Send the request
45 $response = Invoke-RestMethod -Method POST -Uri $ApiEndpoint -Headers $headers -Body (ConvertTo-Json $requestBody)
46
47 # Return the message content
48 return $response.choices[0].message.content
49}
50
51# Show startup text
52Clear-Host
53Write-Host "######################`n# ChatGPT Powershell #`n######################`n`nEnter your prompt to continue. (type 'exit' to quit or 'reset' to start a new chat)" -ForegroundColor Yellow
54
55# Add system message to MessageHistory
56Initialize-MessageHistory $AiSystemMessage
57
58# Main loop
59while ($true) {
60 # Capture user input
61 $userMessage = Read-Host "`nYou"
62
63 # Check if user wants to exit or reset
64 if ($userMessage -eq "exit") {
65 break
66 }
67 if ($userMessage -eq "reset") {
68 # Reset the message history so we can start with a clean slate
69 Initialize-MessageHistory $AiSystemMessage
70
71 Write-Host "Messages reset." -ForegroundColor Yellow
72 continue
73 }
74
75 # Add new user prompt to list of messages
76 $MessageHistory.Add(@{"role"="user"; "content"=$userMessage})
77
78 # Query ChatGPT
79 $aiResponse = Invoke-ChatGPT $MessageHistory
80
81 # Show response
82 Write-Host "AI: $aiResponse" -ForegroundColor Yellow
83
84 # Add ChatGPT response to list of messages
85 $MessageHistory.Add(@{"role"="assistant"; "content"=$aiResponse})
86}