1- # tools.py
21import os
32import requests
43import ast
1110from langchain_core .tools import tool
1211from langchain_tavily import TavilySearch
1312
14- # --- Configuration et Initialisation ---
13+ # Configuration et Initialisation
1514load_dotenv ()
1615OPENWEATHER_API_KEY = os .getenv ("OPENWEATHER_API_KEY" )
1716CALENDAR_FILE = "./data/calendar.json"
1817
19- # --- Outil 1 : Calculatrice Sécurisée ---
18+ # Outil 1 : Calculatrice Sécurisée
2019
2120# Opérateurs autorisés pour l'évaluation sécurisée
2221ALLOWED_OPERATORS = {
@@ -55,17 +54,22 @@ def calculate_financial_operation(expression: str) -> str:
5554 return safe_eval (expression )
5655
5756
58- # --- Outil 2 : Recherche Web (Tavily) ---
57+ # Outil 2 : Recherche Web (Tavily)
5958
6059# TavilySearchResults est déjà une classe Tool/Runnable
61- web_search_tool = TavilySearch (
62- name = "recherche_web_actualites" ,
63- description = "Utilisez cet outil pour trouver des informations externes, des actualités financières récentes, ou des définitions qui ne sont pas dans les documents internes." ,
64- max_results = 3
65- )
60+ tavily_search_instance = TavilySearch (max_results = 3 )
61+ @tool
62+ def recherche_web_actualites (query : str ) -> str :
63+ """
64+ Utilisez cet outil pour trouver des informations externes...
65+ """
66+ try :
67+ return tavily_search_instance .invoke (query )
68+ except Exception as e :
69+ return f"Erreur lors de la recherche web (Tavily): { e } "
6670
6771
68- # --- Outil 3 : Météo (OpenWeatherMap) ---
72+ # Outil 3 : Météo (OpenWeatherMap)
6973
7074@tool
7175def get_weather_for_city (city : str ) -> str :
@@ -92,7 +96,7 @@ def get_weather_for_city(city: str) -> str:
9296 return "Erreur de connexion à l'API météo. Veuillez réessayer."
9397
9498
95- # --- Outil 4 : Calendrier / Todo List Locale ---
99+ # Outil 4 : Calendrier /
96100
97101@tool
98102def read_calendar (query : str ) -> str :
@@ -122,12 +126,12 @@ def read_calendar(query: str) -> str:
122126 return f"Erreur lors de la lecture du calendrier: { e } "
123127
124128
125- # --- Liste des Outils Exportée ---
129+ # Liste des Outils Exportée
126130
127131# NOTE : La recherche web est ajoutée directement car elle est déjà une classe Tool
128132EXTERNAL_TOOLS = [
129133 calculate_financial_operation ,
130- web_search_tool ,
134+ recherche_web_actualites ,
131135 get_weather_for_city ,
132136 read_calendar
133137]
0 commit comments