Spaces:
Sleeping
Sleeping
Zeggai Abdellah
commited on
Commit
·
d798578
1
Parent(s):
d76ad36
update the create of the sections tools
Browse files- prepare_env.py +125 -58
prepare_env.py
CHANGED
|
@@ -197,47 +197,67 @@ def section_tool_wrapper(retriever, section_path_chunks, query):
|
|
| 197 |
def create_section_tools(embedding_function, llm):
|
| 198 |
"""Create all section-specific retrieval tools"""
|
| 199 |
|
| 200 |
-
# Define section paths
|
| 201 |
section_paths = {
|
| 202 |
-
'one': 'section_one_chunks.json',
|
| 203 |
-
'two': 'section_two_chunks.json',
|
| 204 |
-
'three': 'section_three_chunks.json',
|
| 205 |
-
'four': 'section_four_chunks.json',
|
| 206 |
-
'five': 'section_five_chunks.json',
|
| 207 |
-
'six': 'section_six_chunks.json',
|
| 208 |
-
'seven': 'section_seven_chunks.json',
|
| 209 |
-
'eight': 'section_eight_chunks.json',
|
| 210 |
-
'nine': 'section_nine_chunks.json',
|
| 211 |
-
'ten': 'section_ten_chunks.json'
|
| 212 |
}
|
| 213 |
|
| 214 |
# Create retrievers for each section
|
| 215 |
section_retrievers = {}
|
| 216 |
for section, path in section_paths.items():
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
# Create main guide retriever
|
| 222 |
guide_path = './data/Guide-pratique-de-mise-en-oeuvre-du-calendrier-national-de-vaccination-2023.json'
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
# WHO Immunization in Practice Tool
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
# General-purpose tool (entire Algerian guide)
|
| 243 |
def guide_retrieval_tool(query: str) -> str:
|
|
@@ -260,9 +280,11 @@ def create_section_tools(embedding_function, llm):
|
|
| 260 |
str: Synthesized answer from the entire national guide.
|
| 261 |
"""
|
| 262 |
if not guide_retriever:
|
| 263 |
-
return "Guide retriever not available"
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
| 266 |
|
| 267 |
def immunization_tool(query: str) -> str:
|
| 268 |
"""
|
|
@@ -281,12 +303,13 @@ def create_section_tools(embedding_function, llm):
|
|
| 281 |
str: Content from the WHO Immunization in Practice guide.
|
| 282 |
"""
|
| 283 |
if not immunization_retriever:
|
| 284 |
-
return "Immunization in Practice retriever not available"
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
|
|
|
|
| 290 |
def section_one_tool(query: str) -> str:
|
| 291 |
"""
|
| 292 |
Section 1: Programme Élargi de Vaccination (PEV)
|
|
@@ -302,8 +325,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 302 |
Returns:
|
| 303 |
str: Response from Section 1.
|
| 304 |
"""
|
| 305 |
-
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
def section_two_tool(query: str) -> str:
|
| 309 |
"""
|
|
@@ -320,8 +347,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 320 |
Returns:
|
| 321 |
str: Disease-specific content from Section 2.
|
| 322 |
"""
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
def section_three_tool(query: str) -> str:
|
| 327 |
"""
|
|
@@ -338,15 +369,19 @@ def create_section_tools(embedding_function, llm):
|
|
| 338 |
Returns:
|
| 339 |
str: Vaccine info from Section 3.
|
| 340 |
"""
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
def section_four_tool(query: str) -> str:
|
| 345 |
"""
|
| 346 |
Section 4: Rattrapage Vaccinal
|
| 347 |
|
| 348 |
Use ONLY when the question involves missed or delayed vaccinations and how to reschedule them
|
| 349 |
-
based on the child
|
| 350 |
|
| 351 |
Do NOT use for standard schedules (on-time), vaccine properties, or cold chain issues.
|
| 352 |
|
|
@@ -356,8 +391,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 356 |
Returns:
|
| 357 |
str: Catch-up guidance from Section 4.
|
| 358 |
"""
|
| 359 |
-
|
| 360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
def section_five_tool(query: str) -> str:
|
| 363 |
"""
|
|
@@ -374,8 +413,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 374 |
Returns:
|
| 375 |
str: Custom recommendations from Section 5.
|
| 376 |
"""
|
| 377 |
-
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
def section_six_tool(query: str) -> str:
|
| 381 |
"""
|
|
@@ -392,8 +435,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 392 |
Returns:
|
| 393 |
str: Cold chain instructions from Section 6.
|
| 394 |
"""
|
| 395 |
-
|
| 396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
def section_seven_tool(query: str) -> str:
|
| 399 |
"""
|
|
@@ -410,8 +457,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 410 |
Returns:
|
| 411 |
str: Best practices from Section 7.
|
| 412 |
"""
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
def section_eight_tool(query: str) -> str:
|
| 417 |
"""
|
|
@@ -428,8 +479,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 428 |
Returns:
|
| 429 |
str: Workflow and safety monitoring details from Section 8.
|
| 430 |
"""
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
def section_nine_tool(query: str) -> str:
|
| 435 |
"""
|
|
@@ -446,8 +501,12 @@ def create_section_tools(embedding_function, llm):
|
|
| 446 |
Returns:
|
| 447 |
str: Planning and stock guidance from Section 9.
|
| 448 |
"""
|
| 449 |
-
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
|
| 452 |
def section_ten_tool(query: str) -> str:
|
| 453 |
"""
|
|
@@ -464,7 +523,13 @@ def create_section_tools(embedding_function, llm):
|
|
| 464 |
Returns:
|
| 465 |
str: Public mobilization strategies from Section 10.
|
| 466 |
"""
|
| 467 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
# Create FunctionTool objects
|
| 469 |
tools = [
|
| 470 |
FunctionTool.from_defaults(name="Guide_vector_tool", fn=guide_retrieval_tool),
|
|
@@ -493,4 +558,6 @@ def prepare_environment():
|
|
| 493 |
tools = create_section_tools(embedding_function, llm)
|
| 494 |
|
| 495 |
print("Environment prepared successfully!")
|
|
|
|
|
|
|
| 496 |
return tools, llm
|
|
|
|
| 197 |
def create_section_tools(embedding_function, llm):
|
| 198 |
"""Create all section-specific retrieval tools"""
|
| 199 |
|
| 200 |
+
# Define section paths - Fixed path structure
|
| 201 |
section_paths = {
|
| 202 |
+
'one': './data/section_one_chunks.json',
|
| 203 |
+
'two': './data/section_two_chunks.json',
|
| 204 |
+
'three': './data/section_three_chunks.json',
|
| 205 |
+
'four': './data/section_four_chunks.json',
|
| 206 |
+
'five': './data/section_five_chunks.json',
|
| 207 |
+
'six': './data/section_six_chunks.json',
|
| 208 |
+
'seven': './data/section_seven_chunks.json',
|
| 209 |
+
'eight': './data/section_eight_chunks.json',
|
| 210 |
+
'nine': './data/section_nine_chunks.json',
|
| 211 |
+
'ten': './data/section_ten_chunks.json'
|
| 212 |
}
|
| 213 |
|
| 214 |
# Create retrievers for each section
|
| 215 |
section_retrievers = {}
|
| 216 |
for section, path in section_paths.items():
|
| 217 |
+
try:
|
| 218 |
+
if os.path.exists(path):
|
| 219 |
+
print(f"Creating retriever for section {section} from {path}")
|
| 220 |
+
vstore, docs = create_vectorstore_from_json(path, f"Guide_2023_{section}", embedding_function)
|
| 221 |
+
section_retrievers[section] = create_retriever(vstore, docs, llm)
|
| 222 |
+
print(f"Successfully created retriever for section {section}")
|
| 223 |
+
else:
|
| 224 |
+
print(f"Warning: File not found for section {section}: {path}")
|
| 225 |
+
section_retrievers[section] = None
|
| 226 |
+
except Exception as e:
|
| 227 |
+
print(f"Error creating retriever for section {section}: {e}")
|
| 228 |
+
section_retrievers[section] = None
|
| 229 |
|
| 230 |
# Create main guide retriever
|
| 231 |
guide_path = './data/Guide-pratique-de-mise-en-oeuvre-du-calendrier-national-de-vaccination-2023.json'
|
| 232 |
+
guide_retriever = None
|
| 233 |
+
try:
|
| 234 |
+
if os.path.exists(guide_path):
|
| 235 |
+
print("Creating main guide retriever...")
|
| 236 |
+
guide_vstore, guide_docs = create_vectorstore_from_json(guide_path, "Guide_2023_multilingual", embedding_function)
|
| 237 |
+
guide_retriever = create_retriever(guide_vstore, guide_docs, llm)
|
| 238 |
+
print("Successfully created main guide retriever")
|
| 239 |
+
else:
|
| 240 |
+
print(f"Warning: Main guide file not found: {guide_path}")
|
| 241 |
+
except Exception as e:
|
| 242 |
+
print(f"Error creating main guide retriever: {e}")
|
| 243 |
|
| 244 |
# WHO Immunization in Practice Tool
|
| 245 |
+
immunization_path = './data/Immunization in Practice_WHO_eng_2015.json'
|
| 246 |
+
immunization_retriever = None
|
| 247 |
+
try:
|
| 248 |
+
if os.path.exists(immunization_path):
|
| 249 |
+
print("Creating immunization retriever...")
|
| 250 |
+
immunization_vstore, immunization_docs = create_vectorstore_from_json(
|
| 251 |
+
immunization_path,
|
| 252 |
+
"Immunization_in_Practice_WHO_eng_2015",
|
| 253 |
+
embedding_function
|
| 254 |
+
)
|
| 255 |
+
immunization_retriever = create_retriever(immunization_vstore, immunization_docs, llm)
|
| 256 |
+
print("Successfully created immunization retriever")
|
| 257 |
+
else:
|
| 258 |
+
print(f"Warning: Immunization file not found: {immunization_path}")
|
| 259 |
+
except Exception as e:
|
| 260 |
+
print(f"Error creating immunization retriever: {e}")
|
| 261 |
|
| 262 |
# General-purpose tool (entire Algerian guide)
|
| 263 |
def guide_retrieval_tool(query: str) -> str:
|
|
|
|
| 280 |
str: Synthesized answer from the entire national guide.
|
| 281 |
"""
|
| 282 |
if not guide_retriever:
|
| 283 |
+
return "Guide retriever not available - main guide file may be missing"
|
| 284 |
+
try:
|
| 285 |
+
return section_tool_wrapper(guide_retriever, guide_path, query)
|
| 286 |
+
except Exception as e:
|
| 287 |
+
return f"Error accessing guide retriever: {str(e)}"
|
| 288 |
|
| 289 |
def immunization_tool(query: str) -> str:
|
| 290 |
"""
|
|
|
|
| 303 |
str: Content from the WHO Immunization in Practice guide.
|
| 304 |
"""
|
| 305 |
if not immunization_retriever:
|
| 306 |
+
return "Immunization in Practice retriever not available - WHO guide file may be missing"
|
| 307 |
+
try:
|
| 308 |
+
return section_tool_wrapper(immunization_retriever, immunization_path, query)
|
| 309 |
+
except Exception as e:
|
| 310 |
+
return f"Error accessing immunization retriever: {str(e)}"
|
| 311 |
|
| 312 |
+
# Section-Specific Tools - Fixed implementation
|
| 313 |
def section_one_tool(query: str) -> str:
|
| 314 |
"""
|
| 315 |
Section 1: Programme Élargi de Vaccination (PEV)
|
|
|
|
| 325 |
Returns:
|
| 326 |
str: Response from Section 1.
|
| 327 |
"""
|
| 328 |
+
if not section_retrievers.get('one'):
|
| 329 |
+
return "Section 1 retriever not available - file may be missing"
|
| 330 |
+
try:
|
| 331 |
+
return section_tool_wrapper(section_retrievers['one'], section_paths['one'], query)
|
| 332 |
+
except Exception as e:
|
| 333 |
+
return f"Error accessing section 1: {str(e)}"
|
| 334 |
|
| 335 |
def section_two_tool(query: str) -> str:
|
| 336 |
"""
|
|
|
|
| 347 |
Returns:
|
| 348 |
str: Disease-specific content from Section 2.
|
| 349 |
"""
|
| 350 |
+
if not section_retrievers.get('two'):
|
| 351 |
+
return "Section 2 retriever not available - file may be missing"
|
| 352 |
+
try:
|
| 353 |
+
return section_tool_wrapper(section_retrievers['two'], section_paths['two'], query)
|
| 354 |
+
except Exception as e:
|
| 355 |
+
return f"Error accessing section 2: {str(e)}"
|
| 356 |
|
| 357 |
def section_three_tool(query: str) -> str:
|
| 358 |
"""
|
|
|
|
| 369 |
Returns:
|
| 370 |
str: Vaccine info from Section 3.
|
| 371 |
"""
|
| 372 |
+
if not section_retrievers.get('three'):
|
| 373 |
+
return "Section 3 retriever not available - file may be missing"
|
| 374 |
+
try:
|
| 375 |
+
return section_tool_wrapper(section_retrievers['three'], section_paths['three'], query)
|
| 376 |
+
except Exception as e:
|
| 377 |
+
return f"Error accessing section 3: {str(e)}"
|
| 378 |
|
| 379 |
def section_four_tool(query: str) -> str:
|
| 380 |
"""
|
| 381 |
Section 4: Rattrapage Vaccinal
|
| 382 |
|
| 383 |
Use ONLY when the question involves missed or delayed vaccinations and how to reschedule them
|
| 384 |
+
based on the child's current age.
|
| 385 |
|
| 386 |
Do NOT use for standard schedules (on-time), vaccine properties, or cold chain issues.
|
| 387 |
|
|
|
|
| 391 |
Returns:
|
| 392 |
str: Catch-up guidance from Section 4.
|
| 393 |
"""
|
| 394 |
+
if not section_retrievers.get('four'):
|
| 395 |
+
return "Section 4 retriever not available - file may be missing"
|
| 396 |
+
try:
|
| 397 |
+
return section_tool_wrapper(section_retrievers['four'], section_paths['four'], query)
|
| 398 |
+
except Exception as e:
|
| 399 |
+
return f"Error accessing section 4: {str(e)}"
|
| 400 |
|
| 401 |
def section_five_tool(query: str) -> str:
|
| 402 |
"""
|
|
|
|
| 413 |
Returns:
|
| 414 |
str: Custom recommendations from Section 5.
|
| 415 |
"""
|
| 416 |
+
if not section_retrievers.get('five'):
|
| 417 |
+
return "Section 5 retriever not available - file may be missing"
|
| 418 |
+
try:
|
| 419 |
+
return section_tool_wrapper(section_retrievers['five'], section_paths['five'], query)
|
| 420 |
+
except Exception as e:
|
| 421 |
+
return f"Error accessing section 5: {str(e)}"
|
| 422 |
|
| 423 |
def section_six_tool(query: str) -> str:
|
| 424 |
"""
|
|
|
|
| 435 |
Returns:
|
| 436 |
str: Cold chain instructions from Section 6.
|
| 437 |
"""
|
| 438 |
+
if not section_retrievers.get('six'):
|
| 439 |
+
return "Section 6 retriever not available - file may be missing"
|
| 440 |
+
try:
|
| 441 |
+
return section_tool_wrapper(section_retrievers['six'], section_paths['six'], query)
|
| 442 |
+
except Exception as e:
|
| 443 |
+
return f"Error accessing section 6: {str(e)}"
|
| 444 |
|
| 445 |
def section_seven_tool(query: str) -> str:
|
| 446 |
"""
|
|
|
|
| 457 |
Returns:
|
| 458 |
str: Best practices from Section 7.
|
| 459 |
"""
|
| 460 |
+
if not section_retrievers.get('seven'):
|
| 461 |
+
return "Section 7 retriever not available - file may be missing"
|
| 462 |
+
try:
|
| 463 |
+
return section_tool_wrapper(section_retrievers['seven'], section_paths['seven'], query)
|
| 464 |
+
except Exception as e:
|
| 465 |
+
return f"Error accessing section 7: {str(e)}"
|
| 466 |
|
| 467 |
def section_eight_tool(query: str) -> str:
|
| 468 |
"""
|
|
|
|
| 479 |
Returns:
|
| 480 |
str: Workflow and safety monitoring details from Section 8.
|
| 481 |
"""
|
| 482 |
+
if not section_retrievers.get('eight'):
|
| 483 |
+
return "Section 8 retriever not available - file may be missing"
|
| 484 |
+
try:
|
| 485 |
+
return section_tool_wrapper(section_retrievers['eight'], section_paths['eight'], query)
|
| 486 |
+
except Exception as e:
|
| 487 |
+
return f"Error accessing section 8: {str(e)}"
|
| 488 |
|
| 489 |
def section_nine_tool(query: str) -> str:
|
| 490 |
"""
|
|
|
|
| 501 |
Returns:
|
| 502 |
str: Planning and stock guidance from Section 9.
|
| 503 |
"""
|
| 504 |
+
if not section_retrievers.get('nine'):
|
| 505 |
+
return "Section 9 retriever not available - file may be missing"
|
| 506 |
+
try:
|
| 507 |
+
return section_tool_wrapper(section_retrievers['nine'], section_paths['nine'], query)
|
| 508 |
+
except Exception as e:
|
| 509 |
+
return f"Error accessing section 9: {str(e)}"
|
| 510 |
|
| 511 |
def section_ten_tool(query: str) -> str:
|
| 512 |
"""
|
|
|
|
| 523 |
Returns:
|
| 524 |
str: Public mobilization strategies from Section 10.
|
| 525 |
"""
|
| 526 |
+
if not section_retrievers.get('ten'):
|
| 527 |
+
return "Section 10 retriever not available - file may be missing"
|
| 528 |
+
try:
|
| 529 |
+
return section_tool_wrapper(section_retrievers['ten'], section_paths['ten'], query)
|
| 530 |
+
except Exception as e:
|
| 531 |
+
return f"Error accessing section 10: {str(e)}"
|
| 532 |
+
|
| 533 |
# Create FunctionTool objects
|
| 534 |
tools = [
|
| 535 |
FunctionTool.from_defaults(name="Guide_vector_tool", fn=guide_retrieval_tool),
|
|
|
|
| 558 |
tools = create_section_tools(embedding_function, llm)
|
| 559 |
|
| 560 |
print("Environment prepared successfully!")
|
| 561 |
+
print(f"Created {len(tools)} tools")
|
| 562 |
+
|
| 563 |
return tools, llm
|