Created
October 19, 2023 08:40
-
-
Save dcondrey/9d41c90bdbf209ea945958f0124c63f7 to your computer and use it in GitHub Desktop.
Get ChatGPT to talk to itself to solve your initial query
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
""" | |
OpenAI API Script | |
================= | |
This script allows you to interact with OpenAI's GPT-3.5 Turbo model. It appends a given first line to a text file, | |
then repeatedly queries OpenAI's API based on the last line in the text file and appends the response to the same file. | |
Requirements: | |
------------- | |
- OpenAI Python package: Install it using `pip install openai`. | |
- OpenAI API key: Set it in your environment variables. | |
Usage: | |
------ | |
Run the script from the command line and provide the required arguments. | |
Example: | |
-------- | |
python crosstalk.py --first_line="What is the meaning of life" --num_iterations=10000 --logfile="conversation.txt" | |
Arguments: | |
---------- | |
--first_line: The first line to append to the text file. | |
--num_iterations: The number of iterations for querying OpenAI's API. | |
--logfile: The name of the text file where the conversation will be logged. | |
""" | |
import openai | |
import os | |
import argparse | |
lastprompt = "" | |
context = "" | |
def set_openai_api_key(): | |
api_key = os.environ.get("OPENAI_API_KEY") | |
if not api_key: | |
print("Please set your OpenAI API key in the environment variable.") | |
exit(1) | |
openai.api_key = api_key | |
def append_to_file(filename, text): | |
with open(filename, "a") as f: | |
f.write(text + "\n") | |
def query_openai_and_append_response(logfile, logresponse, first_line): | |
global lastprompt | |
if not os.path.exists(logfile): | |
print(f"{logfile} does not exist.") | |
return | |
with open(logfile, "r") as f: | |
lines = f.readlines() | |
if lines: | |
last_line = lines[-1].strip() | |
else: | |
print("The text file is empty.") | |
return | |
prompt = [ | |
{"role": "assistant", "content": first_line}, | |
{"role": "system", "content": "End each response with a follow-up question related to the original prompt."}, | |
{"role": "user", "content": last_line} | |
] | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=prompt, | |
temperature=0.6, | |
max_tokens=3000, | |
presence_penalty=0.5, | |
frequency_penalty=0.2 | |
)['choices'][0]['message']['content'].strip() | |
lastprompt = prompt | |
append_to_file(logresponse, response) | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
def repeat(logfile, logresponse, num_iterations, first_line): | |
for _ in range(num_iterations): | |
query_openai_and_append_response(logfile, logresponse, first_line) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='OpenAI API script.') | |
parser.add_argument('--first_line', type=str, help='First line to append to the text file.') | |
parser.add_argument('--num_iterations', type=int, help='Number of iterations.') | |
parser.add_argument('--logfile', type=str, help='Name of the text file.') | |
args = parser.parse_args() | |
set_openai_api_key() | |
if args.first_line: | |
append_to_file(args.logfile, args.first_line) | |
repeat(args.logfile, args.logfile, args.num_iterations, args.first_line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment