Created
February 16, 2024 16:36
-
-
Save scaery/a65e3cb4c36232c9c7c6c9c3da851097 to your computer and use it in GitHub Desktop.
ChatGPT Terminal v3 - Hacking with AI knowledge
This file contains 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
#!/usr/bin/env python3 | |
import os, io | |
from openai import OpenAI | |
from dotenv import load_dotenv | |
load_dotenv() # create .env with OPENAI_API_KEY="Your-chatgpt-api-key begins with sk..." | |
client = OpenAI( | |
# This is the default and can be omitted | |
api_key=os.environ.get("OPENAI_API_KEY"), | |
) | |
def get_chatgpt_response(question): | |
response = client.chat.completions.create( | |
messages = [ {"role": "user", "content": question} ], | |
model="gpt-3.5-turbo", | |
temperature=0 # ranges from 0 to 2, with lower values indicating greater determinism and higher values indicating more randomness | |
) | |
response_message = response.choices[0].message.content | |
return response_message | |
def read_question_from_input(): | |
question_lines = [] | |
print("Enter your question (Press ESCAPE to send the question to ChatGPT):\n") | |
while True: | |
char = input() | |
if char == chr(27): # ASCII code for ESCAPE key | |
break | |
question_lines.append(char) | |
return "".join(question_lines) | |
def main(): | |
question = read_question_from_input() | |
if question.strip(): | |
answer = get_chatgpt_response(question) | |
with io.open("log_chatgpt.log", "a", encoding="UTF-8") as f: | |
print(f"\n\n# START #################################################\nYou:\n\n{question} ???\n", file=f) | |
print(f"ChatGPT:\n\n {answer}", file=f) | |
print("\n# END ###################################################\n", file=f) | |
print("ChatGPT:\n\n", answer) | |
else: | |
print("No question provided. Exiting.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment