From 75e5d25981eca8d90b53b5d087d7154307a428b9 Mon Sep 17 00:00:00 2001 From: AjaySi Date: Wed, 3 Apr 2024 22:46:17 +0530 Subject: [PATCH] WIP - Code refactoring --- lib/ai_writers/keywords_to_blog.py | 54 ++++++++++---------- lib/blog_postprocessing/blog_proof_reader.py | 32 +++++++----- lib/gpt_providers/openai_text_gen.py | 21 ++++++-- main_config | 4 +- 4 files changed, 67 insertions(+), 44 deletions(-) diff --git a/lib/ai_writers/keywords_to_blog.py b/lib/ai_writers/keywords_to_blog.py index 77772000..4b25d56d 100644 --- a/lib/ai_writers/keywords_to_blog.py +++ b/lib/ai_writers/keywords_to_blog.py @@ -48,32 +48,32 @@ def write_blog_from_keywords(search_keywords, url=None): # logger.info/check the final blog content. logger.info(f"######### Blog content Google SERP research: ###########\n\n{blog_markdown_str}\n\n") - # Do Tavily AI research to augument the above blog. - try: - tavily_search_result, t_titles = do_tavily_ai_search(search_keywords) - example_blog_titles.append(t_titles) - blog_markdown_str = blog_with_research(blog_markdown_str, tavily_search_result) - logger.info(f"######### Blog content after Tavily AI research: ######### \n\n{blog_markdown_str}\n\n") - except Exception as err: - logger.error(f"Failed to do Tavily AI research: {err}") - - try: - # Do Metaphor/Exa AI search. - metaphor_search_result, m_titles = do_metaphor_ai_research(search_keywords) - example_blog_titles.append(m_titles) - blog_markdown_str = blog_with_research(blog_markdown_str, metaphor_search_result) - logger.info(f"######## Blog content after EXA AI research: ########## \n\n{blog_markdown_str}\n\n") - except Exception as err: - logger.error(f"Failed to do Metaphor AI search: {err}") - - # Do Google trends analysis and combine with latest blog. - try: - pytrends_search_result = do_google_pytrends_analysis(search_keywords) - logger.info(f"Google Trends keywords to use in the blog: {pytrends_search_result}\n") - blog_markdown_str = blog_with_keywords(blog_markdown_str, pytrends_search_result) - except Exception as err: - logger.error(f"Failed to do Google Trends Analysis:{err}") - logger.info(f"########### Blog Content After Google Trends Analysis:######### \n {blog_markdown_str}\n\n") +# # Do Tavily AI research to augument the above blog. +# try: +# tavily_search_result, t_titles = do_tavily_ai_search(search_keywords) +# example_blog_titles.append(t_titles) +# blog_markdown_str = blog_with_research(blog_markdown_str, tavily_search_result) +# logger.info(f"######### Blog content after Tavily AI research: ######### \n\n{blog_markdown_str}\n\n") +# except Exception as err: +# logger.error(f"Failed to do Tavily AI research: {err}") +# +# try: +# # Do Metaphor/Exa AI search. +# metaphor_search_result, m_titles = do_metaphor_ai_research(search_keywords) +# example_blog_titles.append(m_titles) +# blog_markdown_str = blog_with_research(blog_markdown_str, metaphor_search_result) +# logger.info(f"######## Blog content after EXA AI research: ########## \n\n{blog_markdown_str}\n\n") +# except Exception as err: +# logger.error(f"Failed to do Metaphor AI search: {err}") +# +# # Do Google trends analysis and combine with latest blog. +# try: +# pytrends_search_result = do_google_pytrends_analysis(search_keywords) +# logger.info(f"Google Trends keywords to use in the blog: {pytrends_search_result}\n") +# blog_markdown_str = blog_with_keywords(blog_markdown_str, pytrends_search_result) +# except Exception as err: +# logger.error(f"Failed to do Google Trends Analysis:{err}") +# logger.info(f"########### Blog Content After Google Trends Analysis:######### \n {blog_markdown_str}\n\n") # Combine YOU.com RAG search with the latest blog content. #you_rag_result = get_rag_results(search_keywords) @@ -81,7 +81,7 @@ def write_blog_from_keywords(search_keywords, url=None): #blog_markdown_str = blog_with_research(blog_markdown_str, you_search_result) #logger.info(f"Final blog content: {blog_markdown_str}") - blog_markdown_str = blog_proof_editor(blog_markdown_str, search_keywords) + blog_markdown_str = blog_proof_editor(blog_markdown_str) blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(blog_markdown_str, search_keywords, example_blog_titles) diff --git a/lib/blog_postprocessing/blog_proof_reader.py b/lib/blog_postprocessing/blog_proof_reader.py index 87151294..45d175f7 100644 --- a/lib/blog_postprocessing/blog_proof_reader.py +++ b/lib/blog_postprocessing/blog_proof_reader.py @@ -4,33 +4,39 @@ import sys from pathlib import Path from dotenv import load_dotenv load_dotenv(Path('../../.env')) +import configparser from ..gpt_providers.gemini_pro_text import gemini_text_response from ..gpt_providers.openai_text_gen import openai_chatgpt -def blog_proof_editor(blog_content, blog_keywords): - """ - Helper for blog proof reading. - """ +def blog_proof_editor(blog_content): + """ Helper for blog proof reading. """ + gpt_provider = os.environ["GPT_PROVIDER"] - prompt = f"""As an expert copywriter, I will provide you with 'my blog' and its 'main keywords'. + try: + config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'main_config')) + config = configparser.ConfigParser() + #config = configparser.RawConfigParser() + config.read(config_path, encoding='utf-8-sig') + except Exception as err: + print(f"ProofReader: Failed to read values from config: {err}") + + prompt = f"""As an expert content writer and editor, I will provide you with 'my blog' content. Your task is to rewrite my blog, by following the guidelines below. Below are the guidelines to follow: - 1). Ensure Originality: Edit any sections that lack originality, replacing them with unique and creative content. + 1). You must respond in {config.get('blog_characteristics', 'blog_language')} language. 2). Vocabulary and Grammar Enhancement: Directly correct any grammatical errors and upgrade the vocabulary for better readability. - 3). Improve Sentence Structure: Enhance sentence construction for better clarity and flow. - 4). Tone and Brand Alignment: Adjust the tone, voice, personality of given content to make it unique. - 5). Optimize Content Structure: Reorganize the content for a more impactful presentation, - including better paragraphing and transitions. + 3). Improve Sentence Structure: Enhance sentence construction for better clarity and conversational flow. + 4). Tone and Brand Alignment: Adjust tone, voice, personality for {config.get('blog_characteristics', 'blog_tone')} audience. + 5). Optimize Content Structure: Reorganize content for more impactful presentation, including better paragraphing & transitions. 6). Simplify content: Simplify concepts and replace overly complex words. Use simple english words. - 7). Refine Overall Structure: Make structural changes to improve the overall impact of the content. + 7). Make sure your response content length is of {config.get('blog_characteristics', 'blog_length')} words. - \n\nMain keywords: '{blog_keywords}' - My Blog: '{blog_content}'. """ + \n\nMy Blog: '{blog_content}'. """ if 'openai' in gpt_provider.lower(): try: diff --git a/lib/gpt_providers/openai_text_gen.py b/lib/gpt_providers/openai_text_gen.py index 7cdb1823..000c7ab4 100644 --- a/lib/gpt_providers/openai_text_gen.py +++ b/lib/gpt_providers/openai_text_gen.py @@ -1,7 +1,8 @@ -import time +import os +import time #IWish import logging import openai -import os +import configparser # Configure standard logging logging.basicConfig(level=logging.INFO, format='[%(asctime)s-%(levelname)s-%(module)s-%(lineno)d]- %(message)s') @@ -15,7 +16,7 @@ from tenacity import ( @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) -def openai_chatgpt(prompt, model="gpt-3.5-turbo-0125", temperature=0.7, max_tokens=4096, top_p=0.5, n=1): +def openai_chatgpt(prompt): """ Wrapper function for OpenAI's ChatGPT completion. @@ -33,6 +34,20 @@ def openai_chatgpt(prompt, model="gpt-3.5-turbo-0125", temperature=0.7, max_toke Raises: SystemExit: If an API error, connection error, or rate limit error occurs. """ + try: + config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'main_config')) + + config = configparser.ConfigParser() + config.read(config_path) + + model = config.get('model') + temperature = config.getfloat('temperature') + max_tokens = config.getint('max_tokens') + top_p = config.getfloat('top_p') + n = config.getint('n') + except Exception as err: + logger.error(f"Unable to read Openai parameters from config file:{err}") + # Wait for 10 seconds to comply with rate limits for _ in range(5): time.sleep(1) diff --git a/main_config b/main_config index 242bb635..28083fd8 100644 --- a/main_config +++ b/main_config @@ -6,6 +6,8 @@ # ################################################### +[blog_characteristics] + # Length of blogs Or word count. Note: It wont be exact and depends on GPT providers and Max token count. blog_length = 2000 @@ -19,7 +21,7 @@ blog_demographic = "All" blog_type = "Informational" # German, Chinese, Arabic, Nepali, Hindi, Hindustani etc -blog_language = "English" +blog_language = "Spanish" # Specify the output format of the blog as: HTML, markdown, plaintext. Defaults to markdown. blog_output_format = "markdown"