Skip to content

Ajout de retry et de délai dans le LLM#7

Closed
FoulquesR wants to merge 3 commits into
mainfrom
fre/retryLLMTropdAppels
Closed

Ajout de retry et de délai dans le LLM#7
FoulquesR wants to merge 3 commits into
mainfrom
fre/retryLLMTropdAppels

Conversation

@FoulquesR

Copy link
Copy Markdown
Contributor

Contournement simple de la limitation à 100 requête par minute par l'API Albert. Modificaiton de la fonction ask_llm dans processor analyze content. Je n'ai pas modifié d'autres fonctions, car celle-ci est la fonction coeur. Elle ne levait pas d'exception, elle en lève désormais. Les exceptions sont rattrappées ensuite dans la fonction df_analyze_content.
J'ai ajouté un fichier test qui permet de tester la fonction ask_llm avec un peu plus de 100 appels à Albert large

Comment thread app/processor/analyze_content.py Outdated
continue # Réessaye

else:
raise Exception(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utiliser raise

Comment thread app/processor/analyze_content.py Outdated

return content # Succès

except Exception as e:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Récupérer le type exacte de l'exception (surement quelque chose comme openai.Exception)

Tu peux faire print(repr(e)), print(e.class)

Comment thread app/processor/analyze_content.py Outdated
):
if attempt < max_retries - 1:
wait_time = retry_delay * (attempt + 1)
print(f"Erreur 429 détectée, retry dans {wait_time:.1f}s (tentative {attempt+1}/{max_retries})")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remplacer par un logger.warning.

Comment thread app/tests/llm_tests.py Outdated
from app.ai_models.config_albert import API_KEY_ALBERT, BASE_URL_PROD


def test_rate_limiting():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mettre dans le le dossier docia/tests/data/

Comment thread app/processor/analyze_content.py Outdated
f"Erreur de rate limiting per minute détectée dans la réponse malgré {max_retries} tentatives : {str(e)[:200]}"
)

raise Exception(f"Erreur lors de l'appel au LLM : {str(e)}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utiliser raise.

"""
Script pour tester le rate limiting de l'API LLM en effectuant
plus de 100 appels par minute.
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ne pas faire un vrai appel d'api. Utiliser mock.patch pour patcher self.client.chat.completions.create et raise l'exception du rate limit.
Penser aussi à patch time.sleep pour éviter de sleep pendant le test.
Tu peux récupérer le nombre d'appels sur le mock pour savoir si ça a été appelé plusieurs fois. Tu peux aussi récupérer l'output des loggers et voir si il y a le texte du retry (https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html#accessing-captured-output-from-a-test-function)

Comment thread app/tests/llm_tests.py Outdated
)

# Histoire de contexte (environ 5000 caractères)
contexte_histoire = """

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai l'impression que c'est toujours vide ? C'est encore utilisé ?

@trecouvr

Copy link
Copy Markdown
Collaborator

Réintégrée dans #11

@trecouvr trecouvr closed this Nov 21, 2025
@FoulquesR FoulquesR deleted the fre/retryLLMTropdAppels branch December 15, 2025 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants