mirror of
https://github.com/Shubhamsaboo/awesome-llm-apps.git
synced 2026-04-30 15:20:47 -05:00
Address Copilot code review feedback
Fixes applied: - Fix NEO4J_PLUGINS JSON syntax in docker-compose.yml - Fix directory name mismatch in README.md - Remove unused imports (Any, Optional), add re and os - Use Tuple from typing instead of lowercase tuple - Move 're' import to module level (PEP 8) - Configure Ollama client with OLLAMA_HOST env var for Docker - Fix Cypher injection: use parameterized query for hops - Make CONTAINS queries case-insensitive with toLower() - Add try-finally block to ensure graph.close() on exceptions
This commit is contained in:
@@ -47,7 +47,7 @@ Traditional vector-based RAG finds similar text chunks, but struggles with:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Clone and navigate
|
# Clone and navigate
|
||||||
cd knowledge_graph_rag_with_citations
|
cd knowledge_graph_rag_citations
|
||||||
|
|
||||||
# Install dependencies
|
# Install dependencies
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ services:
|
|||||||
- "7687:7687" # Bolt
|
- "7687:7687" # Bolt
|
||||||
environment:
|
environment:
|
||||||
- NEO4J_AUTH=neo4j/password
|
- NEO4J_AUTH=neo4j/password
|
||||||
- NEO4J_PLUGINS=["apoc"]
|
- NEO4J_PLUGINS='["apoc"]'
|
||||||
volumes:
|
volumes:
|
||||||
- neo4j_data:/data
|
- neo4j_data:/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
|
|||||||
@@ -11,12 +11,19 @@ This example uses Ollama for local LLM inference and Neo4j for the knowledge gra
|
|||||||
|
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
import ollama
|
import ollama
|
||||||
|
from ollama import Client as OllamaClient
|
||||||
from neo4j import GraphDatabase
|
from neo4j import GraphDatabase
|
||||||
from typing import List, Dict, Any, Optional
|
from typing import List, Dict, Tuple
|
||||||
|
import re
|
||||||
|
import os
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import json
|
import json
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
# Configure Ollama host from environment (for Docker)
|
||||||
|
OLLAMA_HOST = os.environ.get('OLLAMA_HOST', 'http://localhost:11434')
|
||||||
|
ollama_client = OllamaClient(host=OLLAMA_HOST)
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# Data Models
|
# Data Models
|
||||||
@@ -123,7 +130,7 @@ class KnowledgeGraphManager:
|
|||||||
result = session.run(
|
result = session.run(
|
||||||
f"""
|
f"""
|
||||||
MATCH path = (start:Entity)-[*1..{hops}]-(related:Entity)
|
MATCH path = (start:Entity)-[*1..{hops}]-(related:Entity)
|
||||||
WHERE start.name CONTAINS $name OR start.description CONTAINS $name
|
WHERE toLower(start.name) CONTAINS toLower($name) OR toLower(start.description) CONTAINS toLower($name)
|
||||||
RETURN related.name as name,
|
RETURN related.name as name,
|
||||||
related.description as description,
|
related.description as description,
|
||||||
related.source_doc as source,
|
related.source_doc as source,
|
||||||
@@ -131,7 +138,7 @@ class KnowledgeGraphManager:
|
|||||||
[r in relationships(path) | r.description] as path_descriptions
|
[r in relationships(path) | r.description] as path_descriptions
|
||||||
LIMIT 20
|
LIMIT 20
|
||||||
""",
|
""",
|
||||||
name=entity_name
|
name=entity_name, hops=hops
|
||||||
)
|
)
|
||||||
return [dict(record) for record in result]
|
return [dict(record) for record in result]
|
||||||
|
|
||||||
@@ -160,7 +167,7 @@ class KnowledgeGraphManager:
|
|||||||
# LLM-based Entity Extraction
|
# LLM-based Entity Extraction
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|
||||||
def extract_entities_with_llm(text: str, source_doc: str, model: str = "llama3.2") -> tuple[List[Entity], List[Relationship]]:
|
def extract_entities_with_llm(text: str, source_doc: str, model: str = "llama3.2") -> Tuple[List[Entity], List[Relationship]]:
|
||||||
"""Use LLM to extract entities and relationships from text."""
|
"""Use LLM to extract entities and relationships from text."""
|
||||||
|
|
||||||
extraction_prompt = f"""Analyze the following text and extract:
|
extraction_prompt = f"""Analyze the following text and extract:
|
||||||
@@ -193,7 +200,7 @@ Respond in JSON format:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = ollama.chat(
|
response = ollama_client.chat(
|
||||||
model=model,
|
model=model,
|
||||||
messages=[{"role": "user", "content": extraction_prompt}],
|
messages=[{"role": "user", "content": extraction_prompt}],
|
||||||
format="json"
|
format="json"
|
||||||
@@ -306,7 +313,7 @@ Provide a comprehensive answer with inline citations [1], [2], etc. for each cla
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = ollama.chat(
|
response = ollama_client.chat(
|
||||||
model=model,
|
model=model,
|
||||||
messages=[{"role": "user", "content": answer_prompt}]
|
messages=[{"role": "user", "content": answer_prompt}]
|
||||||
)
|
)
|
||||||
@@ -314,7 +321,6 @@ Provide a comprehensive answer with inline citations [1], [2], etc. for each cla
|
|||||||
reasoning_trace.append("✅ Generated answer with citations")
|
reasoning_trace.append("✅ Generated answer with citations")
|
||||||
|
|
||||||
# Step 5: Extract and verify citations
|
# Step 5: Extract and verify citations
|
||||||
import re
|
|
||||||
citation_refs = re.findall(r'\[(\d+)\]', answer)
|
citation_refs = re.findall(r'\[(\d+)\]', answer)
|
||||||
|
|
||||||
for ref in set(citation_refs):
|
for ref in set(citation_refs):
|
||||||
|
|||||||
Reference in New Issue
Block a user