File size: 10,673 Bytes
3068aa9
 
 
 
0253392
34b88c0
3068aa9
b8de053
 
3068aa9
 
 
 
 
b8de053
3068aa9
 
b8de053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3068aa9
34b88c0
3068aa9
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8de053
3068aa9
 
34b88c0
 
 
 
 
 
b5a69c1
34b88c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3068aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
 
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
 
 
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
 
 
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
 
 
 
b8de053
3068aa9
 
 
 
 
 
 
 
 
b8de053
3068aa9
b8de053
 
 
 
 
 
 
 
3068aa9
 
b8de053
3068aa9
 
b8de053
3068aa9
 
 
b8de053
3068aa9
 
 
b8de053
3068aa9
 
b8de053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34b88c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8de053
3068aa9
 
34b88c0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

from llama_index.core.tools import FunctionTool
from duckduckgo_search import DDGS
from llama_index.llms.openai import OpenAI
from llama_index.readers.wikipedia.base import WikipediaReader
from llama_index.readers.papers import ArxivReader
from datetime import datetime
from typing import Union, List
import ast

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
# model llm 
#-------------------------------------------------------------------------------------------------------------------------------------------------------------

llm = OpenAI(model="gpt-3.5-turbo", temperature=0)


#-------------------------------------------------------------------------------------------------------------------------------------------------------------
# 🔎 Outil de recherche web avec DuckDuckGo (remplace Serper)
#--------------------------------------------------------------------------------------------------------------------------------------

def detect_answer_type(question: str) -> str:
    prompt = f"""
You are a classifier.
Your task is to determine the expected type of answer for a given question.

Possible types:
- number: a numerical value like 7 or 3.14
- list: multiple items (comma separated or array)
- date: a calendar date or year
- boolean: true/false, yes/no
- string: a word, name, or short sentence

Only return the type name. No explanation.

Question: "{question}"
Answer:
"""
    response = llm.complete(prompt).text.strip().lower()
    return response

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
# 🔎 Outil de recherche web avec DuckDuckGo / WikipediaReader / ArxivReader
#-------------------------------------------------------------------------------------------------------------------------------------------------------------

def web_search(query: str) -> str:
    """Web search with DuckDuckGo"""
    try:
        with DDGS() as ddgs:
            results = list(ddgs.text(query, max_results=5))
            
            # Formatage des résultats
            formatted_results = []
            for result in results:
                formatted_results.append(f"📄 {result.get('title', 'No title')}\n🔗 {result.get('href', 'No link')}\n📝 {result.get('body', 'No description')}\n")
            
            return "\n".join(formatted_results) if formatted_results else "Aucun résultat trouvé."
            
    except Exception as e:
        return f"Erreur lors de la recherche: {str(e)}"

# Création du tool de recherche
search_tool = FunctionTool.from_defaults(
    fn=web_search,
    name="web_search",
    description="Searches for information on the web using DuckDuckGo."
)

def load_arxiv(query: str) -> str:
    reader = ArxivReader()
    docs = reader.load_data(search_query=query)
    return "n\n".join([d.text for d in docs])

arxiv_tool = FunctionTool.from_defaults(
    fn= load_arxiv,
    name="arxiv_search",
    description="Recherche et chrge des articles scientifiques depuis arXiv"

)

def load_wikipedia(query: str) -> str:
    wiki_loader = WikipediaReader()
    docs = wiki_loader.load_data(pages=[query])
    return "\n\n".join([d.text for d in docs])

wiki_tool = FunctionTool.from_defaults(
    fn=load_wikipedia,
    name="wikipedia_search",
    description="Charge un article Wikipedia en anglais à partir d'un titre"
)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
# 🔧 Exemple d'outil custom
#-------------------------------------------------------------------------------------------------------------------------------------------------------------


def say_hello(name: str) -> str:
    """Salue une personne"""
    return f"Hello {name}, je suis ton agent GAIA."

hello_tool = FunctionTool.from_defaults(
    fn=say_hello,
    name="say_hello", 
    description="Salue une personne avec son nom"
)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 Résumé de texte
#-------------------------------------------------------------------------------------------------------------------------------------------------------------

def summarize_text(text: str) -> str:
    prompt = f"""
Summarize the following text clearly, in no more than three key points. Ignore secondary details.

Texte :
{text}

Résumé :
"""
    return llm.complete(prompt).text.strip()

summarize_tool = FunctionTool.from_defaults(
    fn=summarize_text,
    name="summarize_text",
    description="Provides a clear and concise summary of a long text."
)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 Extraction d'entités nommées
#-------------------------------------------------------------------------------------------------------------------------------------------------------------

def extract_entities(text: str) -> str:
    prompt = f"""
Read the following text and list the named entities found, categorized as: people, locations, organizations, and dates.

Texte :
{text}

Entités :
"""
    return llm.complete(prompt).text.strip()


entities_tool = FunctionTool.from_defaults(
    fn=extract_entities,
    name="extract_entities",
    description="Extracts names of people, places, dates, and organizations from a given text."
)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 Date du jour
#-------------------------------------------------------------------------------------------------------------------------------------------------------------

def get_today_date() -> str:
    return datetime.now().strftime("%Y-%m-%d")

date_tool = FunctionTool.from_defaults(
    fn=get_today_date,
    name="get_today_date",
    description="Returns the current date in the format YYYY-MM-DD."
)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 Extraction d'événements
#-------------------------------------------------------------------------------------------------------------------------------------------------------------

def extract_events(text: str) -> str:
    prompt = f"""
Identify and list the key events described in the following text. Use one short sentence per event.

Texte :
{text}

Événements :
"""
    return llm.complete(prompt).text.strip()

events_tool = FunctionTool.from_defaults(
    fn=extract_events,
    name="extract_events",
    description="Lists major events mentioned in a text."
)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 Question ciblée sur un texte
#-------------------------------------------------------------------------------------------------------------------------------------------------------------


def question_answer(text: str, question: str) -> str:
    prompt = f"""
You are an agent that answers questions based solely on a given text.

RULES:
- If the answer is clearly present in the text, provide it concisely.
- If the answer is not explicitly in the text, reply: "I don't know."
- Do NOT make assumptions.
- Do NOT repeat the question.
- Always give the shortest possible answer (e.g., a word, a date, a number).

TEXTE :
{text}

QUESTION :
{question}

RÉPONSE :
"""
    return llm.complete(prompt).text.strip()


qa_tool = FunctionTool.from_defaults(
    fn=question_answer,
    name="question_answer",
    description="Answers a specific question based on a given text"
)


def enforce_answer_format(answer: str, expected_type: str) -> Union[str, int, float, bool, List[str]]:
    prompt = f"""
You are a formatting assistant. Your task is to extract and return only the value corresponding to the expected type from the given answer.

Respond ONLY with the exact Python value (e.g., 4, "blue", true, ["a", "b"]) and nothing else.

Examples:

Expected type: number  
Answer: "There are 4 albums."  
Output: 4

Expected type: list  
Answer: "Apples, oranges, and bananas."  
Output: ["apples", "oranges", "bananas"]

Expected type: date  
Answer: "He died on September 1, 1715."  
Output: "1715-09-01"

Expected type: boolean  
Answer: "Yes, that's correct."  
Output: true

Now process the following:

Expected type: {expected_type}  
Answer: {answer}  
Output:
"""
    raw_output = llm.complete(prompt).text.strip()

    # Tentative de parsing Python natif
    try:
        return ast.literal_eval(raw_output)
    except Exception:
        return raw_output  # Fallback si ce n’est pas une valeur parseable

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 calculator
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
def calculator(expression: str) -> str:
    """Évalue une expression mathématique."""
    try:
        return str(eval(expression))
    except Exception:
        return "Erruer de calcul"

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 Question ciblée sur un texte
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
calculator_tool = FunctionTool.from_defaults(
    fn= calculator,
    name="calculator",
    description = "Effectue des calculs arithmétiques sur des expressions comme comme '2 + 3 * 4'."
)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#🔧 Question ciblée sur un texte
#-------------------------------------------------------------------------------------------------------------------------------------------------------------




# Liste exportable

TOOLS = [search_tool, hello_tool, summarize_tool, entities_tool, date_tool, events_tool, qa_tool, calculator_tool,
         wiki_tool, arxiv_tool]