Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions graphiti_core/prompts/dedupe_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@


class EdgeDuplicate(BaseModel):
reasoning: str = Field(
default='',
description=(
'Briefly reason about duplicates and contradictions before the final arrays. '
'For every selected fact, cite its continuous idx number.'
),
)
duplicate_facts: list[int] = Field(
...,
description='List of idx values of duplicate facts (only from EXISTING FACTS range). Empty list if none.',
Expand Down Expand Up @@ -82,6 +89,11 @@ def resolve_edge(context: dict[str, Any]) -> list[Message]:
- Return all contradicted idx values in contradicted_facts.
- If no contradictions, return an empty list for contradicted_facts.

Before returning the arrays, explain your decision in a short reasoning field. '
For every duplicate or contradiction, explicitly cite its idx number in the reasoning '
(for example, "idx 2 is contradicted because the value changed"). Then return the '
duplicate_facts and contradicted_facts arrays using those same idx values.

<EXAMPLE>
EXISTING FACT: idx=0, "Alice joined Acme Corp in 2020"
NEW FACT: "Alice joined Acme Corp in 2020"
Expand Down
25 changes: 25 additions & 0 deletions tests/prompts/test_dedupe_edges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from graphiti_core.prompts.dedupe_edges import EdgeDuplicate, resolve_edge


def test_edge_duplicate_schema_collects_reasoning_before_indices():
fields = list(EdgeDuplicate.model_fields)

assert fields[:3] == ['reasoning', 'duplicate_facts', 'contradicted_facts']
assert EdgeDuplicate(
reasoning='idx 1 contradicts the new fact',
duplicate_facts=[],
contradicted_facts=[1],
).contradicted_facts == [1]


def test_resolve_edge_prompt_requires_index_reasoning_first():
prompt = resolve_edge(
{
'existing_edges': [{'idx': 0, 'fact': 'A'}],
'edge_invalidation_candidates': [{'idx': 1, 'fact': 'B'}],
'new_edge': 'C',
}
)[-1].content

assert 'reasoning' in prompt.lower()
assert 'idx number' in prompt.lower()
Loading