sri96 commited on
Commit
b7fdf79
·
verified ·
1 Parent(s): 93ab360

updated app

Browse files
Files changed (1) hide show
  1. app.py +119 -30
app.py CHANGED
@@ -1,41 +1,130 @@
1
- import streamlit as st
2
- import re
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import requests
5
 
6
- # url = "http://127.0.0.1:4200/query_data"
7
-
8
- # payload = {}
9
- # files=[
10
-
11
- # ]
12
- # headers = {}
13
-
14
- # response = requests.request("POST", url, headers=headers, data=payload, files=files)
15
-
16
- # print(response.text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  st.title("i2e Enterprise Chatbot")
19
 
20
  prompt = st.text_input("Ask Question")
21
 
22
-
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  if prompt:
25
  print("processing request")
26
-
27
- url = "http://127.0.0.1:4200/query_data"
28
-
29
- payload = {'prompt': prompt}
30
- files = [
31
-
32
- ]
33
- headers = {}
34
-
35
- response = requests.request("POST", url, headers=headers, data=payload, files=files)
36
- full_response = response.text.replace(u"\u2000", "")
37
- full_response=response.text.replace("\\n\\n"," \\n")
38
- full_response = full_response.replace("\\n", " \\n")
39
-
40
- print(full_response)
41
- st.write(full_response)
 
 
 
1
 
2
+ from flask import Flask
3
+ from flask import request, jsonify
4
+ from langchain_core.prompts import ChatPromptTemplate
5
+ from langchain_core.output_parsers import StrOutputParser
6
+ from langchain.retrievers.document_compressors import DocumentCompressorPipeline
7
+ from langchain_community.document_transformers import EmbeddingsRedundantFilter
8
+ from langchain.retrievers.document_compressors import EmbeddingsFilter
9
+ #from langchain_text_splitters import CharacterTextSplitter
10
+ from langchain.retrievers import ContextualCompressionRetriever
11
+ from langchain_groq import ChatGroq
12
+ #from langchain.document_loaders import HuggingFaceDatasetLoader
13
+ # from langchain_community.document_loaders import UnstructuredExcelLoader
14
+ # from langchain.document_loaders import CSVLoader
15
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
16
+ from langchain.embeddings import HuggingFaceEmbeddings
17
+ from langchain.vectorstores import FAISS
18
+ # from transformers import AutoTokenizer, AutoModelForQuestionAnswering
19
+ # from transformers import AutoTokenizer, pipeline
20
+ # from langchain import HuggingFacePipeline
21
+ import re
22
+ import os
23
+ import streamlit as st
24
  import requests
25
 
26
+ def start():
27
+ # Define the path to the pre-trained model you want to use
28
+ modelPath = "sentence-transformers/all-MiniLM-l6-v2"
29
+
30
+ # Create a dictionary with model configuration options, specifying to use the CPU for computations
31
+ model_kwargs = {'device': 'cpu'}
32
+
33
+ # Create a dictionary with encoding options, specifically setting 'normalize_embeddings' to False
34
+ encode_kwargs = {'normalize_embeddings': False}
35
+
36
+ # Initialize an instance of HuggingFaceEmbeddings with the specified parameters
37
+ embeddings = HuggingFaceEmbeddings(
38
+ model_name=modelPath, # Provide the pre-trained model's path
39
+ model_kwargs=model_kwargs, # Pass the model configuration options
40
+ encode_kwargs=encode_kwargs # Pass the encoding options
41
+ )
42
+
43
+
44
+ # Initialize the HuggingFaceEmbeddings
45
+ model_path = "sentence-transformers/all-MiniLM-l6-v2"
46
+ model_kwargs = {'device': 'cpu'}
47
+ encode_kwargs = {'normalize_embeddings': False}
48
+ embeddings = HuggingFaceEmbeddings(
49
+ model_name=model_path,
50
+ model_kwargs=model_kwargs,
51
+ encode_kwargs=encode_kwargs
52
+ )
53
+
54
+ # Load the FAISS index
55
+ db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
56
+
57
+ retriever = db.as_retriever(search_kwargs={"k": 2})
58
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
59
+ redundant_filter = EmbeddingsRedundantFilter(embeddings=embeddings)
60
+ relevant_filter = EmbeddingsFilter(embeddings=embeddings)
61
+ pipeline_compressor = DocumentCompressorPipeline(transformers=[text_splitter, redundant_filter, relevant_filter])
62
+ compression_retriever = ContextualCompressionRetriever(base_compressor=pipeline_compressor, base_retriever=retriever)
63
+
64
+ chat = ChatGroq(temperature=0, groq_api_key="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",
65
+ model_name="llama3-70b-8192")
66
+
67
+ rag_template_str = ("""
68
+ Answer the following query based on the context given.
69
+ Stylization:
70
+ 1)Do not include or reference quoted content verbatim in the answer. Don't say "According to context provided"
71
+ 2)Include the source URLs
72
+ 3)Include the Category it belongs to
73
+ Formatting:
74
+ 1)Use bullet points
75
+ Restriction:
76
+ 1)Only use context to answer the question
77
+ 2)If you don't know the answer,reply with "No answer found, you can contact us on https://www.i2econsulting.com/contact-us/"
78
+ context: {context}
79
+ query:{query}
80
+ """)
81
+
82
+ rag_prompt = ChatPromptTemplate.from_template(rag_template_str)
83
+ rag_chain = rag_prompt | chat | StrOutputParser()
84
+
85
+ llm = ChatGroq(groq_api_key="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",
86
+ model_name="mixtral-8x7b-32768")
87
+
88
+ prompt = ChatPromptTemplate.from_template(
89
+ """
90
+ Answer the questions based on the provided context only.
91
+ Please provide the most accurate response based on the question
92
+ <context>
93
+ {context}
94
+ <context>
95
+ Questions:{input}
96
+
97
+ """
98
+ )
99
+ rag_prompt = ChatPromptTemplate.from_template(rag_template_str)
100
+ rag_chain = rag_prompt | chat | StrOutputParser()
101
 
102
  st.title("i2e Enterprise Chatbot")
103
 
104
  prompt = st.text_input("Ask Question")
105
 
106
+ def api_py_function(query):
107
+ context = compression_retriever.get_relevant_documents(query)
108
+ #print(context)
109
+ l = []
110
+ for documents in context[:5]:
111
+ if documents.state['query_similarity_score'] > 0.1:
112
+ content = documents.page_content + str(documents.metadata)
113
+ l.append(content)
114
+ final_context = ''.join(l)
115
+ if l != []:
116
+ response = rag_chain.invoke({"query": query, "context": final_context})
117
+ else:
118
+ response = "No answer found, Please rephrase your question or you can contact us on https://www.i2econsulting.com/contact-us/"
119
+ return response
120
 
121
  if prompt:
122
  print("processing request")
123
+ full_response=api_py_function(prompt)
124
+ # full_response = response.text.replace(u"\u2000", "")
125
+ # full_response=response.text.replace("\\n\\n"," \\n")
126
+ # full_response = full_response.replace("\\n", " \\n")
127
+
128
+ st.write(full_response)
129
+ if __name__ == "__main__":
130
+ start()