Spaces:
Runtime error
Runtime error
File size: 6,362 Bytes
323a8f6 fad2203 40b269a 323a8f6 2cc1867 323a8f6 2cc1867 323a8f6 2cc1867 323a8f6 2cc1867 323a8f6 4f8307a 2cc1867 323a8f6 2cc1867 323a8f6 2cc1867 323a8f6 2cc1867 323a8f6 2cc1867 323a8f6 d7e2f4e 2cc1867 323a8f6 2cc1867 323a8f6 fad2203 323a8f6 2cc1867 65e7c71 2cc1867 62ab84e 323a8f6 |
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# -*- coding: utf-8 -*-
"""ai_app.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1wUztAR4EdQUL3vkpM3Is-ps0TEocClry
"""
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
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
import os
openai_api_key = os.getenv('openai_api_key')
qdrant_url = os.getenv('QDRANT_URL')
qdrant_api_key = os.getenv('qdrant_api_key')
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage
#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)
#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",
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)
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, including but not limited to surgeons, dentists, dermatologists, cardiologists, neurologists, etc., based on the user's query and the provided 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 analyze the user's question:
User's question:\n{question}\n
Your goal is to understand the user's needs and match them with the most relevant healthcare professional(s) from the provided context.
- Reflect on why the question was asked, and deliver 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 comprehensive, logical, and medically accurate answer. When generating the answer, include the following details about the healthcare professional:
• Name of the Professional
• City
• Specialization (e.g., Surgeon, Dentist, Cardiologist, etc.)
• Qualification (e.g., MBBS, FCPS, etc.)
• Years of Experience
• Patient Satisfaction Rate (if available)
• Average Time Spent with Patients (if available)
• Wait Time (if available)
• Hospital/Clinic Address
• Consultation Fee
• Profile Link (if available)
4. If the retrieved context does not contain enough relevant information, or if the documents are irrelevant, respond with 'I can't find the answer to that question in the material I have'.
5. Provide a complete answer to the user. Do not limit the information if there is more useful data available in the retrieved context.
6. At the end of the response, do not include any unnecessary metadata (such as Source, Row, or _id). Only focus on the healthcare professional's information relevant to the user's query.
# Question:
{question}""",
input_variables=["context", "question"]
)
llm = ChatOpenAI(temperature=0.5, model='gpt-4o', 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)
#import strw
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
rag_chain = (
{"context": retriver| format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage
import openai
import gradio as gr
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="Doctors info Assist",
theme="soft",
)
demo.launch(show_api=False)
|