Created
March 26, 2024 18:48
-
-
Save fsndzomga/54be02d3ec61965dca23c1ec1a341eb1 to your computer and use it in GitHub Desktop.
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
from openai import OpenAI | |
from dotenv import load_dotenv | |
load_dotenv() | |
class GPT3DFA: | |
def __init__(self, topic): | |
self.current_state = 'start' | |
self.client = OpenAI() | |
self.states = ['start', 'content', 'conclusion', 'done'] | |
self.state_functions = { | |
'start': self.start_state, | |
'content': self.content_state, | |
'conclusion': self.conclusion_state, | |
} | |
self.topic = topic | |
self.context = "" # To accumulate text across states | |
def start_state(self): | |
prompt = f"Write an introduction about {self.topic}." | |
return self.generate_text(prompt) | |
def content_state(self): | |
prompt = f"Given the introduction: \"{self.context}\" Now, continue writing about {self.topic}." | |
return self.generate_text(prompt) | |
def conclusion_state(self): | |
prompt = f"Given the content: \"{self.context}\" Now, conclude the text on {self.topic}." | |
return self.generate_text(prompt) | |
def generate_text(self, prompt): | |
response = self.client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": prompt}, | |
], | |
n=1, | |
temperature=0.5 | |
) | |
text = response.choices[0].message.content | |
return text | |
def run(self): | |
for state in self.states: | |
if state == 'done': | |
break | |
additional_text = self.state_functions[state]() | |
self.context += additional_text | |
print(additional_text, end='\n\n') | |
# Example usage | |
dfa = GPT3DFA(topic="the history of the internet") | |
dfa.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment