WIP - Code refactoring

This commit is contained in:
AjaySi
2024-04-03 22:46:17 +05:30
parent 1833a85637
commit 75e5d25981
4 changed files with 67 additions and 44 deletions

View File

@@ -48,32 +48,32 @@ def write_blog_from_keywords(search_keywords, url=None):
# logger.info/check the final blog content. # logger.info/check the final blog content.
logger.info(f"######### Blog content Google SERP research: ###########\n\n{blog_markdown_str}\n\n") logger.info(f"######### Blog content Google SERP research: ###########\n\n{blog_markdown_str}\n\n")
# Do Tavily AI research to augument the above blog. # # Do Tavily AI research to augument the above blog.
try: # try:
tavily_search_result, t_titles = do_tavily_ai_search(search_keywords) # tavily_search_result, t_titles = do_tavily_ai_search(search_keywords)
example_blog_titles.append(t_titles) # example_blog_titles.append(t_titles)
blog_markdown_str = blog_with_research(blog_markdown_str, tavily_search_result) # 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") # logger.info(f"######### Blog content after Tavily AI research: ######### \n\n{blog_markdown_str}\n\n")
except Exception as err: # except Exception as err:
logger.error(f"Failed to do Tavily AI research: {err}") # logger.error(f"Failed to do Tavily AI research: {err}")
#
try: # try:
# Do Metaphor/Exa AI search. # # Do Metaphor/Exa AI search.
metaphor_search_result, m_titles = do_metaphor_ai_research(search_keywords) # metaphor_search_result, m_titles = do_metaphor_ai_research(search_keywords)
example_blog_titles.append(m_titles) # example_blog_titles.append(m_titles)
blog_markdown_str = blog_with_research(blog_markdown_str, metaphor_search_result) # 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") # logger.info(f"######## Blog content after EXA AI research: ########## \n\n{blog_markdown_str}\n\n")
except Exception as err: # except Exception as err:
logger.error(f"Failed to do Metaphor AI search: {err}") # logger.error(f"Failed to do Metaphor AI search: {err}")
#
# Do Google trends analysis and combine with latest blog. # # Do Google trends analysis and combine with latest blog.
try: # try:
pytrends_search_result = do_google_pytrends_analysis(search_keywords) # pytrends_search_result = do_google_pytrends_analysis(search_keywords)
logger.info(f"Google Trends keywords to use in the blog: {pytrends_search_result}\n") # 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) # blog_markdown_str = blog_with_keywords(blog_markdown_str, pytrends_search_result)
except Exception as err: # except Exception as err:
logger.error(f"Failed to do Google Trends Analysis:{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") # 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. # Combine YOU.com RAG search with the latest blog content.
#you_rag_result = get_rag_results(search_keywords) #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) #blog_markdown_str = blog_with_research(blog_markdown_str, you_search_result)
#logger.info(f"Final blog content: {blog_markdown_str}") #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, blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(blog_markdown_str,
search_keywords, example_blog_titles) search_keywords, example_blog_titles)

View File

@@ -4,33 +4,39 @@ import sys
from pathlib import Path from pathlib import Path
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv(Path('../../.env')) load_dotenv(Path('../../.env'))
import configparser
from ..gpt_providers.gemini_pro_text import gemini_text_response from ..gpt_providers.gemini_pro_text import gemini_text_response
from ..gpt_providers.openai_text_gen import openai_chatgpt from ..gpt_providers.openai_text_gen import openai_chatgpt
def blog_proof_editor(blog_content, blog_keywords): def blog_proof_editor(blog_content):
""" """ Helper for blog proof reading. """
Helper for blog proof reading.
"""
gpt_provider = os.environ["GPT_PROVIDER"] 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. Your task is to rewrite my blog, by following the guidelines below.
Below are the guidelines to follow: 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 2). Vocabulary and Grammar Enhancement: Directly correct any grammatical errors and upgrade the
vocabulary for better readability. vocabulary for better readability.
3). Improve Sentence Structure: Enhance sentence construction for better clarity and flow. 3). Improve Sentence Structure: Enhance sentence construction for better clarity and conversational flow.
4). Tone and Brand Alignment: Adjust the tone, voice, personality of given content to make it unique. 4). Tone and Brand Alignment: Adjust tone, voice, personality for {config.get('blog_characteristics', 'blog_tone')} audience.
5). Optimize Content Structure: Reorganize the content for a more impactful presentation, 5). Optimize Content Structure: Reorganize content for more impactful presentation, including better paragraphing & transitions.
including better paragraphing and transitions.
6). Simplify content: Simplify concepts and replace overly complex words. Use simple english words. 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}' \n\nMy Blog: '{blog_content}'. """
My Blog: '{blog_content}'. """
if 'openai' in gpt_provider.lower(): if 'openai' in gpt_provider.lower():
try: try:

View File

@@ -1,7 +1,8 @@
import time import os
import time #IWish
import logging import logging
import openai import openai
import os import configparser
# Configure standard logging # Configure standard logging
logging.basicConfig(level=logging.INFO, format='[%(asctime)s-%(levelname)s-%(module)s-%(lineno)d]- %(message)s') 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)) @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. 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: Raises:
SystemExit: If an API error, connection error, or rate limit error occurs. 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 # Wait for 10 seconds to comply with rate limits
for _ in range(5): for _ in range(5):
time.sleep(1) time.sleep(1)

View File

@@ -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. # Length of blogs Or word count. Note: It wont be exact and depends on GPT providers and Max token count.
blog_length = 2000 blog_length = 2000
@@ -19,7 +21,7 @@ blog_demographic = "All"
blog_type = "Informational" blog_type = "Informational"
# German, Chinese, Arabic, Nepali, Hindi, Hindustani etc # 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. # Specify the output format of the blog as: HTML, markdown, plaintext. Defaults to markdown.
blog_output_format = "markdown" blog_output_format = "markdown"