Improved prompting by a lot! Error rate is closer to zero

This commit is contained in:
Roger Gonzalez 2023-05-14 10:45:43 -03:00
parent 4d5f4e0438
commit c4052464c0
Signed by: rogs
GPG Key ID: C7ECE9C6C36EC2E6
2 changed files with 40 additions and 28 deletions

61
app.py
View File

@ -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 = "<html>" + match[1]
html_match = html.split("</html>")
possible_html = html_match[0] + "</html>"
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("</html>")[0]
return_html += (
"<footer><i><b>Remember this is autogenerated by ChatGPT so it is VERY slow. Be patient!</b></i></footer></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)

View File

@ -11,6 +11,11 @@ index_html = """
<p>Want to kmow more? <a target="_blank" href="https://gitlab.com/rogs/the-infinite-website">https://gitlab.com/rogs/the-infinite-website</a></p> # noqa E501
</body>
<footer>
<i>
<b>Remember this is autogenerated by ChatGPT so it is VERY slow. Be patient!</b>
</i>
</footer>
</html>
"""
@ -30,7 +35,7 @@ prompt_html_template = """
(topic_info)
</div>
<div class="bottom-container">
<div class="related-topics-container">
<h2>Related Topics:</h2>
<ul>
(related_links)