Update langchain_rag.py
Browse files- langchain_rag.py +162 -156
langchain_rag.py
CHANGED
|
@@ -1,156 +1,162 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
def __init__(self):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
self.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from typing import List, Dict, Any
|
| 4 |
+
|
| 5 |
+
from langchain_core.documents import Document
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
+
|
| 9 |
+
from extract_error_features import extract_error_features
|
| 10 |
+
|
| 11 |
+
RAW_DOCS_DIR = "data/docs/raw"
|
| 12 |
+
CHUNK_SIZE = 400
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# -------------------------
|
| 16 |
+
# Utils
|
| 17 |
+
# -------------------------
|
| 18 |
+
def chunk_text(text: str, size: int) -> List[str]:
|
| 19 |
+
chunks = []
|
| 20 |
+
for i in range(0, len(text), size):
|
| 21 |
+
chunk = text[i:i+size].strip()
|
| 22 |
+
if chunk:
|
| 23 |
+
chunks.append(chunk)
|
| 24 |
+
return chunks
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def load_raw_docs() -> List[Document]:
|
| 28 |
+
documents = []
|
| 29 |
+
|
| 30 |
+
for fname in os.listdir(RAW_DOCS_DIR):
|
| 31 |
+
path = os.path.join(RAW_DOCS_DIR, fname)
|
| 32 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 33 |
+
text = f.read()
|
| 34 |
+
|
| 35 |
+
chunks = chunk_text(text, CHUNK_SIZE)
|
| 36 |
+
|
| 37 |
+
for chunk in chunks:
|
| 38 |
+
documents.append(
|
| 39 |
+
Document(
|
| 40 |
+
page_content=chunk,
|
| 41 |
+
metadata={
|
| 42 |
+
"source_file": fname,
|
| 43 |
+
"source": "https://www.jenkins.io/doc/"
|
| 44 |
+
}
|
| 45 |
+
)
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return documents
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# -------------------------
|
| 52 |
+
# RAG CLASS
|
| 53 |
+
# -------------------------
|
| 54 |
+
class JenkinsRAGChain:
|
| 55 |
+
def __init__(self):
|
| 56 |
+
print("Loading embeddings...")
|
| 57 |
+
|
| 58 |
+
self.embeddings = HuggingFaceEmbeddings(
|
| 59 |
+
model_name="sentence-transformers/paraphrase-MiniLM-L3-v2",
|
| 60 |
+
model_kwargs={"device": "cpu"}
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
print("Loading documents...")
|
| 64 |
+
self.documents = load_raw_docs()
|
| 65 |
+
|
| 66 |
+
print("Building FAISS index...")
|
| 67 |
+
self.vectorstore = FAISS.from_documents(
|
| 68 |
+
self.documents,
|
| 69 |
+
self.embeddings
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
self.retriever = self.vectorstore.as_retriever(
|
| 73 |
+
search_kwargs={"k": 5}
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# -------------------------
|
| 77 |
+
# Retrieval
|
| 78 |
+
# -------------------------
|
| 79 |
+
def retrieve_docs(self, query: str) -> List[Document]:
|
| 80 |
+
return self.retriever.invoke(query)
|
| 81 |
+
|
| 82 |
+
# -------------------------
|
| 83 |
+
# Simple Explanation Generator (NO LLM needed)
|
| 84 |
+
# -------------------------
|
| 85 |
+
def generate_explanation(self, query: str, docs: List[Document]) -> str:
|
| 86 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 87 |
+
|
| 88 |
+
return f"""
|
| 89 |
+
Jenkins Error Explanation
|
| 90 |
+
|
| 91 |
+
Context from documentation:
|
| 92 |
+
{context[:1500]}
|
| 93 |
+
|
| 94 |
+
Analysis:
|
| 95 |
+
Based on the retrieved documentation, this error likely relates to Jenkins pipeline or configuration issues.
|
| 96 |
+
|
| 97 |
+
Suggested Actions:
|
| 98 |
+
- Check Jenkinsfile syntax
|
| 99 |
+
- Verify plugins and agents
|
| 100 |
+
- Review pipeline configuration
|
| 101 |
+
|
| 102 |
+
Note:
|
| 103 |
+
This explanation is grounded in official Jenkins documentation.
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
# -------------------------
|
| 107 |
+
# Main API
|
| 108 |
+
# -------------------------
|
| 109 |
+
def explain_error(self, log_text: str) -> Dict[str, Any]:
|
| 110 |
+
features = extract_error_features(log_text)
|
| 111 |
+
category = features["category"]
|
| 112 |
+
|
| 113 |
+
query = f"""
|
| 114 |
+
Error Category: {category}
|
| 115 |
+
|
| 116 |
+
Jenkins log:
|
| 117 |
+
{log_text}
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
docs = self.retrieve_docs(query)
|
| 121 |
+
|
| 122 |
+
explanation = self.generate_explanation(query, docs)
|
| 123 |
+
|
| 124 |
+
return {
|
| 125 |
+
"error_category": category,
|
| 126 |
+
"llm_explanation": explanation,
|
| 127 |
+
"retrieved_docs": [
|
| 128 |
+
{
|
| 129 |
+
"content": doc.page_content[:200],
|
| 130 |
+
"source": doc.metadata.get("source")
|
| 131 |
+
}
|
| 132 |
+
for doc in docs
|
| 133 |
+
],
|
| 134 |
+
"retrieval_source": "FAISS + sentence-transformers",
|
| 135 |
+
"embedding_model": "paraphrase-MiniLM-L3-v2"
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# -------------------------
|
| 140 |
+
# Singleton
|
| 141 |
+
# -------------------------
|
| 142 |
+
def get_rag_chain() -> JenkinsRAGChain:
|
| 143 |
+
if not hasattr(get_rag_chain, "_instance"):
|
| 144 |
+
get_rag_chain._instance = JenkinsRAGChain()
|
| 145 |
+
return get_rag_chain._instance
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# -------------------------
|
| 149 |
+
# Test
|
| 150 |
+
# -------------------------
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
print("Initializing RAG...")
|
| 153 |
+
rag = JenkinsRAGChain()
|
| 154 |
+
|
| 155 |
+
sample_error = """
|
| 156 |
+
org.codehaus.groovy.control.MultipleCompilationErrorsException:
|
| 157 |
+
WorkflowScript: 10: expecting '}', found ''
|
| 158 |
+
"""
|
| 159 |
+
|
| 160 |
+
result = rag.explain_error(sample_error)
|
| 161 |
+
|
| 162 |
+
print(json.dumps(result, indent=2))
|