File size: 3,750 Bytes
d228ce2
e8825fb
d228ce2
 
e8825fb
 
 
 
 
d228ce2
 
 
 
 
 
 
 
 
 
e8825fb
 
63be344
e8825fb
63be344
 
e8825fb
 
 
 
 
 
63be344
 
d228ce2
 
ccb8b8b
d228ce2
eed7904
d228ce2
 
e8825fb
d228ce2
 
 
 
 
 
0d92739
d228ce2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8825fb
d228ce2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4a65c1
 
 
 
d228ce2
b4a65c1
 
 
 
 
d228ce2
 
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
import os
from typing import Iterator, Iterable
from tempfile import TemporaryDirectory
from dotenv import load_dotenv
import gradio as gr

from langchain_core.document_loaders import BaseLoader
from langchain_core.documents import Document as LCDocument
from docling.document_converter import DocumentConverter
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
from langchain_milvus import Milvus
from langchain_huggingface import HuggingFaceEndpoint
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

class DoclingPDFLoader(BaseLoader):
    def __init__(self, file_paths: str | list[str]) -> None:
        self._file_paths = file_paths if isinstance(file_paths, list) else [file_paths]
        self._converter = DocumentConverter()

    def lazy_load(self) -> Iterator[LCDocument]:
        for source in self._file_paths:
            dl_doc = self._converter.convert(source).document
            text = dl_doc.export_to_markdown()
            yield LCDocument(page_content=text)
            
    def load(self) -> list[LCDocument]:
        return list(self.lazy_load())

# Load environment variables
load_dotenv()

# File paths configuration
FILE_PATHS = ["vol1.txt", "vol2.txt", "vol3.txt", "vol4.txt", "vol5.txt"]

# Load and split documents
loader = DoclingPDFLoader(file_paths=FILE_PATHS)
docs = loader.load()

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,
    chunk_overlap=200,
)
splits = text_splitter.split_documents(FILE_PATHS)

# Set up embeddings
HF_EMBED_MODEL_ID = "BAAI/bge-small-en-v1.5"
embeddings = HuggingFaceEmbeddings(model_name=HF_EMBED_MODEL_ID)

# Configure Milvus
MILVUS_URI = os.environ.get(
    "MILVUS_URI", f"{(tmp_dir := TemporaryDirectory()).name}/milvus_demo.db"
)

# Initialize vector store
vectorstore = Milvus.from_documents(
    splits,
    embeddings,
    connection_args={"uri": MILVUS_URI},
    drop_old=True,
    index_params={
        "index_type": "IVF_FLAT",
        "metric_type": "L2",
        "params": {"nlist": 100}
    },
)

# Set up LLM
HF_API_KEY = os.environ.get("HF_TOKEN")
HF_LLM_MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
llm = HuggingFaceEndpoint(
    repo_id=HF_LLM_MODEL_ID,
    huggingfacehub_api_token=HF_API_KEY,
)

# FastAPI setup
app = FastAPI()

class QueryRequest(BaseModel):
    question: str

def format_docs(docs: Iterable[LCDocument]):
    return "\n\n".join(doc.page_content for doc in docs)

# Create RAG chain
retriever = vectorstore.as_retriever()
prompt = PromptTemplate.from_template(
    "Context information is below.\n---------------------\n{context}\n---------------------\nGiven the context information and not prior knowledge, answer the query.\nQuery: {question}\nAnswer:\n"
)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

@app.post("/query")
async def query_documents(request: QueryRequest):
    try:
        response = rag_chain.invoke(request.question)
        return {"answer": response}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Gradio interface
def ask_question(question):
    return rag_chain.invoke(question)

if __name__ == "__main__":
    # Launch both FastAPI and Gradio
    iface = gr.Interface(fn=ask_question, inputs="text", outputs="text")
    iface.launch(server_name="0.0.0.0", server_port=7860)
    
    # FastAPI runs on a different port
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)