Forum in maintenance, we will back soon π
Trying to built an ai blog write
Hello Hasan, I'm following you for the last few years and I have watched almost all the latest videos you have published especially on AI that's the reason I purchased this course. This course is amazing no doubt but you should have covered in more detail like I'm new to python but there is only few lessons of python due to which it becomes hard for me to create my own scripts and use AI.
My issue for now is I have create a script using chatgpt it is executing properly but I'm not getting a desired output. What I'm trying to do is, I am creating a python script to write an SEO optimized articles of up to 2000 words script is working but I'm not getting the correct output formatting.
I have tried to do it using separate prompt as well but I'm unable to generate a good prompt which fulfils my need, and there is no blog writing prompt in your prompt collection as well.Β
So, I need your help in make this work. Please update a new prompt in prompt library or just help me with below code to make it work. My goal is to create a SEO optimized article to publish on my website and make it available as a tool (saas platform)
Below is the code I'm trying with: (Spent almost 5 hours without success)
import helpers import llm def generate_blog_post(word_count, selected_model, topic): # Calculate the estimated cost based on the selected model and word count estimated_cost = helpers.estimate_input_cost(selected_model, word_count) # Generate the blog post with the specified word count and topic prompt = f"# {topic}\n\n" blog_post = "" # Set the maximum tokens for the blog post to generate max_tokens = word_count * 10 # Loop until the desired word count is reached or exceeded while len(blog_post.split()) < word_count: # Generate a response from the chat-based GPT-3 model response = llm.llm_generate_text(prompt, service="OpenAI", model=selected_model) # Add the response to the blog post blog_post += response # Update the prompt with the latest generated text prompt += response # Break the loop if the maximum tokens limit is reached if len(prompt.split()) >= max_tokens: break # Format the blog post with headings, lists, tables, bold text, italic text, and other requested elements blog_post = format_blog_post(blog_post) return blog_post, estimated_cost def count_words(text): words = text.split() return len(words) def format_blog_post(blog_post): # Format headings blog_post = blog_post.replace("###", "<h3>").replace("\n\n", "</h3>\n\n") blog_post = blog_post.replace("##", "<h2>").replace("\n\n", "</h2>\n\n") blog_post = blog_post.replace("#", "<h1>").replace("\n\n", "</h1>\n\n") # Format paragraphs with titles blog_post = blog_post.replace("Title:", "<h4>").replace("\n\n", "</h4>\n\n") # Format introduction part blog_post = blog_post.replace("Introduction:", "<p><strong>Introduction:</strong></p>\n\n") # Format bold text blog_post = blog_post.replace("**", "<strong>").replace("**", "</strong>") # Format italic text blog_post = blog_post.replace("*", "<em>").replace("*", "</em>") # Format underline text blog_post = blog_post.replace("_", "<u>").replace("_", "</u>") # Format lists blog_post = blog_post.replace("- ", "<li>").replace("\n\n", "</li>\n\n") blog_post = f"<ul>\n\n{blog_post}</ul>\n\n" # Format tables blog_post = blog_post.replace("Comparison Table:", "<table>\n").replace("|", "</td><td>").replace("\n\n", "</td></tr>\n") blog_post = blog_post.replace("\n", "<tr><td>").replace("</td></tr></td></tr>", "</td></tr>\n").replace("</td></tr></table>", "</td></tr></table>\n") # Format SUMMARY blog_post = blog_post.replace("SUMMARY:", "<p><strong>SUMMARY:</strong></p>\n\n") # Format most asked questions blog_post = blog_post.replace("Most Asked Questions:", "<p><strong>Most Asked Questions:</strong></p>\n\n") # Add additional formatting options here # Improve formatting and appeal blog_post = add_line_breaks(blog_post) blog_post = add_paragraph_tags(blog_post) blog_post = add_blockquotes(blog_post) return blog_post def add_line_breaks(blog_post): # Add line breaks between paragraphs blog_post = blog_post.replace("</p>\n\n<p>", "</p>\n<p>") blog_post = blog_post.replace("</h1>\n\n<p>", "</h1>\n<p>") blog_post = blog_post.replace("</h2>\n\n<p>", "</h2>\n<p>") blog_post = blog_post.replace("</h3>\n\n<p>", "</h3>\n<p>") return blog_post def add_paragraph_tags(blog_post): # Add paragraph tags to unformatted paragraphs paragraphs = blog_post.split("</p>\n<p>") formatted_paragraphs = [f"<p>{p}</p>" for p in paragraphs] blog_post = "</p>\n<p>".join(formatted_paragraphs) return blog_post def add_blockquotes(blog_post): # Add blockquote tags to quotes blog_post = blog_post.replace("\"", """) blog_post = blog_post.replace(""", "<blockquote>"") blog_post = blog_post.replace("</p>\n<p>"", ""</blockquote>\n\n<p>") blog_post = blog_post.replace("</p>\n\n<blockquote>", "<blockquote>") blog_post = blog_post.replace("</blockquote>\n\n<p>", "</blockquote>\n<p>") return blog_post # Set the desired word count and selected model word_count = 500 selected_model = "gpt-3.5-turbo" # Get user input for the topic topic = input("Please enter your topic: ") # Generate the blog post with the specified word count, selected model, and topic blog_post, cost = generate_blog_post(word_count, selected_model, topic) # Count the number of words in the generated blog post word_count = count_words(blog_post) # Print the generated blog post, the word count, and the estimated cost print("Generated Blog Post:") print(blog_post) print("Word Count:", word_count) print("Estimated Cost: $", cost)
Β Many a times, I have tried changing the format option in this script and tried creating a prompt as well nothing working for me. Hope you could help here.Β
And last but not least Many Thanks for this Course. I have learned a lot and it's totally worth it. I hope to hear back from you soon.Β
Hi friend, I will try my best to update with more python lectures.
What prompt are you using here?