forked from dtkav/chat-gpt-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGPT-Terminal.py
45 lines (38 loc) · 1.17 KB
/
ChatGPT-Terminal.py
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
# Import the necessary modules
import openai
import os
import getpass
# Set the API key and some initial parameter values
api_key = ""
temperature = 0.5 # 0 to 1
max_tokens = 2000 # max 4000
personality = ""
# Check the operating system and set the appropriate command to clear the terminal screen
if os.name == 'nt':
clear = 'cls'
else:
clear = 'clear'
# Define a function to clear the terminal screen
def clearScreen():
os.system(clear)
print("===== ChatGPT-Terminal =====")
# Call the function to clear the terminal screen
clearScreen()
# Prompt the user for their API key if it hasn't been provided in the code already
if api_key == "":
openai.api_key = getpass.getpass(prompt="OpenAI API Key")
else:
openai.api_key = api_key
# Enter a while loop to continually prompt the user for input and generate responses from the OpenAI API
while True:
response = openai.Completion.create(
model="text-davinci-003",
prompt=personality + input('\n' + "> "),
temperature=temperature,
max_tokens=max_tokens,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
# Print the generated response
print(">> " + response["choices"][0]["text"].strip())