Improved prompting by a lot! Error rate is closer to zero
This commit is contained in:
parent
4d5f4e0438
commit
c4052464c0
61
app.py
61
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")
|
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
|
Formats the message to send to chatgpt
|
||||||
|
|
||||||
@ -32,12 +32,7 @@ def format_message(message: str) -> list:
|
|||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
return [
|
return [
|
||||||
{
|
{"role": "system", "content": role},
|
||||||
"role": "system",
|
|
||||||
"content": (
|
|
||||||
"You are a very knowledgeable person with random knowledge for everything the humanity has ever done"
|
|
||||||
),
|
|
||||||
},
|
|
||||||
{"role": "user", "content": message},
|
{"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 = "<html>" + match[1]
|
||||||
html_match = html.split("</html>")
|
html_match = html.split("</html>")
|
||||||
possible_html = html_match[0] + "</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
|
return possible_html
|
||||||
|
|
||||||
|
|
||||||
@ -90,7 +85,8 @@ def get_random_topic() -> str:
|
|||||||
messages=format_message(
|
messages=format_message(
|
||||||
"Give me a random list of 10 topics to talk about in a comma separated list. Each topic should be less "
|
"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 "
|
"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}")
|
logging.info(f"The topics list is: {topics}")
|
||||||
topic = random.choice(topics)
|
topic = random.choice(topics)
|
||||||
|
|
||||||
return topic.strip()
|
return topic.strip().capitalize()
|
||||||
|
|
||||||
except openai.error.RateLimitError:
|
except openai.error.RateLimitError:
|
||||||
return get_random_topic()
|
return get_random_topic()
|
||||||
@ -125,7 +121,10 @@ def generate_topic_image(topic: str) -> str:
|
|||||||
try:
|
try:
|
||||||
completion = openai.ChatCompletion.create(
|
completion = openai.ChatCompletion.create(
|
||||||
model="gpt-3.5-turbo",
|
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
|
dalle_prompt = completion.choices[0].message.content
|
||||||
|
|
||||||
@ -137,7 +136,7 @@ def generate_topic_image(topic: str) -> str:
|
|||||||
generate_topic_image(topic)
|
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
|
Gets the response from chatgpt
|
||||||
|
|
||||||
@ -162,15 +161,16 @@ def get_chatgpt_html_response(message: str, count: int = 0) -> str:
|
|||||||
completion = openai.ChatCompletion.create(
|
completion = openai.ChatCompletion.create(
|
||||||
model="gpt-3.5-turbo",
|
model="gpt-3.5-turbo",
|
||||||
messages=format_message(
|
messages=format_message(
|
||||||
(
|
f"""
|
||||||
f"Give me information about {message}. Now, given this HTML: \n {prompt_html_template} \n replace "
|
You are going to add information to this: {html}.
|
||||||
"(topic) with the name of the selected topic and (topic_info) with 5 parragraphs talking about the "
|
- Add 5 parragraphs talking about {message} in the "information-container" div.
|
||||||
"topic. Replace (related_links) with 5 links related to the topic. For these links, the href "
|
- Add 5 links for related topics in the "related-topics-container" div. The href should be
|
||||||
"should be '/infinite?topic=(slugified_related_topic_name)' and the text should be the name of the "
|
"/infinite?topic=(slugified_related_topic_name)" and the text should be "(related_topic_name).
|
||||||
"related topic. The name of these topics should not be more than 5 words. Finally, replace "
|
- Add 5 links for new random topics not related to {message} in the "new-topics-container" div. The href
|
||||||
"(new_topics) with a 5 links with completely new and random topics to talk about. These links "
|
should be the same as before.
|
||||||
"should follow the same rules as before. Do not replace (image_source). Respond only the code"
|
- Only respond with the HTML code
|
||||||
)
|
""",
|
||||||
|
"You are a very skilled web developer with expertise in HTML.",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
except openai.error.RateLimitError:
|
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}")
|
logging.info(f"The image is {image}")
|
||||||
|
|
||||||
html_with_image = re.sub(pattern, f'src="{image}"', html)
|
complete_html = re.sub(pattern, f'src="{image}"', html)
|
||||||
return html_with_image
|
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("/")
|
@app.route("/")
|
||||||
@ -200,19 +205,21 @@ def index():
|
|||||||
@app.route("/start")
|
@app.route("/start")
|
||||||
def start():
|
def start():
|
||||||
random_topic = get_random_topic()
|
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")
|
@app.route("/infinite")
|
||||||
def topic():
|
def topic():
|
||||||
topic = request.args.get("topic", None)
|
topic = request.args.get("topic", None) or request.args.get("topics", None)
|
||||||
if not topic:
|
if not topic:
|
||||||
return redirect("/start")
|
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:
|
if len(unslugified_topic.split()) > 5:
|
||||||
logging.info(f"The topic is too long: {unslugified_topic}")
|
logging.info(f"The topic is too long: {unslugified_topic}")
|
||||||
return "Error! Your topic should be < 5 words"
|
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)
|
||||||
|
7
htmls.py
7
htmls.py
@ -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
|
<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>
|
</body>
|
||||||
|
<footer>
|
||||||
|
<i>
|
||||||
|
<b>Remember this is autogenerated by ChatGPT so it is VERY slow. Be patient!</b>
|
||||||
|
</i>
|
||||||
|
</footer>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -30,7 +35,7 @@ prompt_html_template = """
|
|||||||
(topic_info)
|
(topic_info)
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="bottom-container">
|
<div class="related-topics-container">
|
||||||
<h2>Related Topics:</h2>
|
<h2>Related Topics:</h2>
|
||||||
<ul>
|
<ul>
|
||||||
(related_links)
|
(related_links)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user