-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (116 loc) · 4.02 KB
/
main.py
File metadata and controls
137 lines (116 loc) · 4.02 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
import os
from fastapi import FastAPI, UploadFile, File, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from dotenv import load_dotenv
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_pinecone import PineconeVectorStore
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain_groq import ChatGroq
from pinecone import Pinecone
from data_processor import get_data
# Load environment variables
load_dotenv()
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
PINECONE_INDEX = os.getenv("PINECONE_INDEX_NAME")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
app = FastAPI()
PORT = int(os.environ.get("PORT", 8000))
origins = [
"http://localhost:5173",
"http://127.0.0.1:5173"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/favicon.ico")
async def favicon():
return Response(status_code=204)
@app.get("/")
def home():
return {"message": "Hello from FastAPI!"}
# Initialize Pinecone, embedding model, LLM
pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(PINECONE_INDEX)
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="llama-3.1-8b-instant")
rag_chain = None
# Input model
class SourceItem(BaseModel):
type: str
path: str
dynamic: bool = False
depth: int = 1
@app.post("/process")
async def process(sources: list[SourceItem]):
global rag_chain
print("[1] Processing input sources...")
docs = "".join(get_data([s.model_dump() for s in sources])) # Blocking version
print(f"[DEBUG] Total text length: {len(docs)}")
splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=100)
chunks = splitter.split_text(docs)
print("[3] Creating embeddings...")
stats = index.describe_index_stats()
if stats.total_vector_count == 0:
vectorstore = PineconeVectorStore.from_texts(
texts=chunks,
embedding=embedding_model,
index_name=PINECONE_INDEX,
namespace="default"
)
print("✅ Vectors uploaded to Pinecone.")
else:
vectorstore = PineconeVectorStore(
index=index,
embedding=embedding_model,
namespace="default"
)
print("✅ Using existing vectors.")
retriever = vectorstore.as_retriever(search_kwargs={"k": 10})
rag_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=True,
verbose=True
)
return {"message": "✅ Data processed and RAG chain initialized."}
# 🔁 Streaming endpoint
@app.post("/stream-process")
async def stream_process(sources: list[SourceItem]):
def stream():
for log in get_data([s.model_dump() for s in sources]):
yield f"data: {log}\n\n"
return StreamingResponse(stream(), media_type="text/event-stream")
# 🔎 Ask Query
class Query(BaseModel):
query: str
@app.post("/ask")
async def ask_question(payload: Query):
global rag_chain
if not rag_chain:
return {"error": "❌ RAG chain not initialized. Run /process first."}
try:
result = rag_chain.invoke({"query": payload.query})
return {"answer": result['result']}
except Exception as e:
return {"error": str(e)}
# 🧹 Reset Pinecone
@app.post("/reset")
async def reset_index():
try:
stats = index.describe_index_stats()
namespaces = stats.namespaces.keys()
if "default" in namespaces:
index.delete(delete_all=True, namespace="default")
return {"message": "✅ Pinecone vectors cleared."}
else:
return {"message": "⚠️ No vectors found in 'default' namespace."}
except Exception as e:
return {"error": f"❌ Error resetting index: {str(e)}"}