Spaces:
Runtime error
Runtime error
File size: 3,069 Bytes
7293656 de79e7e 7293656 de79e7e 7293656 fe8ed55 7293656 fe8ed55 7293656 fe8ed55 7293656 | 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 | import os
'''
client = Groq(
# This is the default and can be omitted
api_key=os.environ.get("GROQ_API_KEY"),
)
chat_streaming = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a professional Data Engineer."},
{"role": "user", "content": "Can you explain how the data lake works?"},
],
model="llama-3.1-8b-instant",
temperature=0.3,
max_tokens=1200,
top_p=1,
stop=None,
stream=True,
)
for chunk in chat_streaming:
print(chunk.choices[0].delta.content, end="")
'''
from langchain_groq import ChatGroq
llm = ChatGroq(model="llama-3.1-8b-instant")
from langchain_huggingface import HuggingFaceEmbeddings
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
REVIEWS_CSV_PATH = "/content/reviews.csv"
# Path to the CSV file containing reviews /content/reviews.csv
REVIEWS_CHROMA_PATH = "chroma_data"
# Path to store the Chroma vector database data
# Load the reviews from the CSV file
loader = CSVLoader(file_path=REVIEWS_CSV_PATH, source_column="review")
# CSVLoader loads data from a CSV file.
# `source_column="review"` specifies that the column named "review" contains the text to be loaded.
reviews = loader.load()
# Loads the reviews as a list of documents, each document containing the text from the "review" column.
#print(reviews)
from langchain_chroma import Chroma
vectorstore = Chroma.from_documents(
documents=reviews,
embedding=embed_model,
persist_directory=REVIEWS_CHROMA_PATH,
)
retriever = vectorstore.as_retriever()
from langchain_core.prompts import PromptTemplate
template = ("""Your job is to use patient reviews to answer questions about their experience at a hospital.
Use the following context to answer questions.
Be as detailed as possible, but don't make up any information that's not from the context.
If you don't know an answer, say you don't know.
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()
)
from IPython.display import display, Markdown
response = rag_chain.invoke("Has anyone complained about communication with the hospital staff??")
Markdown(response)
query = "Who all had complains about communicaation with hospital staff?"
for chunk in rag_chain.stream(query):
print(chunk, end="")
import gradio as gr
def rag_memory(text):
response = rag_chain.invoke(text)
return response
description = "Real-time Hospital - Patient Review AI App with Groq API and LangChain"
demo = gr.Interface(
description=description,
fn=rag_memory,
inputs="Ask Query regarding Patient Feedback",
outputs="AI response",
live=True,
batch=True,
max_batch_size=10000,
)
demo.queue(max_size=300000)
demo.launch() |