Haspit1 / app.py
nitind1's picture
Upload app.py with huggingface_hub
79c20b6 verified
Raw History Blame Contribute Delete
15.9 kB
######################## WRITE YOUR CODE HERE #########################
# Import libraries
import os
import chromadb
from dotenv import load_dotenv
import json
import torch
# LangChain imports
from langchain_core.documents import Document
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain.prompts import ChatPromptTemplate
from langchain.chains.query_constructor.base import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor, CrossEncoderReranker
from langchain.retrievers import ContextualCompressionRetriever
# LangChain community & experimental imports
from langchain_community.vectorstores import Chroma
from langchain_community.document_loaders import PyPDFDirectoryLoader, PyPDFLoader
from langchain_community.cross_encoders import HuggingFaceCrossEncoder
from langchain_experimental.text_splitter import SemanticChunker
from langchain.text_splitter import (
CharacterTextSplitter,
RecursiveCharacterTextSplitter
)
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
# LangChain OpenAI imports
from langchain_openai import AzureOpenAIEmbeddings, AzureChatOpenAI
from langchain.memory import ConversationSummaryBufferMemory
from langchain_openai import ChatOpenAI
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_openai import OpenAIEmbeddings
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_community.agent_toolkits import create_sql_agent
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
# LlamaParse & LlamaIndex imports
from llama_parse import LlamaParse
from llama_index.core import Settings, SimpleDirectoryReader
# LangGraph import
from langgraph.graph import StateGraph, END, START
# Pydantic import
from pydantic import BaseModel
# Typing imports
from typing import Dict, List, Tuple, Any, TypedDict
# Other utilities
import numpy as np
import streamlit as st
from datetime import datetime
from typing import List
from langchain_core.callbacks import CallbackManagerForRetrieverRun
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever
from unstructured.partition.pdf import partition_pdf
from unstructured.partition.text import partition_text
from langchain_community.document_loaders import DirectoryLoader
torch.classes.__path__ = []
#====================================SETUP=====================================#
# Fetch secrets from Hugging Face Spaces
api_key = os.environ["API_KEY"]
endpoint = os.environ["OPENAI_API_BASE"]
llama_api_key = os.environ['LLAMA_API_KEY']
# Initializing the OpenAI embedding function for Chroma
embedding_function = chromadb.utils.embedding_functions.OpenAIEmbeddingFunction(
api_base=endpoint, # Complete the code to define the API base endpoint
api_key=api_key, # Complete the code to define the API key
model_name='text-embedding-ada-002' # This is a fixed value and does not need modification
)
# Initializing the OpenAI Embeddings
embedding_model = OpenAIEmbeddings(
openai_api_base=endpoint,
openai_api_key=api_key,
model='text-embedding-ada-002'
)
# Initializing the Chat OpenAI model
llm = ChatOpenAI(
openai_api_base=endpoint,
openai_api_key=api_key,
model="gpt-4o-mini",
streaming=False
)
# set the LLM and embedding model in the LlamaIndex settings.
Settings.llm = llm # Complete the code to define the LLM model
Settings.embedding = embedding_model # Complete the code to define the embedding model
#================================Creating RAG======================#
class AgentState(TypedDict):
query: str # The current user query
context: List[Dict[str, Any]] # Retrieved documents (content and metadata)
response: str
semanticstore = Chroma(
collection_name="semantic_chunks",
persist_directory="./new_db",
embedding_function=embedding_model
)
# Create a retriever from the vector store
retriever = semanticstore.as_retriever(
search_type='similarity',
search_kwargs={'k': 10}
)
metadata_field_info = [
AttributeInfo(
name="Document",
description="Title of the document",
type="string"
),
AttributeInfo(
name="page_content",
description="content of the original page",
type="string"
)
]
document_name = "source"
structured_retriever = SelfQueryRetriever.from_llm(
llm,
semanticstore,
document_name,
metadata_field_info
)
def retrieve_documents(query: str):
"""
Retrieves matching documents with the user query.
Args:
query (str): The current user query.
Returns:
Dict (str): The retrieved response.
"""
retrieved_documents = []
retrieved_documents = structured_retriever.invoke(query)
retrieved_documents.extend(structured_retriever.invoke(query))
return retrieved_documents
def expand_and_retrieve(query, llm=llm, structured_retriever=structured_retriever):
# Query Expansion
query_expansion_prompt = """
Perform query expansion on the user query received. If there are multiple common ways of phrasing a user question \
or common synonyms for key words in the question, make sure to return multiple versions \
of the query with the different phrasings.
If the query has multiple parts, split them into separate simpler queries. This is the only case where you can generate more than 3 queries.
If there are acronyms or words you are not familiar with, do not try to rephrase them.
Return only 3 versions of the query as a list.
Generate only a list of queries. Do not mention anything before or after the list.
Question:
{question}"""
#query=state[query]
# Get query expansions
#print("Expanding the user Query")
query_expansions = llm.invoke(query_expansion_prompt.format(question=query))
query_expansions_list = query_expansions.content.split("\n")
#print(query_expansions_list)
# Retrieve context for each expanded query
context_list = []
for question in query_expansions_list:
retrieved = retrieve_documents(query)
context_list.extend(retrieved)
#return only unique docs
unique_context_dict = {doc.metadata['source']: doc for doc in context_list}
unique_context_list = list(unique_context_dict.values())
#print("Unique documents retrieved")
return unique_context_list
class CustomRetriever(BaseRetriever):
"""A custom retriever that contains the top k documents that contain the user query.
This retriever only implements the sync method _get_relevant_documents.
"""
def _get_relevant_documents(
self, query: str, *, run_manager: CallbackManagerForRetrieverRun
) -> List[Document]:
"""Sync implementations for retriever."""
#state = {"query": query, "context_list": []}
context_list = []
#responses = structured_retriever.invoke(query)
context_list = expand_and_retrieve(query, llm=llm, structured_retriever=structured_retriever)
return context_list
crossencoder = HuggingFaceCrossEncoder(model_name="cross-encoder/ms-marco-MiniLM-L-6-v2")
reranker = CrossEncoderReranker(model=crossencoder, top_n=10)
reranker_retriever = ContextualCompressionRetriever(base_compressor=reranker, base_retriever=CustomRetriever())
@tool
def agentic_rag(query: str):
"""
Runs the RAG-based agent with conversation history for context-aware responses.
Args:
query(str): The current user query.
Returns:
Dict (str): The retrieved response.
"""
# Initialize state with necessary parameters
inputs = {
"query": query,
"context_list": []
}
output = reranker_retriever.invoke(query)
#output = expand_and_retrieve(inputs)
#reranked_docs = reranker_retriever.invoke(query)
#response = reranked_docs
#tables_retrieved = []
#context_for_llm = []
#for doc in reranked_docs:
#if doc.metadata['type'] == 'table':
#tables_retrieved.append(doc)
#else:
#reranked_docs
return output
class HaspitBot:
def __init__(self):
"""
Initialize the HaspitBot class, the LLM client, tools, and the agent executor.
"""
# Initialize the OpenAI client using the provided credentials
self.client = ChatOpenAI(
model_name="gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version)
api_key= os.environ["API_KEY"], # API key for authentication
openai_api_base = os.environ["OPENAI_API_BASE"],
temperature=0 # Controls randomness in responses; 0 ensures deterministic results
)
# Define tools available to the chatbot, such as web search
tools = [agentic_rag]
# Define the system prompt to set the behavior of the chatbot
system_prompt = """
You are a knowledgable agent, specializing in retrieving relevant documents.
Guidelines for Interaction:
Always use the agentic_rag tool to retrieve up-to-date documents.
"""
# Build the prompt template for the agent
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt), # System instructions
("human", "{input}"), # Placeholder for human input
("placeholder", "{agent_scratchpad}") # Placeholder for intermediate reasoning steps
])
# Create an agent capable of interacting with tools and executing tasks
agent = create_tool_calling_agent(self.client, tools, prompt)
# Wrap the agent in an executor to manage tool interactions and execution flow
self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
def handle_customer_query(self, query: str) -> str:
"""
Process a customer's query and provide a response, taking into account past interactions.
Args:
query (str): Customer's query.
Returns:
str: Chatbot's response.
"""
# Retrieve relevant past interactions for context
#relevant_history = self.get_relevant_history(user_id, query)
# Build a context string from the relevant history
#context = "Previous relevant interactions:\n"
#for memory in relevant_history:
#context += f"Customer: {memory['memory']}\n" # Customer's past messages
#context += f"Support: {memory['memory']}\n" # Chatbot's past responses
#context += "---\n"
# Print context for debugging purposes
#print("Context: ", context)
# Prepare a prompt combining past context and the current query
prompt = f"""
Current customer query: {query}
Retrieve relevant documents that match the customer query.
"""
# Generate a response using the agent
response = self.agent_executor.invoke({"input": prompt})
# Store the current interaction for future reference
#self.store_customer_interaction(
#user_id=user_id,
#message=query,
#response=response["output"],
#metadata={"type": "support_query"}
#)
# Return the chatbot's response
return response['output']
#=====================User Interface using streamlit ===========================#
def haspit_search_streamlit():
"""
A Streamlit-based UI for Semantic Search.
"""
st.title("Haspit Document Search")
st.write("Ask me to search for relevant documents on Haspit.")
st.write("Type 'exit' to end the search session.")
# Initialize session state for chat history and user_id if they don't exist
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'user_id' not in st.session_state:
st.session_state.user_id = None
# Login form: Only if user is not logged in
if st.session_state.user_id is None:
with st.form("login_form", clear_on_submit=True):
user_id = st.text_input("Please enter your name to begin:")
submit_button = st.form_submit_button("Login")
if submit_button and user_id:
st.session_state.user_id = user_id
st.session_state.chat_history.append({
"role": "assistant",
"content": f"Welcome, {user_id}! How can I help you with your search today?"
})
st.session_state.login_submitted = True # Set flag to trigger rerun
if st.session_state.get("login_submitted", False):
st.session_state.pop("login_submitted")
st.rerun()
else:
# Display chat history
for message in st.session_state.chat_history:
with st.chat_message(message["role"]):
st.write(message["content"])
# Chat input with custom placeholder text
user_query = st.chat_input("Type your search here (or exit to end): ", key="chat_input")
if user_query:
if user_query.lower() == "exit":
st.session_state.chat_history.append({"role": "user", "content": "exit"})
with st.chat_message("user"):
st.write("exit")
goodbye_msg = "Goodbye! Feel free to return if you want to search for content in documents on Haspit."
st.session_state.chat_history.append({"role": "assistant", "content": goodbye_msg})
with st.chat_message("assistant"):
st.write(goodbye_msg)
st.session_state.user_id = None
st.rerun()
return
st.session_state.chat_history.append({"role": "user", "content": user_query})
with st.chat_message("user"):
st.write(user_query)
# Filter input using Llama Guard
#filtered_result = filter_input_with_llama_guard(user_query)
#filtered_result = filtered_result.replace("\n", " ") # Normalize the result
# Check if input is safe based on allowed statuses
#if filtered_result in ["safe", "unsafe S6", "unsafe S7"]:
#try:
#if 'chatbot' not in st.session_state:
st.session_state.chatbot = HaspitBot()
response = st.session_state.chatbot.handle_customer_query(user_query)
st.write(response)
st.session_state.chat_history.append({"role": "assistant", "content": response})
#else:
#response = st.session_state.chatbot.handle_customer_query(user_query)
#st.write(response)
#st.session_state.chat_history.append({"role": "assistant", "content": response})
#error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
#st.write(error_msg)
#st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
#else:
#inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
#st.write(inappropriate_msg)
#st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
if __name__ == "__main__":
haspit_search_streamlit()