Created
December 14, 2025 08:38
-
-
Save tufac2/fce2eecb02ffb28ee6f0db6db6a5f898 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async def kg_test(self, state: State) -> State: | |
| print("🕸️ Generando Cypher Query para Knowledge Graph...") | |
| customer_profile = state.get("customerProfile") | |
| system_template = """You are an expert assistant in creating Knowledge Graphs generating Cypher Queries. | |
| Your tasks are: | |
| 1. Identify entities and relationships and property keys from the context. | |
| 2. Context is the Customer Profile provided. | |
| 3. Generate Cypher Query to create a knowledge Graph from the identified entities, relationships, and property keys. | |
| Guidelines: | |
| - Extract all entities and relationships as possible. | |
| - ALWAYS extract a person Profession as an Entity. | |
| - ALWAYS extract a Profession's Industry as an Entity. | |
| - Be Creative. Understand hidden relationships from the network. | |
| - Read the context three times and carefully before generating Cypher Query. | |
| - Return ONLY a Cypher Query. Do not return anything else. | |
| - Do not hallucinate. | |
| - Entities to include: Person, City, Profession, Industry. | |
| Few Shot Example: | |
| Example Context: | |
| {{ | |
| "name": "Fabián Almenara Cerezo", | |
| "email": "fabian.almenara@gmail.com", | |
| "city": "granada", | |
| "customerProfile": {{ | |
| "name": "Fabián Almenara Cerezo", | |
| "industry": "Information Technology and Services", | |
| "city": "Granada", | |
| "state": "Spain", | |
| "currentPosition": "CTO Cubicup", | |
| "interests": ["Innovation", "AI", "Sales"] | |
| }} | |
| }} | |
| Answer: | |
| MERGE (Fabián_Almenara_Cerezo:Person {{name: "Fabián Almenara Cerezo", email: "fabian.almenara@gmail.com"}}) | |
| MERGE (CTO_Cubicup:Profession {{name: "CTO Cubicup"}}) | |
| MERGE (Information_Technology:Industry {{name: "Information Technology and Services"}}) | |
| MERGE (Fabián_Almenara_Cerezo)-[:HAS_PROFESSION]->(CTO_Cubicup) | |
| MERGE (CTO_Cubicup)-[:BELONGS_TO_INDUSTRY]->(Information_Technology) | |
| MERGE (Granada:City {{name: "Granada"}}) | |
| MERGE (Spain:Country {{name: "Spain"}}) | |
| MERGE (Fabián_Almenara_Cerezo)-[:LIVES_IN]->(Granada) | |
| MERGE (Fabián_Almenara_Cerezo)-[:FROM]->(Spain) | |
| """ | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", system_template), | |
| ("user", "Context: {context}") | |
| ]) | |
| chain = prompt | self.llm | |
| try: | |
| # Serializar el perfil para el contexto | |
| context_str = str(customer_profile.model_dump()) if customer_profile else "No profile data" | |
| completion = await chain.ainvoke({"context": context_str}) | |
| cypher_query = completion.content if hasattr(completion, 'content') else str(completion) | |
| # Limpiar posible markdown formatting ```cypher ... ``` | |
| cypher_query = cypher_query.replace("```cypher", "").replace("```", "").strip() | |
| print(f"✅ Cypher Query generada:\n{cypher_query}") | |
| return State({"cypherQuery": cypher_query}) | |
| except Exception as e: | |
| print(f"❌ Error generando Cypher Query: {e}") | |
| return State({"cypherQuery": ""}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment