create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Depends, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import os
|
| 4 |
+
import chromadb
|
| 5 |
+
from langchain_community.document_loaders.pdf import PyPDFDirectoryLoader
|
| 6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 8 |
+
from langchain.vectorstores import Chroma
|
| 9 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 10 |
+
|
| 11 |
+
# Define the Chatbot class
|
| 12 |
+
class DocumentChatbot:
|
| 13 |
+
def __init__(self, model_name: str, embedding_model: str, documents_path: str, chroma_path: str):
|
| 14 |
+
self.model = ChatOpenAI(model=model_name, temperature=0)
|
| 15 |
+
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 16 |
+
self.embeddings = OpenAIEmbeddings(model=embedding_model)
|
| 17 |
+
self.db_chroma = self._load_documents(documents_path, chroma_path)
|
| 18 |
+
self.prompt_template = """
|
| 19 |
+
Answer the question based only on the following context:
|
| 20 |
+
{context}
|
| 21 |
+
Answer the question based on the above context: {question}.
|
| 22 |
+
Provide a detailed answer.
|
| 23 |
+
Don’t justify your answers.
|
| 24 |
+
Don’t give information not mentioned in the CONTEXT INFORMATION.
|
| 25 |
+
Do not say "according to the context" or "mentioned in the context" or similar.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
def _load_documents(self, documents_path: str, chroma_path: str):
|
| 29 |
+
# Load and process documents
|
| 30 |
+
loader = PyPDFDirectoryLoader(documents_path)
|
| 31 |
+
pages = loader.load_and_split(self.text_splitter)
|
| 32 |
+
db_chroma = Chroma.from_documents(pages, self.embeddings, persist_directory=chroma_path)
|
| 33 |
+
return db_chroma
|
| 34 |
+
|
| 35 |
+
def generate_response(self, message: str):
|
| 36 |
+
docs_chroma = self.db_chroma.similarity_search_with_score(message, k=5)
|
| 37 |
+
context_text = "\n\n".join([doc.page_content for doc, _score in docs_chroma])
|
| 38 |
+
prompt_template = ChatPromptTemplate.from_template(self.prompt_template)
|
| 39 |
+
prompt = prompt_template.format(context=context_text, question=message)
|
| 40 |
+
|
| 41 |
+
response = ""
|
| 42 |
+
for chunk in self.model.stream(prompt):
|
| 43 |
+
response += chunk.content
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
# Define the request model
|
| 47 |
+
class ChatRequest(BaseModel):
|
| 48 |
+
message: str
|
| 49 |
+
|
| 50 |
+
# Dependency Injection
|
| 51 |
+
def get_chatbot():
|
| 52 |
+
return DocumentChatbot(
|
| 53 |
+
model_name="gpt-4",
|
| 54 |
+
embedding_model="text-embedding-3-small",
|
| 55 |
+
documents_path="/content/drive/MyDrive/Test Documents", # Update this path as necessary
|
| 56 |
+
chroma_path="test-documents-2"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Initialize FastAPI app
|
| 60 |
+
app = FastAPI()
|
| 61 |
+
|
| 62 |
+
# API Endpoint
|
| 63 |
+
@app.post("/chat")
|
| 64 |
+
async def chat(request: ChatRequest, chatbot: DocumentChatbot = Depends(get_chatbot)):
|
| 65 |
+
try:
|
| 66 |
+
response = chatbot.generate_response(request.message)
|
| 67 |
+
return {"response": response}
|
| 68 |
+
except Exception as e:
|
| 69 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 70 |
+
|
| 71 |
+
# Optional: A health check endpoint
|
| 72 |
+
@app.get("/health")
|
| 73 |
+
async def health_check():
|
| 74 |
+
return {"status": "ok"}
|