Kuldip2411 commited on
Commit
f8817e5
1 Parent(s): 8f4d57a

Upload 5 files

Browse files
Files changed (5) hide show
  1. app.py +106 -104
  2. app_config.py +13 -19
  3. faiss_index/index.faiss +0 -0
  4. faiss_index/index.pkl +3 -0
  5. requirements.txt +11 -11
app.py CHANGED
@@ -1,104 +1,106 @@
1
- import streamlit as st
2
- import random
3
- from app_config import SYSTEM_PROMPT, NLP_MODEL_NAME, NUMBER_OF_VECTORS_FOR_RAG, NLP_MODEL_TEMPERATURE, NLP_MODEL_MAX_TOKENS, VECTOR_MAX_TOKENS
4
- from functions import get_vectorstore_with_doc_from_pdf, tiktoken_len, get_vectorstore_with_doc_from_word
5
- from langchain.memory import ConversationSummaryBufferMemory
6
- from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
7
- from langchain.chains.summarize import load_summarize_chain
8
- from langchain.prompts import PromptTemplate
9
- from langchain_groq import ChatGroq
10
- from dotenv import load_dotenv
11
- from pathlib import Path
12
- import os
13
- from streamlit_pdf_viewer import pdf_viewer
14
- env_path = Path('.') / '.env'
15
- load_dotenv(dotenv_path=env_path)
16
-
17
- def response_generator(prompt: str) -> str:
18
- """this function can be used for general quetion answers which are related to tyrex and tyre recycling
19
-
20
- Args:
21
- prompt (string): user query
22
-
23
- Returns:
24
- string: answer of the query
25
- """
26
-
27
- try:
28
- retriever = st.session_state.retriever
29
- docs = retriever.invoke(prompt)
30
- my_context = [doc.page_content for doc in docs]
31
- my_context = '\n\n'.join(my_context)
32
-
33
- system_message = SystemMessage(content = SYSTEM_PROMPT.format(context=my_context, previous_message_summary=st.session_state.rag_memory.moving_summary_buffer))
34
- chat_messages = (system_message + st.session_state.rag_memory.chat_memory.messages + HumanMessage(content=prompt)).messages
35
- print("total tokens: ", tiktoken_len(str(chat_messages)))
36
- # print("my_context*********",my_context)
37
- response = st.session_state.llm.invoke(chat_messages)
38
- return response.content
39
-
40
- except Exception as error:
41
- print(error)
42
- return "Oops! something went wrong, please try again."
43
-
44
-
45
- st.markdown(
46
- """
47
- <style>
48
- .st-emotion-cache-janbn0 {
49
- flex-direction: row-reverse;
50
- text-align: right;
51
- }
52
- </style>
53
- """,
54
- unsafe_allow_html=True,
55
- )
56
-
57
- # When user gives input
58
- with st.sidebar:
59
- st.header("Hitachi Support Bot")
60
- button = st.toggle("View Doc file.")
61
-
62
- if button:
63
- pdf_viewer("GPT OUTPUT.pdf")
64
- else:
65
- print("SYSTEM MESSAGE")
66
- if "messages" not in st.session_state:
67
- st.session_state.messages=[{"role": "system", "content": SYSTEM_PROMPT}]
68
-
69
- print("SYSTEM MODEL")
70
- if "llm" not in st.session_state:
71
- st.session_state.llm = ChatGroq(temperature=NLP_MODEL_TEMPERATURE, groq_api_key=str(os.getenv('GROQ_API_KEY')), model_name=NLP_MODEL_NAME)
72
-
73
- print("rag")
74
- if "rag_memory" not in st.session_state:
75
- st.session_state.rag_memory = ConversationSummaryBufferMemory(llm=st.session_state.llm, max_token_limit= 5000)
76
-
77
- print("retrival")
78
- if "retriever" not in st.session_state:
79
- # vector_store = get_vectorstore_with_doc_from_pdf('GPT OUTPUT.pdf')
80
- vector_store = get_vectorstore_with_doc_from_word('GPT OUTPUT.docx')
81
- st.session_state.retriever = vector_store.as_retriever(k=NUMBER_OF_VECTORS_FOR_RAG)
82
-
83
- print("container")
84
- # Display chat messages from history
85
- container = st.container(height=700)
86
- for message in st.session_state.messages:
87
- if message["role"] != "system":
88
- with container.chat_message(message["role"]):
89
- st.write(message["content"])
90
-
91
- if prompt := st.chat_input("Enter your query here... "):
92
- with container.chat_message("user"):
93
- st.write(prompt)
94
- st.session_state.messages.append({"role":"user" , "content":prompt})
95
-
96
- with container.chat_message("assistant"):
97
- response = response_generator(prompt=prompt)
98
- print("******************************************************** Response ********************************************************")
99
- print("MY RESPONSE IS:", response)
100
- st.write(response)
101
-
102
- print("Response is:", response)
103
- st.session_state.rag_memory.save_context({'input': prompt}, {'output': response})
104
- st.session_state.messages.append({"role":"assistant" , "content":response})
 
 
 
1
+ import os
2
+ import base64
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ import streamlit as st
6
+ from app_config import SYSTEM_PROMPT,MODEL,MAX_TOKENS,TRANSFORMER_MODEL
7
+ from langchain.memory import ConversationSummaryBufferMemory
8
+ from langchain_community.embeddings import HuggingFaceEmbeddings
9
+ from langchain_google_genai import ChatGoogleGenerativeAI
10
+ from langchain_groq import ChatGroq
11
+ from streamlit_pdf_viewer import pdf_viewer
12
+ from pydantic import BaseModel
13
+ from langchain.chains import LLMChain
14
+ from langchain.prompts import ChatPromptTemplate
15
+ from langchain_community.vectorstores import FAISS
16
+ from sentence_transformers import SentenceTransformer
17
+ from typing import Any
18
+
19
+ st.title("Hitachi Support Bot")
20
+
21
+ class Element(BaseModel):
22
+ type: str
23
+ text: Any
24
+
25
+ # llm = ChatGoogleGenerativeAI(
26
+ # model=MODEL,
27
+ # max_tokens=MAX_TOKENS
28
+ # )
29
+ llm = ChatGroq(model=MODEL,api_key='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')
30
+
31
+
32
+ prompt = ChatPromptTemplate.from_template(SYSTEM_PROMPT)
33
+ qa_chain = LLMChain(llm=llm,prompt=prompt)
34
+ embeddings = HuggingFaceEmbeddings(model_name=TRANSFORMER_MODEL)
35
+ db = FAISS.load_local("faiss_index",embeddings,allow_dangerous_deserialization=True)
36
+
37
+ st.markdown(
38
+ """
39
+ <style>
40
+ .st-emotion-cache-janbn0 {
41
+ flex-direction: row-reverse;
42
+ text-align: right;
43
+ }
44
+ </style>
45
+ """,
46
+ unsafe_allow_html=True,
47
+ )
48
+
49
+ def response_generator(question):
50
+ relevant_docs = db.similarity_search_with_relevance_scores(question,k=5)
51
+ context = ""
52
+ relevant_images = []
53
+ for d,score in relevant_docs:
54
+ if score > 0:
55
+ if d.metadata['type'] == 'text':
56
+ context += str(d.metadata['original_content'])
57
+ elif d.metadata['type'] == 'table':
58
+ context += str(d.metadata['original_content'])
59
+ elif d.metadata['type'] == 'image':
60
+ context += d.page_content
61
+ relevant_images.append(d.metadata['original_content'])
62
+ result = qa_chain.run({'context':context,"question":question})
63
+ return result,relevant_images
64
+
65
+ with st.sidebar:
66
+ st.header("Hitachi Support Bot")
67
+ button = st.toggle("View Doc file.")
68
+
69
+ if button:
70
+ pdf_viewer("GPT OUTPUT.pdf")
71
+ else:
72
+ if "messages" not in st.session_state:
73
+ st.session_state.messages=[{"role": "system", "content": SYSTEM_PROMPT}]
74
+
75
+ if "llm" not in st.session_state:
76
+ st.session_state.llm = llm
77
+ if "rag_memory" not in st.session_state:
78
+ st.session_state.rag_memory = ConversationSummaryBufferMemory(llm=st.session_state.llm, max_token_limit= 5000)
79
+
80
+ container = st.container(height=700)
81
+ for message in st.session_state.messages:
82
+ if message["role"] != "system":
83
+ if message["role"] == "user":
84
+ with container.chat_message(message["role"]):
85
+ st.write(message["content"])
86
+ if message["role"] == "assistant":
87
+ with container.chat_message(message["role"]):
88
+ st.write(message["content"])
89
+ for i in range(len(message["images"])):
90
+ st.image(Image.open(BytesIO(base64.b64decode(message["images"][i].encode('utf-8')))))
91
+
92
+ if prompt := st.chat_input("Enter your query here... "):
93
+ with container.chat_message("user"):
94
+ st.write(prompt)
95
+ st.session_state.messages.append({"role":"user" , "content":prompt})
96
+ with container.chat_message("assistant"):
97
+ response,images = response_generator(prompt)
98
+ st.write(response)
99
+ for i in range(len(images)):
100
+ st.markdown("""---""")
101
+ st.image(Image.open(BytesIO(base64.b64decode(images[i].encode('utf-8')))))
102
+ st.markdown("""---""")
103
+
104
+
105
+ st.session_state.rag_memory.save_context({'input': prompt}, {'output': response})
106
+ st.session_state.messages.append({"role":"assistant" , "content":response,'images':images})
app_config.py CHANGED
@@ -1,19 +1,13 @@
1
-
2
- SYSTEM_PROMPT = """
3
- 1. You are Support bot for hitachi corporation. You must answer of any user questions using context only.
4
- 2. if you can't provide the answer of the quetions then only tell them "Thank you for your question! I'm here to help with information related to Hitachi corporation.the answer of this question is not given in this video. If you have any queries about those topics, feel free to ask. For other questions, I recommend reaching out to the appropriate source." nothing else.
5
- 3. User can also give you some greetings like thank you, welcome, please, sorry etc... so you have to handle it appropriately without giving any unnecessary information which is not wanted by user.
6
- 4. any information must be answered from provided context only, you must not to answer outside to the context.
7
-
8
- context: {context}
9
- """
10
-
11
-
12
- NLP_MODEL_NAME = "llama3-70b-8192"
13
- REASONING_MODEL_NAME = "mixtral-8x7b-32768"
14
- REASONING_MODEL_TEMPERATURE = 0
15
- NLP_MODEL_TEMPERATURE = 0
16
- NLP_MODEL_MAX_TOKENS = 5400
17
- VECTOR_MAX_TOKENS = 6000
18
- VECTORS_TOKEN_OVERLAP_SIZE = 20
19
- NUMBER_OF_VECTORS_FOR_RAG = 1
 
