### Clone and Install Project Dependencies
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Commands to initialize the repository and install required Python packages.
```bash
git clone https://github.com/fair2wise/FAIRtoWISE-FORUM-AI
```
```bash
cd FAIRtoWISE-FORUM-AI
pip install -r requirements.txt
```
--------------------------------
### Start FastAPI Server with Specific KG
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Launch the KG-RAG LLM chat as a FastAPI server, configured to use a specified knowledge graph JSON file.
```bash
python kg_rag_ollama.py \
--graph storage/kg/opv_expert_graph.json \
--api
```
--------------------------------
### Start KG-RAG FastAPI Server
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Starts the FastAPI server for the KG-RAG system, making it accessible as an Ollama-compatible proxy. This is useful for integrating with front-end applications like OpenWebUI.
```bash
# Start the FastAPI server
python -m modules.kg_rag_ollama_api --api --graph ./storage/kg/matkg_graph.json
# Server starts at http://0.0.0.0:11435
# Configure OpenWebUI:
# Admin Settings → Connections → Ollama API → http://localhost:11435
```
--------------------------------
### KG-RAG CLI Usage Examples
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Demonstrates various command-line options for the KG-RAG system, including interactive mode, one-shot questions, specifying a knowledge graph, and running competency evaluations.
```bash
# Interactive mode
python -m modules.kg_rag_ollama_api
# Ask (exit to quit): What are the key factors affecting OPV efficiency?
# One-shot question
python -m modules.kg_rag_ollama_api --question "How does annealing influence phase separation in P3HT:PCBM?"
# Use specific knowledge graph
python -m modules.kg_rag_ollama_api \
--graph ./storage/kg/matkg_qwen3_235b_580papers.json \
--question "What characterization techniques are used for morphology analysis?"
# Run competency question evaluation (baseline vs KG-RAG)
python -m modules.kg_rag_ollama_api --competency
# Results saved to: ./storage/competency_questions/competency_results_qwen3_235b_580papers.json
```
--------------------------------
### Start Open WebUI Server
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Launch the Open WebUI server. Ensure the Ollama API URL is set to http://localhost:11435 in Admin Settings to connect with the kg_rag_ollama.py FastAPI server.
```bash
open-webui serve
```
--------------------------------
### Start KG-RAG LLM Chat FastAPI Server
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Launch the kg_rag_ollama.py script as a FastAPI server, making it accessible via HTTP. The server will run on http://0.0.0.0:11435.
```bash
python kg_rag_ollama.py --api
```
--------------------------------
### D3.js Node Dragging Behavior
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/storage/kg/view_kg.html
Implements drag-and-drop functionality for nodes in a D3.js force-directed graph. When dragging starts, node positions are fixed; when dragging ends, fixed positions are released.
```javascript
function dragstarted(event,d) { if (!event.active) sim.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; }
function dragged(event,d) { d.fx = event.x; d.fy = event.y; }
function dragended(event,d) { if (!event.active) sim.alphaTarget(0); d.fx = null; d.fy = null; }
return d3.drag().on("start", dragstarted).on("drag", dragged).on("end", dragended);
```
--------------------------------
### KG-RAG System Initialization and Query
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Initializes the Knowledge Graph and Ollama client, then retrieves relevant nodes and generates an answer to a question using the RAG prompt. Requires `KnowledgeGraph`, `OllamaClient`, `Conversation`, `retrieve_nodes`, `build_rag_prompt`, and `RAG_SYSTEM`.
```python
from modules.kg_rag_ollama_api import (
KnowledgeGraph, OllamaClient, Conversation,
retrieve_nodes, build_rag_prompt, RAG_SYSTEM
)
import asyncio
# Load knowledge graph with embeddings
kg = KnowledgeGraph(
graph_file="./storage/kg/matkg_qwen3_235b_580papers.json",
embed_model="all-MiniLM-L6-v2"
)
# Initialize Ollama client
client = OllamaClient(
url="http://localhost:11434/api/chat",
model="qwen3:235b"
)
# Create conversation with RAG system prompt
conversation = Conversation(RAG_SYSTEM)
# Retrieve relevant nodes for a query
question = "What is the role of P3HT crystallinity in OPV performance?"
node_infos = retrieve_nodes(question, kg)
# Build context from retrieved nodes
context = kg.build_context(
nodes=node_infos,
include_structured=True,
char_budget=12000,
hint_terms=["P3HT", "crystallinity", "OPV"]
)
# Generate RAG prompt and get answer
rag_prompt = build_rag_prompt(question, context)
messages = conversation.build(rag_prompt)
async def get_answer():
response = await client.chat(messages)
print(response)
asyncio.run(get_answer())
```
--------------------------------
### Initialize LLMTermExtractor with CBORG backend
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Initializes the LLMTermExtractor for processing PDF documents using a cloud-based CBORG backend. Requires a chat client configured with backend details, model, API key, and base URL.
```python
from modules.extract_terms import LLMTermExtractor, make_chat_client
# Or use CBORG backend for cloud LLM
chat_client = make_chat_client(
backend="cborg",
model="google/gemini-flash",
cborg_api_key="your-api-key",
cborg_base="https://api.cborg.lbl.gov"
)
extractor_cborg = LLMTermExtractor(
model_name="google/gemini-flash",
data_dir="./polymer_papers",
output_file="./storage/terminology/extracted_terms_gemini.json",
schema_path="./storage/schema/matkg_schema.yaml",
chat_client=chat_client,
)
```
--------------------------------
### Initialize D3.js Visualization
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/storage/kg/view_kg.html
Sets up the SVG canvas, tooltip, color scale, and D3 force simulation. Renders initial nodes and links.
```javascript
const width = window.innerWidth, height = window.innerHeight; const svg = d3.select("svg"); const tooltip = d3.select(".tooltip"); const neonColors = d3.scaleSequential(d3.interpolateRainbow).domain([0, d3.max(Object.values(degree)) || 1]); const g = svg.append("g"); let link = g.append("g").selectAll("line").data(links).join("line").attr("class", "link").attr("stroke-width", 1); let node = g.append("g").selectAll("circle").data(nodes).join("circle").attr("class", "node").attr("r", 6).attr("fill", d => neonColors(degree[d.id] || 0)).attr("stroke", "#fff").attr("stroke-width", 0.5).call(drag(simulation()));
```
--------------------------------
### Run KG-RAG Chat with Specific KG (CLI)
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Load and use a custom knowledge graph JSON file for CLI-based chat interactions.
```bash
python kg_rag_ollama.py --graph storage/kg/my_custom_kg.json
```
--------------------------------
### Programmatic FastAPI Server Creation
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Creates and runs the FastAPI application for the KG-RAG system programmatically using `uvicorn`. This allows for custom server configurations.
```python
# Programmatic server creation
from modules.kg_rag_ollama_api import create_fastapi_app, run_fastapi
import uvicorn
app = create_fastapi_app("./storage/kg/matkg_graph.json")
uvicorn.run(app, host="0.0.0.0", port=11435)
```
--------------------------------
### Run One-Shot Question with Specific KG
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Query the KG-RAG LLM with a specific question, utilizing a custom knowledge graph file.
```bash
python kg_rag_ollama.py \
--graph storage/kg/opv_expert_graph.json \
--question "How does annealing influence phase separation in P3HT:PCBM?"
```
--------------------------------
### Initialize LLMTermExtractor with Ollama
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Initializes the LLMTermExtractor for processing PDF documents using a local Ollama model. Specify model name, base URL, data directory, output file, context length, schema path, and worker count.
```python
from modules.extract_terms import LLMTermExtractor, make_chat_client
# Initialize with Ollama backend
extractor = LLMTermExtractor(
model_name="gemma3:27b",
ollama_base_url="http://localhost:11434",
temperature=0.0,
data_dir="./polymer_papers",
output_file="./storage/terminology/extracted_terms.json",
context_length=50,
schema_path="./storage/schema/matkg_schema.yaml",
max_workers=4,
)
# Process all PDFs in directory
result = extractor.process_directory()
# Returns: {
# "status": "success",
# "processed_files": 100,
# "processed_pages_total": 2847,
# "processed_pages_with_terms": 1523,
# "unique_terms": 4521,
# "output_file": "./storage/terminology/extracted_terms.json"
# }
```
--------------------------------
### Run KG-RAG Competency Evaluation
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Evaluate the KG-RAG model against a baseline using a predefined competency question set. Results are saved incrementally.
```bash
python kg_rag_ollama.py --competency
```
--------------------------------
### Run KG-RAG LLM Chat CLI
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Execute the KG-RAG LLM chat script for interactive console-based questions.
```bash
python kg_rag_ollama.py
```
--------------------------------
### Ask One-Shot Question with KG-RAG LLM
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/README.md
Ask a single question to the KG-RAG LLM without entering the interactive REPL. This performs semantic search, node expansion, PDF grounding, and RAG answer generation.
```bash
python kg_rag_ollama.py --question "What is the role of P3HT crystallinity in OPV performance?"
```
--------------------------------
### Execute Pipeline via CLI
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Run the extraction pipeline, organize directories, or perform dry runs using the command line interface.
```bash
python run_pipeline_cborg.py --pdf-root ./polymer_papers --models google/gemini-flash-lite
```
```bash
python run_pipeline_cborg.py --organize --source-dir ./all_papers --pdf-root ./polymer_papers
```
```bash
python run_pipeline_cborg.py --dry-run --models qwen3:235b lbl/cborg-chat:latest
```
--------------------------------
### JSON to Knowledge Graph Conversion CLI
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Command-line interface for converting extracted JSON terms into a knowledge graph. Use this for batch processing of terminology data.
```bash
# Basic conversion
python -m modules.json2kg ./storage/terminology/extracted_terms.json ./storage/kg/matkg_graph.json
# With verbose output
python -m modules.json2kg ./storage/terminology/extracted_terms.json ./storage/kg/matkg_graph.json --verbose
# Output: INFO: Wrote 4521 nodes and 12847 edges → ./storage/kg/matkg_graph.json
```
--------------------------------
### Run Term Extraction with CBORG Cloud API
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Runs term extraction on a directory of PDFs using the CBORG cloud API. Configure PDF directory, output file, model, backend, CBORG API key, CBORG base URL, schema path, and max workers. Note that lower parallelism is recommended for cloud APIs.
```python
from pathlib import Path
from modules.extract_terms import run_extraction
import os
result = run_extraction(
pdf_dir=Path("./research_papers"),
output_json=Path("./storage/terminology/terms_cborg.json"),
model="lbl/cborg-chat:latest",
backend="cborg",
cborg_api_key=os.environ.get("CBORG_API_KEY"),
cborg_base="https://api.cborg.lbl.gov",
schema_path="storage/schema/matkg_schema.yaml",
max_workers=1, # Lower parallelism for cloud APIs
)
```
--------------------------------
### D3.js Pathfinding: Shortest Path Highlighting
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/storage/kg/view_kg.html
Finds and highlights the shortest path between two selected nodes using Breadth-First Search (BFS). Nodes and links on the path are styled differently to indicate their inclusion.
```javascript
function highlightPath(source, target) {
const adj = {};
nodes.forEach(n => {
adj[n.id] = [];
});
links.forEach(l => {
adj[l.source.id || l.source].push(l.target.id || l.target);
adj[l.target.id || l.target].push(l.source.id || l.source);
});
const queue = [[source.id]];
const visited = new Set([source.id]);
let path = null;
while (queue.length) {
const curr = queue.shift();
const nodeId = curr[curr.length-1];
if (nodeId === target.id) {
path = curr;
break;
}
for (let neigh of adj[nodeId]) {
if (!visited.has(neigh)) {
visited.add(neigh);
queue.push([...curr, neigh]);
}
}
}
if (!path) return;
node.attr("stroke", d => path.includes(d.id) ? "#f0f":"#333")
.attr("stroke-width", d => path.includes(d.id) ? 3:0.5);
link.attr("class", l => isConsecutive(path, l.source.id||l.source, l.target.id||l.target) ? "path-highlight" : "link");
}
```
--------------------------------
### Run Term Extraction with Ollama
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Provides a high-level API to run term extraction on a directory of PDFs using the Ollama backend. Configure PDF directory, output file, model, backend, Ollama URL, schema path, temperature, context length, and max workers.
```python
from pathlib import Path
from modules.extract_terms import run_extraction
# Run extraction with Ollama
result = run_extraction(
pdf_dir=Path("./polymer_papers"),
output_json=Path("./storage/terminology/extracted_terms.json"),
model="qwen3:235b",
backend="ollama",
ollama_url="http://localhost:11434",
schema_path="storage/schema/matkg_schema.yaml",
temperature=0.0,
context_length=50,
max_workers=4,
)
print(f"Extracted {result['unique_terms']} terms from {result['processed_files']} files")
```
--------------------------------
### Organize and Process Research Papers in Python
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Use these functions to batch papers into checkpoint folders and execute the extraction pipeline across specified models.
```python
organize_papers_into_folders(
source_dir=Path("./all_papers"),
output_root=Path("./polymer_papers"),
papers_per_folder=25
)
```
```python
run_checkpoint_pipeline(
pdf_root=Path("./polymer_papers"),
models=["google/gemini-flash-lite", "lbl/cborg-chat:latest"],
dry_run=False
)
```
--------------------------------
### Lookup Chemical Entities in ChEBI Ontology
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
Use ChebiOboLookup to enrich chemical data by querying the ChEBI ontology. Load the ontology from a .obo file and then look up compounds by name or synonym to retrieve detailed information.
```python
from modules.agents.chebi import ChebiOboLookup
# Load ChEBI ontology
chebi = ChebiOboLookup("./storage/ontologies/chebi.obo")
# Lookup chemical by name or synonym
result = chebi.lookup("methanol")
# Returns:
# {
# "chebi_id": "CHEBI:17790",
# "name": "methanol",
# "definition": "The simplest alcohol...",
# "synonyms": ["methyl alcohol", "wood alcohol", "MeOH"],
# "formula": "CH4O",
# "mass": "32.04186",
# "charge": "0",
# "inchi": "InChI=1S/CH4O/c1-2/h2H,1H3",
# "inchikey": "OKKJLVBELUTLKV-UHFFFAOYSA-N",
# "smiles": "CO",
# "roles": ["CHEBI:33292"], # ChEBI role IDs
# "parents": ["CHEBI:30879"],
# "relationships": {...},
# "xrefs": {"CAS": ["67-56-1"], "KEGG": ["C00132"]}
# }
```
```python
# Lookup P3HT monomer
result = chebi.lookup("thiophene")
if result:
print(f"Formula: {result['formula']}, SMILES: {result['smiles']}")
```
--------------------------------
### Link Interaction: Tooltips
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/storage/kg/view_kg.html
Configures mouseover events for links (associations) to display relationship details in a tooltip.
```javascript
link.on("mouseover", (e,d) => { tooltip.style("display","block").style("left",(e.pageX+10)+"px").style("top",(e.pageY+10)+"px").html(` Relation: ${d.label}
Source: ${(d.source.label||d.source.id||d.source)}
Target: ${(d.target.label||d.target.id||d.target)} `); d3.select(e.currentTarget).attr("stroke","#ff0").attr("stroke-width",3); }).on("mouseout", (e,d) => { tooltip.style("display","none"); d3.select(e.currentTarget).attr("stroke","#0ff").attr("stroke-width",1); });
```
--------------------------------
### Orchestrate Pipeline Execution
Source: https://context7.com/fair2wise/fairtowise-forum-ai/llms.txt
The run_pipeline_cborg module provides functions to run checkpoint-based evaluation pipelines and organize research papers into folders for batch processing.
```python
from pathlib import Path
from run_pipeline_cborg import run_checkpoint_pipeline, organize_papers_into_folders
```
--------------------------------
### Node Interaction: Tooltips and Dragging
Source: https://github.com/fair2wise/fairtowise-forum-ai/blob/main/storage/kg/view_kg.html
Configures mouseover events for nodes to display detailed tooltips and enable drag-and-drop functionality via the D3 simulation.
```javascript
node.on("mouseover", (e,d) => { d3.select(e.currentTarget).transition().duration(200).attr("r", 12).attr("stroke", "#f0f").attr("stroke-width", 2); const props = d.properties.map(p => `
${d.description || ""}
${d.formula ? `Formula: ${d.formula}
` : ""} ${props ? "Properties: