San-D commited on
Commit
dddc42f
·
verified ·
1 Parent(s): 67147c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Query
2
+ from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
3
+ from langchain_community.document_loaders import PyPDFDirectoryLoader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain.chains.question_answering import load_qa_chain
7
+ from langchain_mistralai import ChatMistralAI
8
+ from langchain.prompts import PromptTemplate
9
+ import os
10
+
11
+ app = FastAPI()
12
+
13
+ # ✅ Root Route to Prevent 404 Error
14
+ @app.get("/")
15
+ def home():
16
+ return {"message": "Welcome to the RAG API! Use /query to ask questions."}
17
+
18
+ # ✅ Set environment variables for API keys (Replace with actual keys)
19
+ os.environ['MISTRAL_API_KEY'] = "your-mistral-api-key"
20
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = "your-huggingface-api-token"
21
+
22
+ # ✅ Load and process documents
23
+ def load_docs(directory=r"C:\Users\San_D\Desktop\Project API\RAG\DATA"):
24
+ loader = PyPDFDirectoryLoader(directory)
25
+ return loader.load()
26
+
27
+ documents = load_docs()
28
+
29
+ def split_docs(documents, chunk_size=512, chunk_overlap=20):
30
+ text_splitter = RecursiveCharacterTextSplitter(
31
+ chunk_size=chunk_size, chunk_overlap=chunk_overlap, separators=["\n\n", "\n", " ", ""])
32
+ return text_splitter.split_documents(documents)
33
+
34
+ docs = split_docs(documents)
35
+
36
+ # ✅ Initialize embeddings and vector store
37
+ embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
38
+ if docs:
39
+ index = FAISS.from_documents(docs, embeddings)
40
+ else:
41
+ index = None # Prevents errors if no documents are found
42
+
43
+ def get_similar_docs(query, k=4):
44
+ if index:
45
+ return index.similarity_search(query, k=k)
46
+ return []
47
+
48
+ # ✅ Load LLM
49
+ llm = ChatMistralAI(model="mistral-large-latest", temperature=0.7, max_tokens=150)
50
+
51
+ # ✅ Define prompt
52
+ med_prompt = PromptTemplate.from_template(
53
+ """
54
+ You are an expert medical assistant.
55
+ Based only on the summaries provided below, answer the given question.
56
+ Give all details as much as possible.
57
+ If you don't know the answer, respond with "I don't know".
58
+
59
+ Source material: {context}
60
+
61
+ Question: {question}
62
+ Answer:
63
+ """
64
+ )
65
+
66
+ chain = med_prompt | llm
67
+
68
+ @app.post("/query")
69
+ def get_answer(query: str = Query(..., description="The user's question")):
70
+ relevant_docs = get_similar_docs(query)
71
+ if not relevant_docs:
72
+ raise HTTPException(status_code=404, detail="No relevant documents found.")
73
+
74
+ response = chain.invoke({"context": relevant_docs, "question": query})
75
+ return {"answer": response.content}
76
+
77
+ # ✅ Run Uvicorn if executed directly (for local testing)
78
+ if __name__ == "__main__":
79
+ import uvicorn
80
+ uvicorn.run(app, host="0.0.0.0", port=8000)