Spaces:
Runtime error
Runtime error
| 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() |