@@ -464,42 +464,48 @@ def get_closest_mappings(
464464 # Process the response objects into Mapping or MappingResult objects
465465 for o in response .objects :
466466 # Calculate similarity based on distance if similarities are requested
467- if o .metadata .distance :
468- similarity = 1 - o .metadata .distance
469- if o .references :
470- # Extract concept and terminology data
471- concept_data = o .references ["hasConcept" ].objects [0 ]
472- terminology_data = concept_data .references ["hasTerminology" ].objects [0 ]
473- terminology = Terminology (
474- name = str (terminology_data .properties ["name" ]), id = str (terminology_data .uuid )
475- )
476- concept = Concept (
477- terminology = terminology ,
478- pref_label = str (concept_data .properties ["prefLabel" ]),
479- concept_identifier = str (concept_data .properties ["conceptID" ]),
480- id = str (concept_data .uuid ),
481- )
467+ if similarities :
468+ dist = getattr (getattr (o , "metadata" , None ), "distance" , None )
469+ if dist is not None :
470+ similarity = 1.0 - dist
471+
472+ # Get references with a defensive approach
473+ concept = None
474+ if getattr (o , "references" , None ):
475+ has_concept = o .references .get ("hasConcept" )
476+ if has_concept and getattr (has_concept , "objects" , None ):
477+ concept_data = has_concept .objects [0 ]
478+ term_ref = getattr (concept_data , "references" , None )
479+ has_term = term_ref .get ("hasTerminology" ) if term_ref else None
480+ if has_term and getattr (has_term , "objects" , None ):
481+ terminology_data = has_term .objects [0 ]
482+ terminology = Terminology (
483+ name = str (terminology_data .properties ["name" ]), id = str (terminology_data .uuid )
484+ )
485+ concept = Concept (
486+ terminology = terminology ,
487+ pref_label = str (concept_data .properties ["prefLabel" ]),
488+ concept_identifier = str (concept_data .properties ["conceptID" ]),
489+ id = str (concept_data .uuid ),
490+ )
482491
483492 text = str (o .properties ["text" ])
484493 # o.vector is a dictionary with varying key
485- embedding = next (iter (o . vector .values ()), None )
494+ obj_embedding = next (iter (getattr ( o , " vector" , {}) .values ()), None )
486495
487496 # Create Mapping objects and append them to the list
488497 if not self .use_weaviate_vectorizer :
489498 mapping = Mapping (
490499 concept = concept ,
491500 text = text ,
492- embedding = embedding ,
501+ embedding = obj_embedding ,
493502 sentence_embedder = str (o .properties ["hasSentenceEmbedder" ]),
494503 )
495504 else :
496- mapping = Mapping (concept = concept , text = text , embedding = embedding )
505+ mapping = Mapping (concept = concept , text = text , embedding = obj_embedding )
497506
498507 # Append MappingResult if similarities is True, else just Mapping
499- if similarities :
500- mappings .append (MappingResult (mapping , similarity ))
501- else :
502- mappings .append (mapping )
508+ mappings .append (MappingResult (mapping , similarity ) if similarities else mapping )
503509 except Exception as e :
504510 raise RuntimeError (f"Failed to fetch closest mappings: { e } " )
505511 return mappings
0 commit comments