From c4052464c01652044f4b3dd3a1238153d1a5006d Mon Sep 17 00:00:00 2001 From: Roger Gonzalez Date: Sun, 14 May 2023 10:45:43 -0300 Subject: [PATCH] Improved prompting by a lot! Error rate is closer to zero --- app.py | 61 +++++++++++++++++++++++++++++++------------------------- htmls.py | 7 ++++++- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index 04d3512..4b4c4a3 100644 --- a/app.py +++ b/app.py @@ -18,7 +18,7 @@ openai.api_key = os.getenv("OPENAI_KEY") logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") -def format_message(message: str) -> list: +def format_message(message: str, role: str) -> list: """ Formats the message to send to chatgpt @@ -32,12 +32,7 @@ def format_message(message: str) -> list: None """ return [ - { - "role": "system", - "content": ( - "You are a very knowledgeable person with random knowledge for everything the humanity has ever done" - ), - }, + {"role": "system", "content": role}, {"role": "user", "content": message}, ] @@ -67,7 +62,7 @@ def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None] html = "" + match[1] html_match = html.split("") possible_html = html_match[0] + "" - if "(topic)" not in possible_html and "(slugified_related_topic_name)" not in possible_html: + if "(slugified_related_topic_name)" not in possible_html: return possible_html @@ -90,7 +85,8 @@ def get_random_topic() -> str: messages=format_message( "Give me a random list of 10 topics to talk about in a comma separated list. Each topic should be less " "than 5 words long and should not contain commas. Be as creative as possible and choose topics from " - "multiple genres and industries." + "multiple genres and industries.", + "You are a very knowledgeable person with random knowledge about everything the humanity has done", ), ) @@ -103,7 +99,7 @@ def get_random_topic() -> str: logging.info(f"The topics list is: {topics}") topic = random.choice(topics) - return topic.strip() + return topic.strip().capitalize() except openai.error.RateLimitError: return get_random_topic() @@ -125,7 +121,10 @@ def generate_topic_image(topic: str) -> str: try: completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", - messages=format_message(f"Generate a prompt for dall-e about {topic}"), + messages=format_message( + f"Generate a prompt for dall-e about {topic}. Use less than 10 words and only return the prompt", + "You are a dall-e expert", + ), ) dalle_prompt = completion.choices[0].message.content @@ -137,7 +136,7 @@ def generate_topic_image(topic: str) -> str: generate_topic_image(topic) -def get_chatgpt_html_response(message: str, count: int = 0) -> str: +def get_chatgpt_html_response(message: str, count: int = 0, html: str = None) -> str: """ Gets the response from chatgpt @@ -162,15 +161,16 @@ def get_chatgpt_html_response(message: str, count: int = 0) -> str: completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=format_message( - ( - f"Give me information about {message}. Now, given this HTML: \n {prompt_html_template} \n replace " - "(topic) with the name of the selected topic and (topic_info) with 5 parragraphs talking about the " - "topic. Replace (related_links) with 5 links related to the topic. For these links, the href " - "should be '/infinite?topic=(slugified_related_topic_name)' and the text should be the name of the " - "related topic. The name of these topics should not be more than 5 words. Finally, replace " - "(new_topics) with a 5 links with completely new and random topics to talk about. These links " - "should follow the same rules as before. Do not replace (image_source). Respond only the code" - ) + f""" + You are going to add information to this: {html}. + - Add 5 parragraphs talking about {message} in the "information-container" div. + - Add 5 links for related topics in the "related-topics-container" div. The href should be + "/infinite?topic=(slugified_related_topic_name)" and the text should be "(related_topic_name). + - Add 5 links for new random topics not related to {message} in the "new-topics-container" div. The href + should be the same as before. + - Only respond with the HTML code + """, + "You are a very skilled web developer with expertise in HTML.", ), ) except openai.error.RateLimitError: @@ -188,8 +188,13 @@ def get_chatgpt_html_response(message: str, count: int = 0) -> str: logging.info(f"The image is {image}") - html_with_image = re.sub(pattern, f'src="{image}"', html) - return html_with_image + complete_html = re.sub(pattern, f'src="{image}"', html) + return_html = complete_html.split("")[0] + return_html += ( + "" + ) + + return return_html @app.route("/") @@ -200,19 +205,21 @@ def index(): @app.route("/start") def start(): random_topic = get_random_topic() - return get_chatgpt_html_response(random_topic) + html = prompt_html_template.replace("(topic)", random_topic) + return get_chatgpt_html_response(random_topic, html=html) @app.route("/infinite") def topic(): - topic = request.args.get("topic", None) + topic = request.args.get("topic", None) or request.args.get("topics", None) if not topic: return redirect("/start") - unslugified_topic = topic.replace("-", " ").replace("_", " ").replace("%20", " ").replace("\n", " ") + unslugified_topic = topic.replace("-", " ").replace("_", " ").replace("%20", " ").capitalize() if len(unslugified_topic.split()) > 5: logging.info(f"The topic is too long: {unslugified_topic}") return "Error! Your topic should be < 5 words" - return get_chatgpt_html_response(unslugified_topic) + html = prompt_html_template.replace("(topic)", unslugified_topic) + return get_chatgpt_html_response(unslugified_topic, html=html) diff --git a/htmls.py b/htmls.py index fd53f25..f81e311 100644 --- a/htmls.py +++ b/htmls.py @@ -11,6 +11,11 @@ index_html = """

Want to kmow more? https://gitlab.com/rogs/the-infinite-website

# noqa E501 + """ @@ -30,7 +35,7 @@ prompt_html_template = """ (topic_info) -
+