Código executável demonstrando como extrair triplas de texto e realizar consultas estruturadas de múltiplos saltos (Multi-Hop QA).


🗺️ Fluxo de Processamento

flowchart LR
    TEXTO["Texto: 'Alice lidera o Time de Backend, que desenvolve a API de Pagamentos com Go.'"]
    --> EXTRACT["Extrator de Relações"]
    EXTRACT --> N1["(Alice) --[LIDERA]--> (Time de Backend)"]
    EXTRACT --> N2["(Time de Backend) --[DESENVOLVE]--> (API de Pagamentos)"]
    EXTRACT --> N3["(API de Pagamentos) --[CONSTRUÍDA_COM]--> (Go)"]
    N1 & N2 & N3 --> QUERY["Query: 'Qual linguagem o time da Alice usa?'"]
    QUERY --> GRAPH_TRAVERSAL["Navegação no Grafo: Alice -> Time Backend -> API -> Go ⚡"]

    style TEXTO fill:#ede7f6,stroke:#512da8,stroke-width:2px
    style EXTRACT fill:#fff3e0,stroke:#ff9800,stroke-width:1.5px
    style N1 fill:#e1f5fe,stroke:#03a9f4,stroke-width:1.5px
    style N2 fill:#e1f5fe,stroke:#03a9f4,stroke-width:1.5px
    style N3 fill:#e1f5fe,stroke:#03a9f4,stroke-width:1.5px
    style QUERY fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
    style GRAPH_TRAVERSAL fill:#00897b,stroke:#004d40,stroke-width:2px,color:#ffffff

📄 Código de Demonstração (knowledge_graph_demo.py)

class GrafoConhecimentoMini:
    def __init__(self):
        self.conexoes = {}

    def adicionar(self, origem, relacao, destino):
        self.conexoes.setdefault(origem, []).append((relacao, destino))

    def buscar_caminho(self, inicio, fim, visitados=None):
        if visitados is None:
            visitados = set()
        visitados.add(inicio)
        if inicio == fim:
            return [inicio]
        for rel, vizinho in self.conexoes.get(inicio, []):
            if vizinho not in visitados:
                caminho = self.buscar_caminho(vizinho, fim, visitados)
                if caminho:
                    return [f"{inicio} -({rel})-> {vizinho}"] + caminho[1:]
        return None

if __name__ == "__main__":
    kg = GrafoConhecimentoMini()
    kg.adicionar("Alice", "LIDERA", "Time Backend")
    kg.adicionar("Time Backend", "DESENVOLVE", "API Pagamentos")
    kg.adicionar("API Pagamentos", "USA_LINGUAGEM", "Golang")

    print("=== Demonstração de Grafo de Conhecimento (GraphRAG) ===")
    caminho = kg.buscar_caminho("Alice", "Golang")
    print("Caminho relacional encontrado:")
    for passo in caminho:
        print("  " + passo)

🚀 Saída Esperada no Terminal / Execução

=== Demonstração de Grafo de Conhecimento (GraphRAG) ===
Caminho relacional encontrado:
  Alice -(LIDERA)-> Time Backend
  Time Backend -(DESENVOLVE)-> API Pagamentos
  API Pagamentos -(USA_LINGUAGEM)-> Golang

🧭 Navegação Rápida

| 📖 Teoria | 📊 Slides | 🧠 Quiz | 💻 Exemplos | 🧩 Exercícios | | :— | :— | :— | :— | :— | | Ler Tópico | Ver Slides | Fazer Quiz | Ver Código | Praticar |