1
+ SYSTEM_PROMPT = """
2
+ You are Support bot for hitachi corporation. You must answer of any user questions using context only.
3
+ You have context. make answer using context only and answer must be concise and also if needed use bullet points and other markups.
4
+ don't metion figure number, table number,.
5
+ context: {context}
6
+ Give the answer of this Question: {question}
7
+
8
+ """
9
+
10
+ # MODEL = "gemini-1.5-flash"
11
+ MODEL = "llama-3.1-70b-versatile"
12
+ MAX_TOKENS = 4000
13
+ TRANSFORMER_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
 
 
 
 
 
 
faiss_index/index.faiss ADDED
Binary file (223 kB). View file
 
faiss_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9714a57f85868cb0d2a61778b84593da45764246cfeb039ec0d933f2984edf7f
3
+ size 4279867
requirements.txt CHANGED
@@ -1,11 +1,11 @@
1
- streamlit
2
- langchain
3
- langchain_groq
4
- python-dotenv
5
- langchain_community
6
- langchain_chroma
7
- tiktoken
8
- sentence_transformers
9
- pymupdf
10
- docx2txt
11
- streamlit_pdf_viewer
 
1
+ langchain==0.2.12
2
+ langchain_google_genai==1.0.8
3
+ sentence_transformers==3.0.1
4
+ streamlit==1.37.0
5
+ streamlit_pdf_viewer==0.0.14
6
+ langchain_community==0.2.11
7
+ torch==2.3.1
8
+ torchaudio==2.3.1
9
+ torchvision==0.18.1
10
+ faiss-cpu==1.8.0
11
+ langchain_groq