Download mcp/llm_engine.py from Surajnitp/flowmarg: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/mcp/llm_engine.py
- Command line
-
hf download hf://spaces/Surajnitp/flowmarg/mcp/llm_engine.py
-
curl -L -o llm_engine.py https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/mcp/llm_engine.py
11 kB
| """ | |
| LLM ENGINE - TRACK DESIGN ENGINEER GRADE | |
| Thinks like a senior track design engineer | |
| """ | |
| import os | |
| import sys | |
| import re | |
| from typing import List, Dict, Any, Optional | |
| from core.bootstrap import ROOT # noqa: F401 | |
| from core.config import ProductionConfig | |
| from core.audit_logger import get_logger | |
| from core.rag import log_llm_answer | |
| logger = get_logger("mcp.llm_engine") | |
| import ollama | |
| import chromadb | |
| from sentence_transformers import SentenceTransformer | |
| class LLMEngine: | |
| def __init__(self, model_name: str = "qwen2:1.5b"): | |
| """ | |
| Initialize LLM Engine with Engineering Mindset | |
| """ | |
| self.model_name = model_name | |
| self.embedding_model = SentenceTransformer(ProductionConfig.EMBEDDING_MODEL) | |
| # Connect to ChromaDB | |
| chroma_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "knowledge", "chroma_db") | |
| self.chroma_client = chromadb.PersistentClient(path=chroma_path) | |
| self.collection = None | |
| try: | |
| self.collection = self.chroma_client.get_collection(ProductionConfig.CHROMA_COLLECTION) | |
| logger.info("Connected to knowledge base") | |
| except Exception as e: | |
| logger.warning("Knowledge base not found: %s", e) | |
| self._check_ollama() | |
| def _check_ollama(self): | |
| """Check if Ollama is running and model is available""" | |
| try: | |
| ollama.list() | |
| logger.info("Ollama is running") | |
| models = ollama.list() | |
| model_names = [m['model'] for m in models['models']] | |
| if self.model_name not in model_names: | |
| logger.warning("Model '%s' not found.", self.model_name) | |
| print(f" Available: {', '.join(model_names[:5]) if model_names else 'None'}") | |
| print(f" To download: ollama pull {self.model_name}") | |
| else: | |
| print(f"β Model '{self.model_name}' is available") | |
| except Exception as e: | |
| print(f"β Ollama not running!") | |
| print(f" Start Ollama with: ollama serve (in a separate window)") | |
| print(f" Error: {e}") | |
| def search_knowledge(self, question: str, top_k: int = 5) -> List[Dict]: | |
| """Search knowledge base for relevant chunks""" | |
| if not self.collection: | |
| print("β οΈ No knowledge base connection") | |
| return [] | |
| try: | |
| question_embedding = self.embedding_model.encode([question]) | |
| results = self.collection.query( | |
| query_embeddings=question_embedding.tolist(), | |
| n_results=top_k | |
| ) | |
| formatted = [] | |
| if results['documents'] and results['documents'][0]: | |
| for i in range(len(results['documents'][0])): | |
| formatted.append({ | |
| "content": results['documents'][0][i], | |
| "metadata": results['metadatas'][0][i], | |
| "score": 1 - results['distances'][0][i] if results['distances'] else 0 | |
| }) | |
| return formatted | |
| except Exception as e: | |
| print(f"β οΈ Search error: {e}") | |
| return [] | |
| def build_context(self, results: List[Dict]) -> str: | |
| """Build context from search results""" | |
| if not results: | |
| return "No relevant information found." | |
| context_parts = [] | |
| for i, result in enumerate(results, 1): | |
| source = result['metadata'].get('source', 'Unknown') | |
| section = result['metadata'].get('title', 'Unknown Section') | |
| content = result['content'][:3000] | |
| context_parts.append(f""" | |
| [REFERENCE {i}] | |
| Source: {source} | |
| Section: {section} | |
| Content: {content} | |
| """) | |
| return "\n".join(context_parts) | |
| # ============================================================ | |
| # ENGINEERING MINDSET - THE KEY UPGRADE | |
| # ============================================================ | |
| def _get_system_prompt(self) -> str: | |
| from core.config import ProductionConfig | |
| return ProductionConfig.NR_ASSISTANT_SYSTEM_PROMPT + """ | |
| YOUR EXPERTISE: | |
| - Network Rail Standards: NR/L2/TRK/2500, NR/L2/TRK/2102, NR/L2/TRK/2049 | |
| - Track Design Phases 1-5 (Survey, Design, CAD, Assurance, S&C) | |
| YOUR ANSWER FORMAT: | |
| 1. REQUIREMENT (cite file + page + clause) | |
| 2. REASON | |
| 3. APPLICATION | |
| 4. RISK/WARNING | |
| 5. ACTION | |
| """ | |
| def _build_prompt(self, question: str, context: str) -> str: | |
| """Build the user prompt with engineering focus""" | |
| return f""" | |
| ENGINEER'S QUESTION: {question} | |
| RELEVANT STANDARDS AND KNOWLEDGE: | |
| {context} | |
| INSTRUCTIONS TO THE SENIOR ENGINEER: | |
| 1. Use your 15+ years of track design experience | |
| 2. Structure your answer as: REQUIREMENT β REASON β APPLICATION β RISK β ACTION | |
| 3. Cite specific standards (e.g., "NR 2102 Section 4.2.1") | |
| 4. Give practical, actionable guidance | |
| 5. Include any critical warnings or checks | |
| 6. If information is missing, say so and suggest where to find it | |
| YOUR ENGINEERING ANSWER: | |
| """ | |
| # ============================================================ | |
| # MAIN ASK FUNCTION | |
| # ============================================================ | |
| def ask(self, question: str, top_k: int = 5) -> Dict: | |
| """Ask a question and get an engineering-grade answer""" | |
| # 1. Search knowledge base | |
| results = self.search_knowledge(question, top_k) | |
| if not results: | |
| return { | |
| "answer": """ | |
| β οΈ I don't have enough information in my knowledge base. | |
| RECOMMENDATION: | |
| 1. Check if you have the relevant standard in: knowledge/standards/ | |
| 2. Run the knowledge loader: python scripts/ingest_knowledge.py | |
| 3. Verify the standard PDF is in the correct folder | |
| Please ensure your knowledge base contains the relevant standards. | |
| """, | |
| "sources": [], | |
| "confidence": 0, | |
| "total_chunks_found": 0 | |
| } | |
| # 2. Build context | |
| context = self.build_context(results) | |
| # 3. Build engineering prompt | |
| prompt = self._build_prompt(question, context) | |
| # 4. Get LLM response | |
| try: | |
| response = ollama.chat( | |
| model=self.model_name, | |
| messages=[ | |
| {"role": "system", "content": self._get_system_prompt()}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| answer = response['message']['content'] | |
| if results: | |
| meta0 = results[0].get("metadata") or {} | |
| log_llm_answer( | |
| source_file=str(meta0.get("source", "chroma")), | |
| standard_clause=str(meta0.get("clause") or meta0.get("section") or "unknown"), | |
| ) | |
| except Exception as e: | |
| answer = f"β Error generating response: {e}" | |
| # 5. Extract sources | |
| sources = [] | |
| for result in results: | |
| sources.append({ | |
| "source": result['metadata'].get('source', 'Unknown'), | |
| "section": result['metadata'].get('title', 'Unknown Section'), | |
| "type": result['metadata'].get('type', 'Unknown'), | |
| "score": round(result['score'], 3) | |
| }) | |
| return { | |
| "answer": answer, | |
| "sources": sources, | |
| "confidence": round(results[0]['score'], 3) if results else 0, | |
| "total_chunks_found": len(results) | |
| } | |
| # ============================================================ | |
| # DISPLAY FUNCTIONS | |
| # ============================================================ | |
| def ask_with_sources(self, question: str, top_k: int = 5) -> str: | |
| """Get engineering answer with sources""" | |
| result = self.ask(question, top_k) | |
| output = f""" | |
| {'='*65} | |
| π€οΈ TRACK DESIGN ENGINEER | |
| {'='*65} | |
| π QUESTION: {question} | |
| {'='*65} | |
| π‘ ENGINEERING ANSWER: | |
| {'='*65} | |
| {result['answer']} | |
| {'='*65} | |
| π REFERENCES: | |
| {'='*65} | |
| """ | |
| if result['sources']: | |
| for i, source in enumerate(result['sources'], 1): | |
| output += f""" | |
| [{i}] {source['source']} | |
| Section: {source['section']} | |
| Type: {source['type']} | |
| Confidence: {source['score']:.2%} | |
| """ | |
| else: | |
| output += "\n No references available" | |
| output += f""" | |
| {'='*65} | |
| π ASSESSMENT: | |
| Confidence: {result['confidence']:.2%} | |
| Sources Used: {result['total_chunks_found']} | |
| π‘ ENGINEERING JUDGEMENT: | |
| {self._get_judgement(result)} | |
| {'='*65} | |
| """ | |
| return output | |
| def _get_judgement(self, result: Dict) -> str: | |
| """Add engineering judgement to the response""" | |
| if result['confidence'] > 0.8: | |
| return "β High confidence - This information is well supported by standards." | |
| elif result['confidence'] > 0.6: | |
| return "π‘ Moderate confidence - Verify against the actual standard." | |
| elif result['confidence'] > 0.4: | |
| return "π Low confidence - Cross-check with the original standard document." | |
| else: | |
| return "β οΈ Very low confidence - Consider checking the knowledge base content." | |
| # ============================================================ | |
| # COMMAND LINE INTERFACE | |
| # ============================================================ | |
| def main(): | |
| """Command line interface for Engineering LLM""" | |
| print("=" * 65) | |
| print(" π€οΈ TRACK DESIGN ENGINEER - AI ASSISTANT") | |
| print(" Senior Engineer with 15+ years experience") | |
| print("=" * 65) | |
| print() | |
| engine = LLMEngine() | |
| print("\nπ‘ Ask me anything about UK railway track design.") | |
| print(" I'll answer like a senior track design engineer.\n") | |
| print("π EXAMPLE QUESTIONS:") | |
| print(" - What is the ballast depth requirement?") | |
| print(" - How to design a ballast ramp?") | |
| print(" - What is S&C placement philosophy?") | |
| print(" - What should a GAD drawing show?") | |
| print(" - Explain stressing of CWR") | |
| print() | |
| print("-" * 65) | |
| while True: | |
| try: | |
| question = input("\nπ Engineer: ").strip() | |
| if question.lower() in ['exit', 'quit', 'q']: | |
| print("\nπ Goodbye, Engineer!") | |
| break | |
| if not question: | |
| continue | |
| print("\nβ³ Thinking like a Senior Engineer...\n") | |
| response = engine.ask_with_sources(question) | |
| print(response) | |
| except KeyboardInterrupt: | |
| print("\n\nπ Goodbye, Engineer!") | |
| break | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| if __name__ == "__main__": | |
| main() | |