Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
Merge branch 'main' into reasoning
Mohd Kaif5555c2afa5ee
diff --cc semantica/reasoning/reasoner.pyindex 47f0171d,4f49b575..2b452072--- a/semantica/reasoning/reasoner.py+++ b/semantica/reasoning/reasoner.py@@@ -330,40 -330,24 +330,48 @@@ class Reasonerdef _match_pattern(self, pattern: str, fact: str, initial_bindings: Dict[str, str]) -> Optional[Dict[str, str]]:"""Match a pattern against a fact with initial bindings."""+ # Split on ?var placeholders first, then escape only the literal segments.+ # This avoids re.escape() mangling the surrounding parentheses and ?+ # before the variable substitution step.+ segments = re.split(r"(\?\w+)", pattern)+ seen_vars: set = set()+ p_regex = ""+ for seg in segments:+ if seg.startswith("?"):+ var_name = seg[1:]+ if var_name in initial_bindings:+ # Already bound — require the exact literal value+ p_regex += re.escape(initial_bindings[var_name])+ elif var_name in seen_vars:+ # Same variable used twice — use a backreference+ p_regex += f"(?P={var_name})"+ else:+ p_regex += f"(?P<{var_name}>.+?)"+ seen_vars.add(var_name)+ else:+ p_regex += re.escape(seg)+ p_regex = f"^{p_regex}$"++ # Simple regex-based matcher for patterns like "Person(?x)" and facts like "Person(John)"++ p_regex = re.escape(pattern)+ p_regex = re.sub(r"\\\?(\w+)", r"(?P<\1>.+)", p_regex)+ p_regex = f"^{p_regex}$"+try:match = re.match(p_regex, fact)if match:new_bindings = initial_bindings.copy()for var, value in match.groupdict().items():if var in new_bindings and new_bindings[var] != value:+ return None # Binding conflict+ return None # Binding conflictnew_bindings[var] = valuereturn new_bindingsexcept Exception:pass++return Nonedef _substitute(self, pattern: str, bindings: Dict[str, str]) -> str:diff --cc semantica/semantic_extract/methods.pyindex 43a9ea27,2418bb0a..d5cd1b7d--- a/semantica/semantic_extract/methods.py+++ b/semantica/semantic_extract/methods.py@@@ -1804,6 -1804,6 +1804,121 @@@ Instructions3. Use the provided entities list as a reference for subjects and objects.4. {relation_types_instruction}++Text to extract from:++{text}++Entities found in text: {entities_str}"""++++++ if not entities:++ error_msg = "No entities provided for relation extraction. Relations require existing entities."++ logger.error(error_msg)++ if not silent_fail:++ raise ProcessingError(error_msg)++ return []++++ # Pass api_key if provided in kwargs++ provider_kwargs = kwargs.copy()++++ # Check if api_key is provided but empty, or not provided at all++ if "api_key" not in provider_kwargs or not provider_kwargs["api_key"]:++ import os++ env_key = f"{provider.upper()}_API_KEY"++ api_key = os.getenv(env_key)++ if api_key:++ provider_kwargs["api_key"] = api_key++++ # Remove None/empty API key if still present to avoid provider errors++ if "api_key" in provider_kwargs and not provider_kwargs["api_key"]:++ del provider_kwargs["api_key"]++++ # 2. PROVIDER VALIDATION++ try:++ llm = create_provider(provider, model=model, **provider_kwargs)++ if not llm.is_available():++ error_msg = f"{provider} provider not available for relation extraction (key missing?)."++ logger.error(error_msg)++ if not silent_fail:++ raise ProcessingError(error_msg)++ return []++ except Exception as e:++ error_msg = f"Failed to create {provider} provider for relations: {e}"++ logger.error(error_msg)++ if not silent_fail:++ raise ProcessingError(error_msg) from e++ return []++++ # 3. TEXT LENGTH CHECK AND CHUNKING++ if max_text_length is None:++ # Default limits for chunking only - NOT for LLM generation++ max_text_length = {++ "groq": 64000,++ "openai": 64000,++ "gemini": 64000,++ "anthropic": 64000,++ "deepseek": 64000,++ }.get(provider.lower(), 32000)++++ if len(text) > max_text_length:++ logger.info(f"Text length ({len(text)}) exceeds limit for relations. Chunking...")++ return _extract_relations_chunked(++ text, entities, provider=provider, model=model,++ silent_fail=silent_fail, max_text_length=max_text_length,++ max_retries=max_retries,++ **kwargs++ )++++ original_entities = entities++ # Use a fixed internal default for prompt entity cap (do not accept overrides from kwargs)++ max_entities_prompt = 80++ prompt_entities = original_entities++ if max_entities_prompt > 0 and len(original_entities) > max_entities_prompt:++ prompt_entities = filter_entities_for_text(++ text,++ original_entities,++ max_keep=max_entities_prompt,++ )++++ entities_str = ", ".join([f"{e.text} ({e.label})" for e in prompt_entities])++++ # Use custom relation types if provided++ relation_types = kwargs.get("relation_types")++ if relation_types:++ relation_types_str = ", ".join(relation_types)++ relation_types_instruction = f"""++Preferred relation types: {relation_types_str}.++You may also use related or similar relation types if they better capture the relationship (e.g., variations, synonyms, or domain-specific relations).++If a relation doesn't fit any of the preferred types, use the most appropriate type from the preferred list or a closely related type that accurately describes the relationship."""++ else:++ relation_types_instruction = """++Extract meaningful relationships between entities. Use appropriate relation types that accurately describe how entities are connected.++Common relation types include: related_to, part_of, located_in, created_by, uses, depends_on, interacts_with, and similar variations."""++++ verbose_mode = kwargs.get("verbose", False)++ if verbose_mode:++ import sys++ print(f" [methods.extract_relations_llm] Constructing prompt for {len(prompt_entities)} entities...", flush=True, file=sys.stdout)++++ if not SCHEMAS_AVAILABLE:++ raise ImportError("Pydantic schemas not available. Install pydantic/instructor to use LLM extraction.")++++ prompt = f"""Extract relations between entities from the provided text.++Return the result as a JSON object with a "relations" key containing the list of relations.++Each relation must have 'subject', 'predicate', and 'object' fields.++++Example output (JSON format only):++{{++ "relations": [++ {{"subject": "Entity A", "predicate": "related_to", "object": "Entity B", "confidence": 0.95}},++ {{"subject": "Subject Entity", "predicate": "action_verb", "object": "Object Entity", "confidence": 0.90}}++ ]++}}++++Instructions:++1. Extract relations ONLY from the text provided below.++2. Do not include any relations from the example above.++3. Use the provided entities list as a reference for subjects and objects.++4. {relation_types_instruction}++Text to extract from:{text}Entities found in text: {entities_str}"""@@@ -1938,38 -1938,25 +2053,57 @@@ def _parse_relation_resultsubject_text = str(subject_text)object_text = str(object_text)+ # Find matching entities using hybrid similarity; fall back to a+ # synthetic entity so multi-value results are never silently dropped.+ subject_entity = match_entity(subject_text, entities)+ object_entity = match_entity(object_text, entities)++ if not subject_entity:+ subject_entity = Entity(+ text=subject_text, label="UNKNOWN",+ start_char=0, end_char=len(subject_text),+ confidence=0.8, metadata={"synthetic": True},+ )+ if not object_entity:+ object_entity = Entity(+ text=object_text, label="UNKNOWN",+ start_char=0, end_char=len(object_text),+ confidence=0.8, metadata={"synthetic": True},+ )++ relations.append(+ Relation(+ subject=subject_entity,+ predicate=item.get("predicate", "related_to"),+ object=object_entity,+ confidence=item.get("confidence", 0.9),+ context=text,+ metadata={+ "provider": provider,+ "model": model,+ "extraction_method": "llm",+ },+ )+ )+ # Find matching entities using hybrid similarity+ subject_entity = match_entity(subject_text, entities)+ object_entity = match_entity(object_text, entities)++ if subject_entity and object_entity:+ relations.append(+ Relation(+ subject=subject_entity,+ predicate=item.get("predicate", "related_to"),+ object=object_entity,+ confidence=item.get("confidence", 0.9),+ context=text,+ metadata={+ "provider": provider,+ "model": model,+ "extraction_method": "llm",+ },+ )+ )return relations