-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrag_engine.py
More file actions
97 lines (77 loc) · 2.89 KB
/
Copy pathrag_engine.py
File metadata and controls
97 lines (77 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
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
56
57
58
59
60
61
62
63
64
65
66
67
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
#!/usr/bin/env python3
"""
AI RAG System - Using Ollama (free local AI)
"""
import os
import json
from pathlib import Path
from typing import List, Dict
import requests
class RAGEngine:
def __init__(self, persist_directory: str = "./chroma_db"):
self.persist_directory = persist_directory
self.docs = []
self.ollama_url = "http://localhost:11434/api/generate"
self.model = "qwen2.5-coder"
def ingest(self, file_path: str) -> Dict:
path = Path(file_path)
if not path.exists():
return {"status": "file_not_found"}
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
self.docs.append({"source": path.name, "content": content[:5000]})
return {"status": "success", "chunks": 1, "file": path.name}
def retrieve(self, query: str, top_k: int = 3) -> List[Dict]:
# Simple keyword matching for now
results = []
for doc in self.docs:
if any(word.lower() in doc["content"].lower() for word in query.split()[:3]):
results.append({"text": doc["content"][:500], "score": 0.8, "source": doc["source"]})
return results[:top_k]
def generate(self, query: str) -> str:
# Use Ollama for generation
try:
context = " ".join([d["content"][:500] for d in self.docs[:3]])
prompt = f"""Based on this context:
{context}
Question: {query}
Answer:"""
response = requests.post(
self.ollama_url,
json={
"model": self.model,
"prompt": prompt,
"stream": False
},
timeout=60
)
if response.status_code == 200:
return response.json().get("response", "No response from AI")
else:
return f"AI Error: {response.status_code}"
except Exception as e:
return f"Ollama not running? Start with: ollama serve\nError: {str(e)}"
def chat(self, query: str) -> Dict:
answer = self.generate(query)
sources = self.retrieve(query)
return {
"answer": answer,
"sources": sources
}
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--ingest", help="Document to ingest")
parser.add_argument("--query", help="Query")
args = parser.parse_args()
rag = RAGEngine()
if args.ingest:
print(f"📄 Ingesting: {args.ingest}")
result = rag.ingest(args.ingest)
print(json.dumps(result, indent=2))
if args.query:
print(f"\n💬 Question: {args.query}")
result = rag.chat(args.query)
print(f"\nAI: {result['answer'][:500]}")
if __name__ == "__main__":
main()