-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
266 lines (224 loc) · 9.13 KB
/
Copy pathmain.py
File metadata and controls
266 lines (224 loc) · 9.13 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from collections import abc
from foundry_local_sdk import Configuration, FoundryLocalManager
import sqlite3
from datetime import datetime
import os, json, math
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import uvicorn
#global variables to hold models, db connections and contexts
embedding_model = None
chat_model = None
connection = None
cursor = None
context = ""
#array containing base document strings to seed the vector database
documents = [""
]
#init of model
@asynccontextmanager
async def lifespan(app: FastAPI):
global embedding_model, chat_model, connection, cursor
connection = sqlite3.connect('database-rag.db', check_same_thread=False)
cursor = connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS documents (
id INTEGER PRIMARY KEY,
text TEXT,
embedding TEXT,
distance REAL,
source TEXT,
approxcolorr REAL,
approxcolorg REAL,
approxcolorb REAL,
approxcolora REAL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS queries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
query_question TEXT,
query_answer TEXT,
timestamp TEXT
)
''')
connection.commit()
config = Configuration(app_name="foundry_local_rag")
FoundryLocalManager.initialize(config)
embedding_model = FoundryLocalManager.instance.catalog.get_model("qwen3-embedding-0.6b")
embedding_model.download(lambda p: print(f"Downloading embedding model: {p:.1f}%", end="\r", flush=True))
print("\nEmbedding model downloaded.")
embedding_model.load()
chat_model = FoundryLocalManager.instance.catalog.get_model("qwen2.5-0.5b")
chat_model.download(lambda p: print(f"Downloading chat model: {p:.1f}%", end="\r", flush=True))
print("\nChat model downloaded.")
chat_model.load()
cursor.execute("SELECT COUNT(*) FROM documents")
db_count = cursor.fetchone()[0]
if db_count == 0:
embedding_client = embedding_model.get_embedding_client()
doc_embeddings = [item.embedding for item in embedding_client.generate_embeddings(documents).data]
for i, (doc, emb) in enumerate(zip(documents, doc_embeddings)):
min_val = min(emb)
max_val = max(emb)
norm = lambda v: (v - min_val) / (max_val - min_val) * 255 if max_val != min_val else 0
cursor.execute('''
INSERT OR IGNORE INTO documents (id, text, embedding, distance, source, approxcolorr, approxcolorg, approxcolorb, approxcolora)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (i + 1, doc, str(emb), 0.0, "knowledge_base", norm(emb[0]), norm(emb[1]), norm(emb[2]), norm(emb[3])))
connection.commit()
print(f"Indexed {len(doc_embeddings)} documents on startup.")
yield
print("Shutting down.")
if embedding_model:
embedding_model.unload()
if chat_model:
chat_model.unload()
if connection:
connection.close()
#backend init of front end
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
#request and response models
class ChatRequest(BaseModel):
query: str
message_history: list[dict[str, str]] = []
#same pol function from any standart scientific calculator
def cosine_similarity(a, b):
"""
Computes the cosine similarity between two numerical vectors.
Used to determine how semantically close a query is to a document chunk.
"""
return sum(x*y for x,y in zip(a,b)) / (math.sqrt(sum(x*x for x in a)) * math.sqrt(sum(y*y for y in b)))
def chunking(text):
"""
Splits text blocks into paragraphs using double newlines as the boundary.
Cleans leading/trailing whitespaces and recursively processes arrays of texts.
"""
if isinstance(text, str):
return [p.strip() for p in text.split("\n\n") if p.strip()]
result = []
for item in text:
result.extend(chunking(item))
return result
def getTopChunks(query, cursor, top_k=3):
"""
Retrieves the most semantically relevant document chunks from the SQLite database:
1. Generates an embedding vector for the user query using Qwen-Embedding.
2. Fetches all indexed documents and their pre-computed embeddings from the DB.
3. Computes the cosine similarity between the query embedding and each document embedding.
4. Sorts the results in descending order and returns the top_k chunks.
"""
queryResponse = embedding_model.get_embedding_client().generate_embeddings([query])
queryEmb = queryResponse.data[0].embedding
cursor.execute("SELECT text, embedding FROM documents")
rows = cursor.fetchall()
scores = []
for text, embStr in rows:
dbEmb = json.loads(embStr)
score = cosine_similarity(queryEmb, dbEmb)
scores.append((text, score))
scores.sort(key=lambda x: x[1], reverse=True)
return scores[:top_k]
#api endpoint
@app.post("/query")
def query_endpoint(request: ChatRequest):
"""
Core RAG API Endpoint.
Handles dynamic topic context loading or falls back to SQLite database semantic search.
Generates answers using the local Qwen LLM and logs history to SQLite.
"""
global chat_model, connection, cursor, context
user_query = request.query
context = ""
base_dir = os.path.dirname(os.path.abspath(__file__))
#check for topic suffixes sent by the frontend toggle buttons
if user_query.endswith("é*:1"):
#topic 1: vehicle fixing. clean suffix and append formal bibliography
query = user_query[:-(len("é*:1"))] + " (Source: Utah State University Extension, Dept. of Automotive Technology, Bulletin No. 402)"
docs = readMD(os.path.join(base_dir, "vehicle_fixing_guide.md"))
context = "\n".join(docs)
elif user_query.endswith("é*:2"):
#topic 2: water & fire. clean suffix and append formal bibliography
query = user_query[:-(len("é*:2"))] + " (Source: U.S. Dept. of the Army, FM 3-05.70 Survival Manual, Chapter 6: Water Procurement & Chapter 7: Firecraft, 2002)"
docs = readMD(os.path.join(base_dir, "water_and_fire_guide.md"))
context = "\n".join(docs)
elif user_query.endswith("é*:3"):
#topic 3: wilderness survival. clean suffix and append formal bibliography
query = user_query[:-(len("é*:3"))] + " (Source: U.S. Army Infantry School, FM 21-76 Survival Field Manual, Department of the Army, 1992)"
docs = readMD(os.path.join(base_dir, "wilderness_survival_guide.md"))
context = "\n".join(docs)
else:
#default rag mode: search sqlite database using embeddings
query = user_query
results = getTopChunks(query, cursor, top_k=3)
context = "\n".join(f"- {text}" for text, _ in results)
#construct the rag payload for the llm
messages = [
{
"role": "system",
"content": (
"Answer the user's question using only the provided context. Explain everything shortly but very precise and accurate. "
"If the context doesn't contain enough information, say so.\n\n"
f"Context:\n{context}"
),
},
{"role": "user", "content": query},
]
#generate response from the cached qwen model
full_content = ""
try:
for chunk in chat_model.get_chat_client().complete_streaming_chat(messages):
if chunk.choices:
content = chunk.choices[0].delta.content
if content:
full_content += content
except Exception as e:
print(f"Hata: {e}")
full_content = "Error."
#log the interaction history into the sqlite queries table
cursor.execute('''
INSERT INTO queries (query_question, query_answer, timestamp)
VALUES (?, ?, ?)
''', (user_query, full_content, datetime.now().isoformat()))
connection.commit()
context = ""
return {"response": full_content}
#home api endpoint set status of server
@app.get("/")
def home():
return {"status": "Foundry Local RAG API is running"}
def readMD(path):
"""
Parses a markdown file and filters out structural metadata.
Ignores headers (#), comment markings (//), bullet points (*, -), and blank lines.
Returns a list of raw text lines.
used traditional open for python
"""
docs = []
with open(path, "r", encoding="utf-8") as file:
for line in file:
if line.startswith("#"):
continue
if line.startswith("*"):
continue
if line.startswith("-"):
continue
if line.startswith("//"):
continue
elif line.startswith("\n"):
continue
else:
docs.append(line)
return docs
#where it will start
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)