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genai.configure(api_key="AIzaSyCdIOCNM7NmCeu5TS30TxZx6ldN4gSDoTI")
|
| 4 |
+
def call_ai_api(prompt):
|
| 5 |
+
# Set up the model
|
| 6 |
+
generation_config = {
|
| 7 |
+
"temperature": 1,
|
| 8 |
+
"top_p": 0.95,
|
| 9 |
+
"max_output_tokens": 5000000,
|
| 10 |
+
}
|
| 11 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest",
|
| 12 |
+
generation_config=generation_config)
|
| 13 |
+
response = model.generate_content(prompt)
|
| 14 |
+
return response.text
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def generer_prompt_seo(titre_article, mot_cle_principal, ton_cible, mots_cles_associes):
|
| 18 |
+
mots_cles_lexicaux = mots_cles_associes
|
| 19 |
+
prompt = f"""
|
| 20 |
+
CONTEXTE:
|
| 21 |
+
Veuillez écrire un article de 1500 - 2000 mots. N'hésitez pas à prendre votre temps pour réfléchir à votre réponse j'ai besoin d'un long article.
|
| 22 |
+
Vous êtes un rédacteur SEO chargé de composer un article de blog optimisé pour le SEO sur le sujet suivant.
|
| 23 |
+
Voici les informations et directives à suivre :
|
| 24 |
+
|
| 25 |
+
Titre de l'article : {titre_article}
|
| 26 |
+
Mot-clé principal : {mot_cle_principal}
|
| 27 |
+
Ton et/ou public cible : {ton_cible}
|
| 28 |
+
Mots-clés lexicaux à inclure : {mots_cles_lexicaux}
|
| 29 |
+
|
| 30 |
+
INSTRUCTIONS:
|
| 31 |
+
NOMBRE DE MOTS : 1000 à 1500
|
| 32 |
+
### Règles de rédaction :
|
| 33 |
+
N'hésitez pas à prendre votre temps pour réfléchir à votre réponse j'ai besoin d'un long article.
|
| 34 |
+
1. Structure de l'article avec des titres hiérarchisés.
|
| 35 |
+
2. Optimisation du mot-clé principal (densité d'environ 1,5%, utilisation dans le titre SEO, méta description, et répartition uniforme).
|
| 36 |
+
3. Inclusion de sous-titres et de mots-clés LSI pour enrichir le contenu sans bourrage de mots-clés.
|
| 37 |
+
4. Titres et méta descriptions attractifs avec appels à l'action.
|
| 38 |
+
5. Ton informatif et engageant, avec une variété de vocabulaire.
|
| 39 |
+
6. Introduction et conclusion engageantes avec le mot-clé principal.
|
| 40 |
+
7. Section FAQ avec trois questions/réponses courtes.
|
| 41 |
+
8. Au moins un lien externe vers une source crédible.
|
| 42 |
+
N'hésitez pas à prendre votre temps pour réfléchir à votre réponse j'ai besoin d'un long article.
|
| 43 |
+
Assurez que l'article soit lisible, bien structuré et visuellement attrayant.
|
| 44 |
+
"""
|
| 45 |
+
return prompt
|
| 46 |
+
def generate_seo_prompt(article_title, main_keyword, target_tone, associated_keywords):
|
| 47 |
+
lexical_keywords = associated_keywords
|
| 48 |
+
prompt = f"""
|
| 49 |
+
CONTEXT:
|
| 50 |
+
Please write an article of 1500 - 2000 words. Feel free to take your time to think about your response, I need a long article.
|
| 51 |
+
You are an SEO writer tasked with composing an SEO-optimized blog article on the following topic.
|
| 52 |
+
Here are the information and guidelines to follow:
|
| 53 |
+
|
| 54 |
+
Article title: {article_title}
|
| 55 |
+
Main keyword: {main_keyword}
|
| 56 |
+
Target tone and/or audience: {target_tone}
|
| 57 |
+
Lexical keywords to include: {lexical_keywords}
|
| 58 |
+
|
| 59 |
+
INSTRUCTIONS:
|
| 60 |
+
WORD COUNT: 1000 to 1500
|
| 61 |
+
### Writing rules:
|
| 62 |
+
Feel free to take your time to think about your response, I need a long article.
|
| 63 |
+
1. Structure the article with hierarchical headings.
|
| 64 |
+
2. Optimize the main keyword (density of about 1.5%, use in the SEO title, meta description, and evenly distributed).
|
| 65 |
+
3. Include subheadings and LSI keywords to enrich the content without keyword stuffing.
|
| 66 |
+
4. Attractive titles and meta descriptions with calls to action.
|
| 67 |
+
5. Informative and engaging tone, with a variety of vocabulary.
|
| 68 |
+
6. Engaging introduction and conclusion with the main keyword.
|
| 69 |
+
7. FAQ section with three short questions/answers.
|
| 70 |
+
8. At least one external link to a credible source.
|
| 71 |
+
Feel free to take your time to think about your response, I need a long article.
|
| 72 |
+
Ensure that the article is readable, well-structured, and visually appealing.
|
| 73 |
+
"""
|
| 74 |
+
return prompt
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def create_pipeline_fr(titre_article, mot_cle_principal, ton_cible):
|
| 78 |
+
# Initial AI API call
|
| 79 |
+
mots_cles_champ_lexical=generer_mots(titre_article,mot_cle_principal)
|
| 80 |
+
pp=generer_prompt_seo(titre_article, mot_cle_principal, ton_cible, mots_cles_champ_lexical)
|
| 81 |
+
rep = call_ai_api(pp)
|
| 82 |
+
# Second AI API call with additional elaboration request
|
| 83 |
+
prompt2 = f"D'après le prompt {pp}, j'ai eu le résultat suivant : {rep}. Élaborer plus que ça."
|
| 84 |
+
rep2 = call_ai_api(prompt2)
|
| 85 |
+
|
| 86 |
+
# Third AI API call with further instructions
|
| 87 |
+
prompt3 = (f"D'après le prompt {pp}, j'ai eu le résultat suivant : {rep}. Élaborer plus que ça. {rep2} "
|
| 88 |
+
"Le texte est toujours beaucoup trop court. Aussi, il n'y a aucune gestion des titres, le premier est un H2, "
|
| 89 |
+
"et les sous-titres sont balisés comme des paragraphes. Concernant le lien tout à la fin, il faudrait qu'il soit intégré au texte. "
|
| 90 |
+
"Dernier point, la meta description ne doit pas apparaître quand on demande un article.")
|
| 91 |
+
rep3 = call_ai_api(prompt3)
|
| 92 |
+
|
| 93 |
+
# Fourth AI API call to fix title issues and meta description
|
| 94 |
+
prompt4 = ("Fix ça : Mauvaise gestion des titres, le premier titre doit être un H1. Le mot-clé principal n'est pas assez utilisé, il faut une densité de 1,5%. "
|
| 95 |
+
"Le titre ne doit contenir une majuscule que sur le premier mot de la phrase, pas sur les autres. "
|
| 96 |
+
"La meta description ne doit pas être rédigée. " + rep3)
|
| 97 |
+
rep4 = call_ai_api(prompt4)
|
| 98 |
+
|
| 99 |
+
# Fifth AI API call to fix keyword density and internal link
|
| 100 |
+
prompt5 = (f"Fix ça - Le mot-clé principal : {mot_cle_principal} n'est pas assez utilisé, il faut une densité de 1,5% environ. "
|
| 101 |
+
"Ajoute un lien à l'intérieur d'un texte, et rends le FAQ après la conclusion. et n'ajoute pas de remarque a la fin je veux que dans la sortie que du texte " + rep4)
|
| 102 |
+
rep5 = call_ai_api(prompt5)
|
| 103 |
+
|
| 104 |
+
# Sixth AI API call to ensure the text length
|
| 105 |
+
prompt6 = ("Trop court, seulement j'aurai besoin de 1500 mots minimum attendu. "
|
| 106 |
+
"Laisse la même structure de texte, ajoute juste du contenu. " + rep5)
|
| 107 |
+
rep6 = call_ai_api(prompt6)
|
| 108 |
+
|
| 109 |
+
return rep6
|
| 110 |
+
|
| 111 |
+
def create_pipeline(article_title, main_keyword, target_tone):
|
| 112 |
+
# Initial AI API call
|
| 113 |
+
lexical_field_keywords=generate_words(article_title, main_keyword)
|
| 114 |
+
pp = generate_seo_prompt(article_title, main_keyword, target_tone, lexical_field_keywords)
|
| 115 |
+
|
| 116 |
+
rep = call_ai_api(pp)
|
| 117 |
+
|
| 118 |
+
# Second AI API call with additional elaboration request
|
| 119 |
+
prompt2 = f"Based on the prompt {pp}, I got the following result: {rep}. Elaborate further."
|
| 120 |
+
rep2 = call_ai_api(prompt2)
|
| 121 |
+
|
| 122 |
+
# Third AI API call with further instructions
|
| 123 |
+
prompt3 = (f"Based on the prompt {pp}, I got the following result: {rep}. Elaborate further. {rep2} "
|
| 124 |
+
"The text is still way too short. Also, there is no title management, the first title is an H2, "
|
| 125 |
+
"and the subtitles are marked as paragraphs. Regarding the link at the end, it should be integrated into the text. "
|
| 126 |
+
"Lastly, the meta description should not appear when requesting an article.")
|
| 127 |
+
rep3 = call_ai_api(prompt3)
|
| 128 |
+
|
| 129 |
+
# Fourth AI API call to fix title issues and meta description
|
| 130 |
+
prompt4 = ("Fix this: Poor title management, the first title must be an H1. The main keyword is not used enough, it should have a density of 1.5%. "
|
| 131 |
+
"The title should only have a capital letter on the first word of the sentence, not on the others. "
|
| 132 |
+
"The meta description should not be written. " + rep3)
|
| 133 |
+
rep4 = call_ai_api(prompt4)
|
| 134 |
+
|
| 135 |
+
# Fifth AI API call to fix keyword density and internal link
|
| 136 |
+
prompt5 = (f"Fix this - The main keyword: {main_keyword} is not used enough, it should have a density of about 1.5%. "
|
| 137 |
+
"Add a link inside the text, and place the FAQ after the conclusion. " + rep4)
|
| 138 |
+
rep5 = call_ai_api(prompt5)
|
| 139 |
+
|
| 140 |
+
# Sixth AI API call to ensure the text length
|
| 141 |
+
prompt6 = ("Too short, I need a minimum of 1500 words expected. "
|
| 142 |
+
"Keep the same text structure, just add more content. " + rep5)
|
| 143 |
+
rep6 = call_ai_api(prompt6)
|
| 144 |
+
|
| 145 |
+
return rep6
|
| 146 |
+
|
| 147 |
+
def generer_mots(titre_article,mot_cle_principal):
|
| 148 |
+
words=call_ai_api("Generes moi des mots-clés similaires a partir de ca ( SEULEMENT DES MOTS OU GROUPE DE MOTS séparés par virgules ) :"+titre_article+" et "+mot_cle_principal)
|
| 149 |
+
return words
|
| 150 |
+
|
| 151 |
+
def generate_words(article_title, main_keyword):
|
| 152 |
+
words = call_ai_api("Generate similar keywords from these (ONLY WORDS OR GROUPS OF WORDS separated by commas): " + article_title + " and " + main_keyword)
|
| 153 |
+
return words
|