Add an idea node to the graph and connect it to similar ideas. Args: idea (Dict[str, Any]): Idea dictionary containing: - id (str): Unique identifier - name (str): Idea name/title - description (str): Detailed description
(self, idea: Dict[str, Any])
| 91 | |
| 92 | |
| 93 | def add_idea_node(self, idea: Dict[str, Any]) -> None: |
| 94 | """ |
| 95 | Add an idea node to the graph and connect it to similar ideas. |
| 96 | |
| 97 | Args: |
| 98 | idea (Dict[str, Any]): Idea dictionary containing: |
| 99 | - id (str): Unique identifier |
| 100 | - name (str): Idea name/title |
| 101 | - description (str): Detailed description |
| 102 | - Other metadata fields |
| 103 | """ |
| 104 | if not CHROMA_AVAILABLE or self.collection is None: |
| 105 | logger.warning("ChromaDB not available, skipping idea addition") |
| 106 | return |
| 107 | |
| 108 | idea_id = idea.get('id', idea.get('name', str(len(self.graph.nodes)))) |
| 109 | idea_name = idea.get('name', '') |
| 110 | idea_description = idea.get('description', '') |
| 111 | |
| 112 | # Create a searchable text representation |
| 113 | idea_text = f"{idea_name}: {idea_description}" |
| 114 | |
| 115 | # Check if node already exists |
| 116 | if idea_id in self.graph: |
| 117 | logger.info(f"Idea node {idea_id} already exists, skipping") |
| 118 | return |
| 119 | |
| 120 | # Add idea to vector store |
| 121 | try: |
| 122 | self.collection.add( |
| 123 | documents=[idea_text], |
| 124 | ids=[idea_id], |
| 125 | metadatas=[{ |
| 126 | "name": idea_name, |
| 127 | "description": idea_description, |
| 128 | "id": idea_id |
| 129 | }] |
| 130 | ) |
| 131 | except Exception as e: |
| 132 | logger.error(f"Failed to add idea to ChromaDB: {e}") |
| 133 | return |
| 134 | |
| 135 | # Add node to graph |
| 136 | self.graph.add_node(idea_id, **idea) |
| 137 | |
| 138 | # Find similar ideas and create edges |
| 139 | try: |
| 140 | results = self.collection.query( |
| 141 | query_texts=[idea_text], |
| 142 | n_results=min(10, len(self.graph.nodes)) |
| 143 | ) |
| 144 | |
| 145 | if results and results['ids'] and len(results['ids']) > 0: |
| 146 | neighbor_ids = results['ids'][0] |
| 147 | distances = results['distances'][0] |
| 148 | |
| 149 | for neighbor_id, distance in zip(neighbor_ids, distances): |
| 150 | # Skip self |