omb8 commited on
Commit
321b18e
·
verified ·
1 Parent(s): 62d52a4

Upload streamlit.py

Browse files
Files changed (1) hide show
  1. streamlit.py +125 -0
streamlit.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import FreshStart_deploy.streamlit as st
3
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
4
+ from langchain_chroma import Chroma
5
+ from langchain.chains import create_history_aware_retriever, create_retrieval_chain
6
+ from langchain.chains.combine_documents import create_stuff_documents_chain
7
+ from langchain_core.messages import AIMessage, HumanMessage, BaseMessage
8
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
9
+ from langgraph.checkpoint.memory import MemorySaver
10
+ from langgraph.graph import START, StateGraph
11
+ from typing import Sequence
12
+ from typing_extensions import Annotated, TypedDict
13
+ from langchain_community.document_loaders import PyPDFLoader
14
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
15
+
16
+ from dotenv import load_dotenv
17
+
18
+ # Configure API key
19
+ load_dotenv()
20
+ api_key = os.getenv("GOOGLE_API_KEY")
21
+ os.environ["GOOGLE_API_KEY"] = api_key
22
+
23
+ # Initialize the Google Generative AI model
24
+ gemini_embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
25
+ model = ChatGoogleGenerativeAI(model="gemini-1.0-pro", convert_system_message_to_human=True)
26
+
27
+ # Load the document
28
+ document_loader = PyPDFLoader("/Users/maryam/Documents/UWF/our/chatbot/22_studenthandbook-22-23_f2.pdf")
29
+ doc = document_loader.load()
30
+
31
+ # Split documents
32
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
33
+ splits = text_splitter.split_documents(doc)
34
+
35
+ # Create a vector store and retriever
36
+ vectorstore = Chroma.from_documents(documents=splits, embedding=gemini_embeddings)
37
+ retriever = vectorstore.as_retriever()
38
+
39
+ # Set up prompts
40
+ contextualize_q_system_prompt = (
41
+ "Given a chat history and the latest user question "
42
+ "which might reference context in the chat history, "
43
+ "formulate a standalone question which can be understood "
44
+ "without the chat history."
45
+ )
46
+ contextualize_q_prompt = ChatPromptTemplate.from_messages(
47
+ [
48
+ ("system", contextualize_q_system_prompt),
49
+ MessagesPlaceholder("chat_history"),
50
+ ("human", "{input}"),
51
+ ]
52
+ )
53
+ history_aware_retriever = create_history_aware_retriever(model, retriever, contextualize_q_prompt)
54
+
55
+ # Create the question-answer chain
56
+ system_prompt = (
57
+ "You are an assistant for question-answering tasks. "
58
+ "Use the following pieces of retrieved context to answer "
59
+ "the question. If you don't know the answer, say that you "
60
+ "don't know."
61
+ "\n\n"
62
+ "{context}"
63
+ )
64
+ qa_prompt = ChatPromptTemplate.from_messages(
65
+ [
66
+ ("system", system_prompt),
67
+ MessagesPlaceholder("chat_history"),
68
+ ("human", "{input}"),
69
+ ]
70
+ )
71
+ question_answer_chain = create_stuff_documents_chain(model, qa_prompt)
72
+ rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
73
+
74
+ # State management with LangGraph
75
+ class State(TypedDict):
76
+ input: str
77
+ chat_history: Annotated[Sequence[BaseMessage], "add_messages"]
78
+ context: str
79
+ answer: str
80
+
81
+ def call_model(state: State):
82
+ response = rag_chain.invoke(state)
83
+ return {
84
+ "chat_history": [
85
+ HumanMessage(state["input"]),
86
+ AIMessage(response["answer"]),
87
+ ],
88
+ "context": response["context"],
89
+ "answer": response["answer"],
90
+ }
91
+
92
+ workflow = StateGraph(state_schema=State)
93
+ workflow.add_edge(START, "model")
94
+ workflow.add_node("model", call_model)
95
+ memory = MemorySaver()
96
+ app = workflow.compile(checkpointer=memory)
97
+
98
+ # Streamlit User Interface
99
+ st.title("Custom Question-Answering Chatbot")
100
+ st.write("Ask questions based on the loaded document.")
101
+
102
+ # Maintain chat history using Streamlit session state
103
+ if "chat_history" not in st.session_state:
104
+ st.session_state.chat_history = []
105
+
106
+ # User input section
107
+ user_input = st.text_input("Enter your question here:")
108
+
109
+ # Submit button
110
+ if st.button("Submit"):
111
+ if user_input:
112
+ # Prepare state and invoke the model
113
+ state = {"input": user_input, "chat_history": st.session_state.chat_history, "context": "", "answer": ""}
114
+ config = {"configurable": {"thread_id": "246"}}
115
+
116
+ result = app.invoke(state, config=config)
117
+
118
+ # Display response and update chat history
119
+ st.session_state.chat_history.append(HumanMessage(user_input))
120
+ st.session_state.chat_history.append(AIMessage(result["answer"]))
121
+
122
+ st.write("Chatbot:", result["answer"])
123
+ else:
124
+ st.write("Please enter a question.")
125
+