code
class Entities(BaseModel): """Identifying information about entities.""" names: list[str] = Field( ..., description=( "All the person, organization, or business entities that " "appear in the text" ) ) prompt = ChatPromptTemplate.from_messages( [ ( "system", "You are extracting organization and person entities from the text.", ), ( "human", "Use the given format to extract information from the following " "input: {question}", ), ] ) def generate_full_text_query(input: str) -> str: words = [el for el in remove_lucene_chars(input).split() if el] if not words: return "" full_text_query = " AND ".join([f"{word}~2" for word in words]) print(f"Generated Query: {full_text_query}") return full_text_query.strip() # Full-text index query def graph_retriever(question: str) -> str: """ Collects the neighborhood of entities mentioned in the question """ result = "" # Detect entities through the entity chain and pass them to the graph query entities = entity_chain.invoke(question) for entity in entities.names: response = graph.query( """ CALL db.index.fulltext.queryNodes('fulltext_entity_id', $query, {limit:2}) YIELD node, score CALL { WITH node MATCH (node)-[r:!MENTIONS]->(neighbor) RETURN node.id + ' - ' + type(r) + ' -> ' + neighbor.id AS output UNION ALL WITH node MATCH (node)<-[r:!MENTIONS]-(neighbor) RETURN neighbor.id + ' - ' + type(r) + ' -> ' + node.id AS output } RETURN output LIMIT 50 """, {"query": entity}, ) result += "\n".join([el['output'] for el in response]) return result
Authors
Sources
- RAG Using Knowledge Graph: Mastering Advanced Techniques procogia.com via serper
Referenced by nodes (1)
- information extraction concept