ChatGPT experiment in Python.
Last active
May 17, 2023 20:23
-
-
Save lsloan/0606974c5cd3590191650e40397e941e to your computer and use it in GitHub Desktop.
ChatGPT experiment in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import openai | |
class ChatApp: | |
def __init__(self, model='gpt-3.5-turbo', load_file=''): | |
# Setting the API key to use the OpenAI API | |
# openai.api_key = os.getenv('OPENAI_API_KEY') | |
self.model = model | |
self.messages = [] | |
if load_file != '': | |
self.load(load_file) | |
def chat(self, message): | |
if message == 'exit': | |
self.save() | |
os._exit(1) | |
elif message == 'save': | |
self.save() | |
return '(saved)' | |
self.messages.append({'role': 'user', 'content': message}) | |
try: | |
response = openai.ChatCompletion.create( | |
model=self.model, | |
messages=self.messages | |
) | |
except openai.error.AuthenticationError as e: | |
print(e) | |
raise e | |
self.messages.append({'role': 'assistant', | |
'content': response['choices'][0][ | |
'message'].content}) | |
return response['choices'][0]['message'] | |
def messagesToJson(self, indent=4) -> str: | |
return json.dumps(self.messages, indent=indent) | |
def save(self): | |
try: | |
import time | |
import re | |
import json | |
ts = time.time() | |
json_object = self.messagesToJson(indent=4) | |
filename_suffix = self.messages[0]['content'][0:30] | |
filename_suffix = re.sub('[^0-9a-zA-Z]+', '-', | |
f'{filename_suffix}_{ts}') | |
with open(f'models/chat_model_{filename_suffix}.json', | |
'w') as outfile: | |
outfile.write(json_object) | |
except: | |
os._exit(1) | |
def load(self, load_file): | |
with open(load_file) as f: | |
data = json.load(f) | |
self.messages = data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
from ChatGPT import ChatApp | |
app = ChatApp(model='gpt-3.5-turbo') | |
def main(): | |
try: | |
while True: | |
request = input('» ') | |
print('Processing…\r', end='') | |
try: | |
response = app.chat(request) | |
except openai.error.AuthenticationError: | |
break | |
print(20 * ' ', '\r', end='') | |
print(response.content) | |
except EOFError: | |
pass | |
finally: | |
app.save() | |
print() | |
print('Exiting.\nFull conversation transcript…') | |
print(app.messagesToJson(indent=2)) | |
if '__main__' == __name__: | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openai==0.27.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment