-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
117 lines (99 loc) · 3.24 KB
/
gpt.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
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
import atexit
import os
import openai
import requests
import random as r
from utils import get_files_recursively, show_dict
class ChatGPT:
def __init__(self):
openai.api_key = os.getenv("OPENAI_API_KEY")
# Register the save_history method with atexit
#atexit.register(self.save_history)
self.name = "ChatGPT"
self.model = "text-davinci-003"
#self.conversation_history = ""
#self.load_history()
def chat(self, prompt:str=None):
""" open a single chat exchange
returns response text and exits chat
"""
if not prompt:
prompt = input("you: ")
#self.conversation_history += f"User: {prompt}\n{self.name}:"
res = openai.Completion.create(
model=self.model,
#prompt=#self.conversation_history,
prompt=prompt,
max_tokens=1000,
temperature=0,
debug=True)
reply = res.choices[0].text.strip()
#self.conversation_history += reply + "\n"
show_dict(res)
@property
def talk(self):
""" open a conversation in a While loop
exit with <quit> command
"""
while True:
prompt = input("you: ")
if prompt.lower() == "quit":
print("Goodbye!")
break
res = self.chat(prompt)
print(f"gpt:\n{res}")
#def save_history(self):
# from datetime import datetime
#
# # # Get current date and time
# # now = datetime.now()
#
# # # Format date and time as string
# # timestamp = now.strftime("%Y%m%d%H%M%S")
#
# # with open("history.txt", 'w') as f:
# # f.write(f"timestamp: {timestamp}")
# f.write(#self.conversation_history + "\n")
# f.close()
#
# print("history saved")
"""def load_history(self):
with open("history.txt", 'r') as f:
data = f.read()
self.conversation_history += data
f.close()
def clear_history(self):
yorn = input("ARE YOU SURE??? y or n: ")
if yorn.lower() == "y":
random_num = str(r.randint(1000, 9999))
confirm = input(f"Enter {random_num} to clear history: ")
if confirm == random_num:
with open("history.txt", 'w') as f:
data = f.write("")
f.close()
print("history cleared!")"""
def read_file(self, path:str=None):
if not path:
path = input("Enter path: ")
self.chat(files._get_file(path))
#class FileStorage:
# def __init__(self):
# pass
# def _get_file(self, path:str=None, name:str=None):
# if not path:
# path = input("Enter path: ")
# if not name:
# name = os.path.basename(path)
# dot = name.index(".")
# name = name[:dot]
# print(f"Name: {name}")
#
# with open(path, 'r') as f:
# data = f.read()
#
# self.__setattr__(name, data)
# print(f"{name} successfully uploaded")
# return data
#
openai.api_key = "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"
gpt = ChatGPT()