Spaces:
Build error
Build error
Delete rag.py
Browse files
rag.py
DELETED
|
@@ -1,83 +0,0 @@
|
|
| 1 |
-
from langchain import PromptTemplate
|
| 2 |
-
from langchain.llms import CTransformers
|
| 3 |
-
from langchain.chains import RetrievalQA
|
| 4 |
-
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 5 |
-
from fastapi import FastAPI, Request, Form, Response
|
| 6 |
-
from fastapi.responses import HTMLResponse
|
| 7 |
-
from fastapi.templating import Jinja2Templates
|
| 8 |
-
from fastapi.staticfiles import StaticFiles
|
| 9 |
-
from fastapi.encoders import jsonable_encoder
|
| 10 |
-
from qdrant_client import QdrantClient
|
| 11 |
-
from langchain.vectorstores import Qdrant
|
| 12 |
-
import os
|
| 13 |
-
import json
|
| 14 |
-
|
| 15 |
-
app = FastAPI()
|
| 16 |
-
templates = Jinja2Templates(directory="templates")
|
| 17 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 18 |
-
|
| 19 |
-
local_llm = "joshnader/meditron-7b-Q4_K_M-GGUF"
|
| 20 |
-
|
| 21 |
-
config = {
|
| 22 |
-
'max_new_tokens': 512,
|
| 23 |
-
'context_length': 2048,
|
| 24 |
-
'repetition_penalty': 1.1,
|
| 25 |
-
'temperature': 0.1,
|
| 26 |
-
'top_k': 50,
|
| 27 |
-
'top_p': 0.9,
|
| 28 |
-
'stream': True,
|
| 29 |
-
'threads': int(os.cpu_count() / 4)
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
llm = CTransformers(
|
| 34 |
-
model=local_llm,
|
| 35 |
-
model_type="llama",
|
| 36 |
-
**config
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
print("LLM Initialized....")
|
| 42 |
-
|
| 43 |
-
prompt_template = """Use the following pieces of information to answer the user's question.
|
| 44 |
-
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 45 |
-
|
| 46 |
-
Context: {context}
|
| 47 |
-
Question: {question}
|
| 48 |
-
|
| 49 |
-
Only return the helpful answer below and nothing else.
|
| 50 |
-
Helpful answer:
|
| 51 |
-
"""
|
| 52 |
-
|
| 53 |
-
embeddings = SentenceTransformerEmbeddings(model_name="NeuML/pubmedbert-base-embeddings")
|
| 54 |
-
|
| 55 |
-
url = "http://localhost:6333"
|
| 56 |
-
|
| 57 |
-
client = QdrantClient(
|
| 58 |
-
url=url, prefer_grpc=False
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
db = Qdrant(client=client, embeddings=embeddings, collection_name="vector_db")
|
| 62 |
-
|
| 63 |
-
prompt = PromptTemplate(template=prompt_template, input_variables=['context', 'question'])
|
| 64 |
-
|
| 65 |
-
retriever = db.as_retriever(search_kwargs={"k":1})
|
| 66 |
-
|
| 67 |
-
@app.get("/", response_class=HTMLResponse)
|
| 68 |
-
async def read_root(request: Request):
|
| 69 |
-
return templates.TemplateResponse("index.html", {"request": request})
|
| 70 |
-
|
| 71 |
-
@app.post("/get_response")
|
| 72 |
-
async def get_response(query: str = Form(...)):
|
| 73 |
-
chain_type_kwargs = {"prompt": prompt}
|
| 74 |
-
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True, chain_type_kwargs=chain_type_kwargs, verbose=True)
|
| 75 |
-
response = qa(query)
|
| 76 |
-
print(response)
|
| 77 |
-
answer = response['result']
|
| 78 |
-
source_document = response['source_documents'][0].page_content
|
| 79 |
-
doc = response['source_documents'][0].metadata['source']
|
| 80 |
-
response_data = jsonable_encoder(json.dumps({"answer": answer, "source_document": source_document, "doc": doc}))
|
| 81 |
-
|
| 82 |
-
res = Response(response_data)
|
| 83 |
-
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|