How idea-finai/tog-2 works
<explanation>
1. Project Type and Purpose
- This is a research‐oriented full‐stack Python application that performs knowledge‐guided, retrieval‐augmented LLM reasoning (Think‐on‐Graph 2.0).
- It consists of three logical layers:
a. Data and knowledge‐graph setup (Wikidata submodule + datasets)
b. Retrieval + reasoning pipeline (ToG-2 folder)
c. Evaluation suite (eval folder)
2. Top‐Level Components
- CLI Orchestrator (main_tog2.py)
- Named Entity Recognition (ner.py) via Azure API
- Retrieval Engine (search.py) using embedding models & BM25
- Graph Client (client.py + server_urls.txt) communicating with a local Wikidata HTTP service
- Prompt Library (prompt_list.py) for exploration & reasoning stages
- LLM Interface (OpenAI or local LLM calls within utils.py)
- Result Serializer (search.save_2_jsonl_simplier → JSONL output)
- Evaluation Script (eval/eval.py + utils.py) computing Exact Match
- Knowledge‐Graph Stack (Wikidata/simple_wikidata_db):
• Preprocessing stage (preprocess_dump.py + worker/writer/reader)
• Index building (db_deploy/build_index.py via shell scripts)
• Local HTTP server (db_deploy/server.py)
3. Relationships & Data Flow
a. User invokes main_tog2.py with CLI flags →
b. NER Module: sends text to Azure, tags entities →
c. Retrieval Module: for each entity/query, calls embedding/BM25 search →
d. Graph Client: for retrieval results, issues HTTP queries to the local Wikidata service →
e. Search Graph: builds exploration trees to given depth/width, prunes relations →
f. Prompt Generator: builds prompts from prompt_list.py →
g. LLM Module: sends prompts to OpenAI/gpt‐API or local LLM →
h. Aggregator: collects reasoning outputs, applies self-consistency threshold →
i. Serializer: writes predictions to JSONL →
j. Eval Script: consumes JSONL + ground‐truth data → calculates Exact Match.
4. Architectural Patterns & Key Technologies
- Modular pipeline: clear separation of concerns for NER, retrieval, graph search, prompting, LLM calls, and evaluation.
- Microservice for knowledge graph: local HTTP service exposing KG index.
- Asynchronous Preprocessing: reader, worker, writer processes for dump preprocessing.
- Shell‐scripted orchestration (build_index.sh, start_server.sh).
- Core libraries & frameworks: Python 3, OpenAI API, Azure Cognitive Services, embedding models (bge, minilm, colbert), BM25, JSON.
5. Diagramming Guidelines
a. Components to include:
- User/CLI
- NER Service (Azure)
- Retrieval Engine (embedding + BM25)
- KG HTTP Service (local Wikidata)
- Graph Search & Pruning Module
- Prompt Library
- LLM Interface (OpenAI/local)
- Output Serializer
- Eval Script
- Preprocessing & Index Builder (Wikidata submodule)
b. Interactions: draw arrows to show data flow (raw input → NER → retrieval → KG queries → prompts → LLM → output → eval).
c. Color coding/shapes:
- Blue boxes for internal Python modules
- Green boxes for external services (Azure, OpenAI, local KG HTTP)
- Orange boxes for data stores (datasets, index files, JSONL outputs)
d. Labels: clearly label each arrow with the type of data/request (e.g., “HTTP GET entity facts,” “embedding vector query,” “LLM prompt”).
6. Tailoring to This Project
- Emphasize the two‐stage LLM reasoning (exploration vs. reasoning) orchestrated by prompt_list.py and utils.py.
- Show how the Wikidata submodule must be initialized/preprocessed before calling main_tog2.py (link build_index → start_server → client.py).
- Highlight CLI flags controlling width, depth, temperature, pruning options.
- For the evaluation component, show how eval.py consumes both the output JSONL and the original data files in data/.
7. Essential Elements
- Use directional arrows for request/response flows.
- Distinct shapes for scripts vs. long‐running services vs. one‐off preprocessors.
- A legend explaining colors/shapes.
- Detail all major modules—avoid aggregating too much; each file group (NER, retrieval, graph client, LLM) merits its own box.
By following these guidelines, the resulting system design diagram will accurately reflect the modular architecture of Think‐on‐Graph 2.0, clearly show data and control flows, and highlight the interplay between in‐house components and external services.
</explanation>