|
import os |
|
from langchain.embeddings.openai import OpenAIEmbeddings |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain.vectorstores import Qdrant |
|
from langchain.document_loaders import TextLoader |
|
from langchain.chains.question_answering import load_qa_chain |
|
from langchain.llms import OpenAI |
|
import gradio as gr |
|
from qdrant_client import QdrantClient |
|
from langchain.document_loaders import PagedPDFSplitter |
|
|
|
|
|
loader = PagedPDFSplitter("Philippine National Formulary 8th Edition.pdf") |
|
docs = loader.load_and_split() |
|
|
|
|
|
|
|
OPEN_API_KEY = os.environ["OPENAI_API_KEY"] |
|
host = os.environ["QDRANT_HOST"] |
|
api_key = os.environ["QDRANT_API_KEY"] |
|
embeddings = OpenAIEmbeddings() |
|
|
|
|
|
qdrant = Qdrant.from_documents(docs, embeddings, host=host, prefer_grpc=True, api_key=api_key) |
|
|
|
|
|
def question_answering(question): |
|
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff") |
|
query = question |
|
docs = qdrant.similarity_search(query) |
|
answer = chain.run(input_documents=docs, question=query) |
|
return answer |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("Start typing below and then click **Run** to see the output.") |
|
with gr.Row(): |
|
inp = gr.Textbox(placeholder="Ask question here?") |
|
out = gr.Textbox() |
|
btn = gr.Button("Run", api_name="search") |
|
btn.click(fn=question_answering, inputs=inp, outputs=out) |
|
|
|
demo.launch(debug=True) |
|
|
|
|