Doc_App_Bot / app.py
net2asif's picture
Update app.py
bae401f verified
raw
history blame
8.61 kB
# -*- coding: utf-8 -*-
"""ai_app.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1wUztAR4EdQUL3vkpM3Is-ps0TEocClry
"""
# !pip install langchain openai qdrant-client gradio pandas tiktoken -U langchain-community
# from google.colab import userdata
# openai_api_key=userdata.get('openai_api_key')
import gradio as gr
import pandas as pd
from langchain.document_loaders.csv_loader import CSVLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Qdrant
from langchain.chains import VectorDBQA
from langchain.llms import OpenAI
# qdrant_url=userdata.get('Qdrant')
# qdrant_api_key=userdata.get('qdrant_api_key')
# openai_api_key=userdata.get('openai_api_key')
# # groq_api_key=userdata.get('GROQ_API_KEY')
import os
openai_api_key = os.getenv('openai_api_key')
qdrant_url = os.getenv('QDRANT_URL')
qdrant_api_key = os.getenv('qdrant_api_key')
# Now you can use these keys in your application
#csv loader
loader = CSVLoader(file_path='data.csv')
data=loader.load()
#split the documnts
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
texts = text_splitter.split_documents(data)
len(texts)
#embeding
embeding=OpenAIEmbeddings(openai_api_key=openai_api_key, model="text-embedding-3-small")
#import quantization
from langchain.vectorstores import Qdrant
from qdrant_client import QdrantClient, models
from langchain.vectorstores import Qdrant
#using qudadrant vector database
from qdrant_client import QdrantClient, models
qdrant = Qdrant.from_documents(
texts,
embeding,
url=qdrant_url,
prefer_grpc=True,
api_key=qdrant_api_key,
collection_name="llm_app_01",
quantization_config=models.BinaryQuantization(
binary=models.BinaryQuantizationConfig(
always_ram=True,
)
)
)
#qdrant client
qdrant_client = QdrantClient(
url=qdrant_url,
prefer_grpc=True,
api_key=qdrant_api_key,
)
from re import search
#retriver
retriver=qdrant.as_retriever( search_type="similarity", search_kwargs={"k":2})
#search query
query="show me a best darmatology doctor in peshawar "
docs=retriver.get_relevant_documents(query)
#write a code for prety print
# for i in docs:
# print(i.page_content)
# docs[0].metadata.items()
from langchain import PromptTemplate
prompt = PromptTemplate(
template="""
# Your Role
You are a highly skilled AI specialized in healthcare and medical information retrieval. Your expertise lies in understanding the medical needs of patients and accurately matching them with the most suitable healthcare professionals based on the given context.
# Instruction
Your task is to answer the question using the following pieces of retrieved context delimited by XML tags.
<retrieved context>
Retrieved Context:
{context}
</retrieved context>
# Constraint
1. Carefully consider the user's question:
User's question:\n{question}\n
Analyze the intent behind the question, particularly in relation to the medical context, and provide a precise and helpful answer.
- Reflect on why the question was asked and provide an appropriate response based on the context you understand.
2. Select the most relevant information (the key details directly related to the question) from the retrieved context and use it to formulate an answer.
3. Generate a concise, logical, and medically accurate answer. When generating the answer, include the following details about the doctor in a bulleted format:
• Doctor Name: Dr. Shahzad Rashid Awan
• City: Peshawar
• Specialization: Dermatologist
• Qualification: MBBS, MCPS (Dermatology)
• Experience: 12 years
• Patient Satisfaction Rate: 93%
• Avg Time to Patients: 13 mins
• Wait Time: 10 mins
• Hospital Address: Rahim Medical Center And Hospital, Hasht Nagri, Peshawar
• Fee: PKR 1000
• Profile Link: https://www.marham.pk/doctors/peshawar/dermatologist/dr-shahzad-rashid-awan#reviews-scroll
4. If the retrieved context does not contain information relevant to the question, or if the documents are irrelevant, respond with 'I can't find the answer to that question in the material I have'.
5. Limit the answer to five sentences maximum. Ensure the answer is concise, logical, and medically appropriate.
6. At the end of the response, provide the doctor's profile metadata as shown in the relevant documents, ensuring all bullet points are clearly mentioned.
# Question:
{question}""",
input_variables=["context", "question"]
)
# #import conversation
# from langchain.memory import ConversationBufferMemory
# memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# !pip install langchain-openai
#import ChatOpenAI
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(model_name="gpt-4o", temperature=0, openai_api_key=openai_api_key)
def format_docs(docs):
formatted_docs = []
for doc in docs:
# Format the metadata into a string
metadata_str = ', '.join(f"{key}: {value}" for key, value in doc.metadata.items())
# Combine page content with its metadata
doc_str = f"{doc.page_content}\nMetadata: {metadata_str}"
# Append to the list of formatted documents
formatted_docs.append(doc_str)
# Join all formatted documents with double newlines
return "\n\n".join(formatted_docs)
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
rag_chain = (
{"context": retriver| format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
rag_chain.invoke("show me a best darmatology doctor in lahore ")
# import random
# import gradio as gr
# # Gradio Interface
# def search_doctor(input_text):
# return rag_chain.invoke(input_text)
# # Create the Gradio interface
# iface = gr.Interface(
# fn=search_doctor,
# inputs=gr.Textbox(lines=1, label="Ask a medical question"),
# outputs=gr.Textbox(label="Answer"),
# title="Medical Assistant",
# description="Find the best doctors based on your medical needs.",
# allow_flagging="never",
# theme="default",
# css=".gradio-container {border-radius: 10px; padding: 10px; background-color: #f9f9f9;} .gr-button {visibility: hidden;}"
# )
# # Launch the interface without the Gradio logo
# iface.launch(show_api=False)
# import gradio as gr
# # Example RAG model invocation function (replace with your actual function)
# def rag_model_query(query):
# # Replace with actual RAG model invocation
# return rag_chain.invoke(query)
# # Define the Gradio function to handle both echo and RAG queries
# def handle_message(message, history):
# # Check if the message contains a keyword to trigger RAG model
# if "doctor" in message["text"].lower():
# response = rag_model_query(message["text"])
# else:
# response = message["text"]
# return response
# # Create the Gradio interface
# demo = gr.ChatInterface(
# fn=handle_message,
# title="Medical Assistant",
# multimodal=True,
# )
# demo.launch()
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage
import openai
import os
import gradio as gr
# os.environ["OPENAI_API_KEY"] = openai_api_key # Replace with your key
llm = ChatOpenAI(temperature=1.0, model='gpt-4o', openai_api_key=openai_api_key)
# llm = ChatOpenAI(model_name="gpt-4o", temperature=0, openai_api_key=openai_api_key, memory=memory)
def reg(message, history):
history_langchain_format = []
for human, ai in history:
history_langchain_format.append(HumanMessage(content=human))
history_langchain_format.append(AIMessage(content=ai))
history_langchain_format.append(HumanMessage(content=message))
gpt_response = llm(history_langchain_format)
return rag_chain.invoke(message)
# # Gradio ChatInterface
# demo = gr.ChatInterface(
# fn=reg,
# title="Medical Assistant",
# # theme="soft",
# )
# demo.launch(show_api=False)
gr.ChatInterface(predict).launch()