-
Notifications
You must be signed in to change notification settings - Fork 6
✨(ai) add message reply generation tool #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MathisRouget
wants to merge
1
commit into
suitenumerique:main
Choose a base branch
from
Nicolas-Barriere:ai-response-generation-pr-base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| { | ||
| "en-us": { | ||
| "summary_query": "You are an intelligent assistant that summarizes email threads. You should summarize the content of the conversation in one or two lines maximum WITHOUT stating 'Summary:'. If important links appear in the emails, you must mention them clearly in Markdown format and integrate them into the summary. If there are no important links, the summary should not contain any information about links. \n Here is the conversation:\n\n{messages}\n\n. Summarize the above emails in Markdown in the language '{language}' :", | ||
| "autolabels_query": "Current date and time: {date_time}\nEmail or email thread: {messages}\nList of existing labels: {labels}\nFor this email or email thread, you must check for each label whether it is relevant to assign it.\nYou should assign a label only if it is truly relevant!\nRead the description of each label carefully to strictly follow the criteria provided by the user.\nIf no label is relevant, you must not assign any.\n\nYour response must be in the following format:\n[\"LABEL1\", \"LABEL2\", ...]\nNO additional text like 'Here is the list of relevant labels:'.\nIf no label is relevant, you must return an empty list: [] with no other comment !! Your reponse must always be only a list (than can be empty). No explanation or additional text should be included than the list itself." | ||
| "autolabels_query": "Current date and time: {date_time}\nEmail or email thread: {messages}\nList of existing labels: {labels}\nFor this email or email thread, you must check for each label whether it is relevant to assign it.\nYou should assign a label only if it is truly relevant!\nRead the description of each label carefully to strictly follow the criteria provided by the user.\nIf no label is relevant, you must not assign any.\n\nYour response must be in the following format:\n[\"LABEL1\", \"LABEL2\", ...]\nNO additional text like 'Here is the list of relevant labels:'.\nIf no label is relevant, you must return an empty list: [] with no other comment !! Your reponse must always be only a list (than can be empty). No explanation or additional text should be included than the list itself.", | ||
| "new_message_generation_query": "You are an assistant integrated into the email writing area of an email client. THE EXPECTED RESPONSE IS AN EMAIL BODY IN THE LANGUAGE {language}. Your response should ONLY be the body of the message (do not mention the subject or recipients) DO NOT put quotes around your response. If your response contains fields to be filled by the user, MAKE THEM ALL IN BOLD MARKDOWN AND IN BRACKETS so that the user notices them (e.g., **[Your Name]**, **[Date]**, **[Signature]**, **[Number of Users]**). Here is an example of generation: 'User prompt: 'Request an API key for Mistral'. Your response: 'Hello,\nCould you please provide me with an API key for Mistral?\nBest regards,\n[Signature]' Here is the current email draft:\n\n{draft}\n\n. Here is the user prompt : {user_prompt}\nIf the draft does not contain a signature and only in this case, then sign with {name}. NEVER CHANGE ANYTHING ELSE IN THE DRAFT OTHER THAN WHAT THE USER REQUESTS!!!", | ||
| "thread_response_generation_query": "You are an assistant integrated into the email writing area of an email client. THE EXPECTED RESPONSE IS AN EMAIL BODY IN THE LANGUAGE {language}. Your response should ONLY be the body of the message (do not mention the subject or recipients) DO NOT put quotes around your response. If your response contains fields to be filled by the user, MAKE THEM ALL IN BOLD MARKDOWN AND IN BRACKETS so that the user notices them (e.g., **[Your Name]**, **[Date]**, **[Signature]**, **[Number of Users]**). Here is an example of generation: 'User prompt: 'Request an API key for Mistral'. Previous messages: 'Hello Thomas,\nI recommend using Mistral for AI.\nBest regards,\nNicolas' User prompt: 'request the key'. Your response: Hello Nicolas,\nThank you for your response, could you please provide me with an API key for Mistral?\nBest regards,\nThomas' Your response: 'Hello,\nCould you please provide me with an API key for Mistral?\nBest regards,\n[Signature]' Here is the current email draft:\n\n{draft}\n\n. Here are the previous messages in the thread:\n\n{messages}\n\n. Here is the user prompt : {user_prompt}\nIf the draft does not contain a signature and only in this case, then sign with {name}. NEVER CHANGE ANYTHING ELSE IN THE DRAFT OTHER THAN WHAT THE USER REQUESTS!!!" | ||
| }, | ||
| "fr-fr": { | ||
| "summary_query": "Tu es un assistant intelligent qui résume des boucles de mails. Tu dois résumer le contenu de la conversation en une ou deux lignes maximum SANS préciser 'Résumé:'. Si des liens importants apparaissent dans les emails, tu dois les mentionner dans le résumé de façon claire en Markdown et les intégrer au résumé. Si les liens ne sont pas importants ou qu'il n'y en a pas, le résumé ne doit pas contenir d'information à ce sujet ni même le mentionner. Voici la conversation:\n\n{messages}\n\n. Résumé des emails ci-dessus en Markdown dans la langue '{language}' :", | ||
| "autolabels_query": "Date et heure actuelle: {date_time}\nEmail ou conversation d'emails: {messages}\nListe des labels existants: {labels}\nA partir de ce mail ou de cette conversation d'emails, tu dois regarder pour chaque label s'il est pertinent de l'assigner à ce mail ou cette conversation.\nTu ne dois assigner un label que s'il est réellement pertinent!\nLis bien la description de chaque label pour bien respecter les critères renseignés par l'utilisateur.\nSi aucun label n'est pertinent, tu ne dois en assigner aucun.\n\nTa réponse doit être au format suivant:\n[\"LABEL1\", \"LABEL2\", ...]\nSANS aucun texte supplémentaire du style \"Voici la liste des labels pertinents:\".\nSi aucun label n'est pertinent, tu dois renvoyer une liste vide: [] sans aucun autre commentaire !! Ta réponse doit dans tous les cas être uniquement une liste (éventuellement vide)" | ||
| "autolabels_query": "Date et heure actuelle: {date_time}\nEmail ou conversation d'emails: {messages}\nListe des labels existants: {labels}\nA partir de ce mail ou de cette conversation d'emails, tu dois regarder pour chaque label s'il est pertinent de l'assigner à ce mail ou cette conversation.\nTu ne dois assigner un label que s'il est réellement pertinent!\nLis bien la description de chaque label pour bien respecter les critères renseignés par l'utilisateur.\nSi aucun label n'est pertinent, tu ne dois en assigner aucun.\n\nTa réponse doit être au format suivant:\n[\"LABEL1\", \"LABEL2\", ...]\nSANS aucun texte supplémentaire du style \"Voici la liste des labels pertinents:\".\nSi aucun label n'est pertinent, tu dois renvoyer une liste vide: [] sans aucun autre commentaire !! Ta réponse doit dans tous les cas être uniquement une liste (éventuellement vide)", | ||
| "new_message_generation_query": "Tu es un assistant intégré dans la zone d'écriture des mails dans une boîte mail. LA REPONSE ATTENDUE EST UN CORPS DE MAIL dans la langue {language}. Ta réponse doit être UNIQUEMENT le corps du message (ne mentionne pas l'objet ni les destinataires) NE mets PAS de guillemets autour de ta réponse. Si ta réponse contient des champs à remplir par l'utilisateur, METS LES TOUS EN GRAS MARKDOWN ET ENTRE CROCHETS pour que l'utilisateur le remarque. (ex : **[Votre nom]**, **[Date]**, **[Signature]**, **[Nombre d'utilisateurs]**). Voici un exemple de génération : 'Prompt de l'utilisateur : 'Demande une clé API pour Mistral'. Ta réponse : 'Bonjour,\nPourriez-vous me fournir une clé API pour Mistral ?\nBien à vous,\n[Signature]' Voici le brouillon de mail actuel :\n\n{draft}\n\n. Voici le prompt de l'utilisateur : {user_prompt}\nSi le brouillon ne contient pas de signature et uniquement dans ce cas, alors signe avec {name}. NE CHANGE JAMAIS RIEN D'AUTRE PAR RAPPORT AU BROUILLON QUE CE QUE L'UTILISATEUR TE DEMANDE !!!", | ||
| "thread_response_generation_query": "Tu es un assistant intégré dans la zone d'écriture des mails dans une boîte mail. LA REPONSE ATTENDUE EST UN CORPS DE MAIL dans la langue {language}. Ta réponse doit être UNIQUEMENT le corps du message (ne mentionne pas l'objet ni les destinataires) NE mets PAS de guillemets autour de ta réponse. Si ta réponse contient des champs à remplir par l'utilisateur, METS LES TOUS EN GRAS MARKDOWN ET ENTRE CROCHETS pour que l'utilisateur le remarque. (ex : **[Votre nom]**, **[Date]**, **[Signature]**, **[Nombre d'utilisateurs]**). Voici un exemple de génération : 'Prompt de l'utilisateur : 'Demande une clé API pour Mistral'. Messages précédents: 'Bonjour Thomas,\nJe vous conseille d'utiliser Mistral pour l'IA.\nBien à vous,\nNicolas' Prompt de l'utilisateur : 'demande la clé'. Ta réponse : Bonjour Nicolas,\nMerci pour votre réponse, pourriez-vous me fournir une clé API pour Mistral ?\nBien à vous,\nThomas' Ta réponse : 'Bonjour,\nPourriez-vous me fournir une clé API pour Mistral ?\nBien à vous,\n[Signature]' Voici le brouillon de mail actuel :\n\n{draft}\n\n. Voici les anciens messages du thread : \n\n{messages}\n\n. Voici le prompt de l'utilisateur : {user_prompt}\nSi le brouillon ne contient pas de signature et uniquement dans ce cas, alors signe avec {name}. NE CHANGE JAMAIS RIEN D'AUTRE PAR RAPPORT AU BROUILLON QUE CE QUE L'UTILISATEUR TE DEMANDE !!!" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from django.conf import settings | ||
| from django.utils import translation | ||
|
|
||
| from core.ai.utils import get_messages_from_thread, load_ai_prompts | ||
| from core.models import Thread | ||
| from core.services.ai_service import AIService | ||
|
|
||
|
|
||
| def generate_new_message(draft: str, user_prompt: str, name: str) -> str: | ||
| """Generates a new mail using the AI model base on user prompt.""" | ||
|
|
||
| # Determine the active or fallback language | ||
| active_language = translation.get_language() or settings.LANGUAGE_CODE | ||
|
|
||
| # Get the prompt for the active language | ||
| prompts = load_ai_prompts() | ||
| prompt_template = prompts.get(active_language) | ||
| prompt_query = prompt_template["new_message_generation_query"] | ||
| prompt = prompt_query.format( | ||
| draft=draft, language=active_language, user_prompt=user_prompt, name=name | ||
| ) | ||
|
|
||
| answer = AIService().call_ai_api(prompt) | ||
|
|
||
| return answer | ||
|
|
||
|
|
||
| def generate_reply_message(draft: str, thread: Thread, user_prompt: str) -> str: | ||
| """Generates a reply message using the AI model based on the thread context and user prompt.""" | ||
|
|
||
| # Determine the active or fallback language | ||
| active_language = translation.get_language() or settings.LANGUAGE_CODE | ||
|
|
||
| # Extract messages from the thread | ||
| messages = get_messages_from_thread(thread) | ||
| messages_as_text = "\n\n".join([message.get_as_text() for message in messages]) | ||
|
|
||
| # Get the prompt for the active language | ||
| prompts = load_ai_prompts() | ||
| prompt_template = prompts.get(active_language) | ||
| prompt_query = prompt_template["reply_message_generation_query"] | ||
| prompt = prompt_query.format( | ||
| draft=draft, | ||
| messages=messages_as_text, | ||
| language=active_language, | ||
| user_prompt=user_prompt, | ||
| ) | ||
|
|
||
| answer = AIService().call_ai_api(prompt) | ||
|
|
||
| return answer | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code references 'reply_message_generation_query' but the JSON file defines 'thread_response_generation_query'. This will cause a KeyError at runtime when trying to generate reply messages.