| 63 | return [] |
| 64 | |
| 65 | def extract_edges(data: dict) -> list: |
| 66 | try: |
| 67 | response = client.chat.completions.create( |
| 68 | messages=[ |
| 69 | { |
| 70 | "role": "system", |
| 71 | "content": """ |
| 72 | You are an AI assistant that extracts new relationships (edges) between nodes. Your task is to identify connections between the given nodes based on their content. |
| 73 | |
| 74 | Return the extracted edges in the following JSON format: |
| 75 | { |
| 76 | "edges": [ |
| 77 | { |
| 78 | "from": "node_id_1", |
| 79 | "to": "node_id_2", |
| 80 | "label": "RELATIONSHIP" |
| 81 | } |
| 82 | ] |
| 83 | } |
| 84 | |
| 85 | Rules: |
| 86 | - Only create edges between nodes that have a clear relationship based on their content |
| 87 | - The label should be a short, ALL-CAPS description of the relationship |
| 88 | - Only create edges that are not already in the current edges |
| 89 | - Maximum 5 edges per analysis |
| 90 | - Use the node IDs exactly as provided in the input |
| 91 | """ |
| 92 | }, |
| 93 | { |
| 94 | "role": "user", |
| 95 | "content": f""" |
| 96 | New nodes: |
| 97 | {json.dumps(data['newNodes'], indent=2)} |
| 98 | |
| 99 | Current edges: |
| 100 | {json.dumps(data['currentEdges'], indent=2)} |
| 101 | |
| 102 | Create edges between these nodes if meaningful relationships exist. |
| 103 | """ |
| 104 | } |
| 105 | ], |
| 106 | model="gpt-4o-mini", |
| 107 | response_format={ "type": "json_object" } |
| 108 | ) |
| 109 | |
| 110 | edges = json.loads(response.choices[0].message.content)['edges'] |
| 111 | return edges |
| 112 | except Exception as e: |
| 113 | logging.error(f"Error extracting edges: {str(e)}") |
| 114 | return [] |