File size: 4,436 Bytes
6773e36
3c2aa29
 
6773e36
3c2aa29
6773e36
 
3c2aa29
6773e36
c65ff09
8fccf3e
 
3c2aa29
6773e36
3c2aa29
6773e36
 
3c2aa29
6773e36
 
3c2aa29
6773e36
3c2aa29
6773e36
 
 
3c2aa29
6773e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a26f07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6773e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba4dea2
 
6773e36
ba4dea2
6773e36
 
 
699d994
94ce4e1
 
699d994
 
6773e36
699d994
6773e36
8fccf3e
 
 
 
 
94ce4e1
8fccf3e
 
3c2aa29
6773e36
 
c65ff09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba4dea2
c65ff09
 
 
699d994
c65ff09
3c2aa29
 
 
f716764
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
import pandas as pd


df = pd.read_csv('./drugs_side_effects_drugs_com.csv')

df = df[['drug_name', 'medical_condition', 'side_effects']]
df.dropna(inplace=True)

context_data = []
for i in range(2):
    context = " | ".join([f"{col}: {df.iloc[i][col]}" for col in df.columns])
    context_data.append(context)

import os

# Get the secret key from the environment
groq_key = os.environ.get('gloq_key')

## LLM used for RAG
from langchain_groq import ChatGroq

llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key)

## Embedding model!
from langchain_huggingface import HuggingFaceEmbeddings
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")

# create vector store!
from langchain_chroma import Chroma

vectorstore = Chroma(
    collection_name="medical_dataset_store",
    embedding_function=embed_model,
    persist_directory="./",
)

# add data to vector nstore
vectorstore.add_texts(context_data)

retriever = vectorstore.as_retriever()

from langchain_core.prompts import PromptTemplate

template = ("""
    You are CareBot, a pharmacist and medical expert known as Treasure. Your goal is to provide empathetic, supportive, and detailed responses tailored to the user's needs.
    Use the provided context to answer the question. If the question is related to medical condition, drug name and side effects that are not in the context, look online and answer them.
    
    Behavior Guidelines:
    1. Introduction: Greet the user as Treasure during the first interaction.
    2. Personalization: Adapt responses to the user's tone and emotional state.
    3. Empathy: Respond warmly to the user's concerns and questions.
    4. Evidence-Based: Use reliable sources to answer queries. For missing data, advise seeking professional consultation.
    5. Focus: Avoid providing off-topic information; address the user's query specifically.
    6. Encouragement: Balance acknowledging concerns with actionable and constructive suggestions.
    7. Context Integration: Use the given context to deliver accurate and relevant answers without repeating the context explicitly.
    
    Objective:
    Deliver thoughtful, empathetic, and medically sound advice based on the user’s query.
    
    Response Style:
    - Detailed but concise
    - Professional, empathetic tone
    - Clear and actionable guidance

    Context: {context}

    Question: {question}

    Answer:""")

rag_prompt = PromptTemplate.from_template(template)

from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

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


import gradio as gr

# Function to stream responses
def rag_memory_stream(message, history):
    partial_text = ""
    for new_text in rag_chain.stream(message):  # Assuming rag_chain is pre-defined
        partial_text += new_text
        yield partial_text

examples = [
    "What are the side effects of aspirin?",
    "Can ibuprofen cause dizziness?"
]

# Title and description for the app
title = "CareBot: AI Medical Assistant for Drug Information and Side Effects"
description = """
This AI-powered chatbot provides reliable information about drugs, their side effects, and related medical conditions. 
Powered by the Groq API and LangChain, it delivers real-time, accurate responses.

Example Questions:
- What are the side effects of aspirin?
- Can ibuprofen cause dizziness?

Disclaimer: This chatbot is for informational purposes only and not a substitute for professional medical advice.
"""

# Customizing Gradio interface for a better look
# demo = gr.Interface(
#     fn=rag_memory_stream,
#     inputs=gr.Textbox(
#         lines=5, 
#         placeholder="Type your medical question here...", 
#         label="Your Medical Question"
#     ),
#     outputs=gr.Textbox(
#         lines=15,  # Reduced line count for better layout
#         label="AI Response"
#     ),
#     title=title,
#     description=description,
#     allow_flagging="never"
# )

demo = gr.ChatInterface(fn=rag_memory_stream,
                        type="messages",
                        title=title,
                        description=description,
                        fill_height=True,
                        examples=examples,
                        theme="glass",
)

if __name__ == "__main__":
    demo.launch()