Spaces:
Runtime error
Runtime error
Download app.py from corpcarecc/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 4.75 kB
-
https://huggingface.co/spaces/corpcarecc/chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/corpcarecc/chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/corpcarecc/chatbot/resolve/main/app.py
4.75 kB
| import streamlit as st | |
| from time import sleep | |
| import qdrant_client | |
| from qdrant_client import models | |
| from llama_index.core import ChatPromptTemplate | |
| from llama_index.core.llms import ChatMessage, MessageRole | |
| from llama_index.embeddings.fastembed import FastEmbedEmbedding | |
| from llama_index.llms.groq import Groq | |
| from dotenv import load_dotenv | |
| import os | |
| from PIL import Image | |
| load_dotenv() | |
| QDRANT_API_URL = os.getenv('QDRANT_API_URL') | |
| QDRANT_API_KEY = os.getenv('QDRANT_API_KEY') | |
| GROQ_API = os.getenv('GROQ_API') | |
| def initialize_models(): | |
| embed_model = FastEmbedEmbedding(model_name=os.getenv('EMBEDDING_MODEL')) | |
| llm = Groq(model=os.getenv('LLM_MODEL'), | |
| api_key=GROQ_API) | |
| client = qdrant_client.QdrantClient( | |
| url=QDRANT_API_URL, | |
| api_key=QDRANT_API_KEY, | |
| prefer_grpc=True | |
| ) | |
| return embed_model, llm, client | |
| message_templates = [ | |
| ChatMessage( | |
| content=os.getenv('CHAT_TEMPLATE'), | |
| role=MessageRole.SYSTEM), | |
| ChatMessage( | |
| content=""" | |
| We have provided context information below. | |
| {context_str} | |
| --------------------- | |
| Given this information, please answer the question: {query} | |
| --------------------- | |
| If the question is not from the provided context, say `I don't know. Not enough information recieved.` | |
| """, | |
| role=MessageRole.USER, | |
| ), | |
| ] | |
| def search(query, client, embed_model, k=5): | |
| collection_name = os.getenv('COLLECTION_NAME') | |
| query_embedding = embed_model.get_query_embedding(query) | |
| result = client.query_points( | |
| collection_name=collection_name, | |
| query=query_embedding, | |
| limit=k | |
| ) | |
| return result | |
| def pipeline(query, embed_model, llm, client): | |
| # R - Retriever | |
| relevant_documents = search(query, client, embed_model) | |
| context = [doc.payload['context'] for doc in relevant_documents.points] | |
| context = "\n".join(context) | |
| # A - Augment | |
| chat_template = ChatPromptTemplate(message_templates=message_templates) | |
| # G - Generate | |
| response = llm.complete( | |
| chat_template.format( | |
| context_str=context, | |
| query=query) | |
| ) | |
| return response | |
| def extract_thinking_and_answer(response_text): | |
| """Extract thinking process and final answer from response""" | |
| try: | |
| thinking = response_text[response_text.find( | |
| "<think>") + 7:response_text.find("</think>")].strip() | |
| answer = response_text[response_text.find("</think>") + 8:].strip() | |
| return thinking, answer | |
| except: | |
| return "", response_text | |
| def main(): | |
| im = Image.open("favicon.png") | |
| st.set_page_config( | |
| page_title="CorpCare Chatbot", | |
| page_icon=im | |
| ) | |
| st.title("CorpCare Chatbot") | |
| # this will run only once, and be saved inside the cache | |
| embed_model, llm, client = initialize_models() | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| with st.sidebar: | |
| if st.button("Clear Chat"): | |
| st.session_state.messages = [] | |
| st.rerun() | |
| # Display chat messages | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| if message["role"] == "assistant": | |
| thinking, answer = extract_thinking_and_answer( | |
| message["content"]) | |
| with st.expander("Show thinking process"): | |
| st.markdown(thinking) | |
| st.markdown(answer) | |
| else: | |
| st.markdown(message["content"]) | |
| # Chat input | |
| if prompt := st.chat_input("Ask your question about the CC knowledge Repo..."): | |
| # Display user message | |
| st.chat_message("user").markdown(prompt) | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| # Generate and display response | |
| with st.chat_message("assistant"): | |
| message_placeholder = st.empty() | |
| with st.spinner("Thinking..."): | |
| full_response = pipeline(prompt, embed_model, llm, client) | |
| thinking, answer = extract_thinking_and_answer( | |
| full_response.text) | |
| with st.expander("Show thinking process"): | |
| st.markdown(thinking) | |
| response = "" | |
| for chunk in answer.split(): | |
| response += chunk + " " | |
| message_placeholder.markdown(response + "▌") | |
| sleep(0.05) | |
| message_placeholder.markdown(answer) | |
| # Add assistant response to history | |
| st.session_state.messages.append( | |
| {"role": "assistant", "content": full_response.text}) | |
| if __name__ == "__main__": | |
| main() | |