satvikjain commited on
Commit
bb3169c
·
verified ·
1 Parent(s): 86e6bf8

Rename main.py to app.py

Browse files
Files changed (1) hide show
  1. main.py → app.py +110 -195
main.py → app.py RENAMED
@@ -1,195 +1,110 @@
1
- # # Imported required Dependencies
2
- # from langchain.text_splitter import CharacterTextSplitter
3
- # from langchain_community.document_loaders import DirectoryLoader
4
- # from langchain_huggingface import HuggingFaceEmbeddings
5
- # from langchain_huggingface import HuggingFaceEndpoint
6
- # import langchain_community.vectorstores
7
- # from pinecone import Pinecone, ServerlessSpec
8
- # from dotenv import load_dotenv
9
- # import os
10
- # from langchain_core.prompts import PromptTemplate
11
- # from langchain.schema.runnable import RunnablePassthrough
12
- # from langchain.schema.output_parser import StrOutputParser
13
-
14
- # class ChatBot():
15
- # load_dotenv()
16
- # loader = DirectoryLoader('data', glob="*.md")
17
- # documents = loader.load()
18
- # text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=4)
19
- # docs = text_splitter.split_documents(documents)
20
-
21
- # embeddings = HuggingFaceEmbeddings()
22
-
23
- # load_dotenv()
24
- # # Initialize Pinecone client
25
- # pc = Pinecone(
26
- # api_key=os.environ.get("PINECONE_API_KEY")
27
- # )
28
-
29
- # # Define Index Name
30
- # index_name = "noqs-chatbot"
31
-
32
- # # Checking Index
33
- # if index_name not in pc.list_indexes().names():
34
- # # Create new Index
35
- # pc.create_index(name=index_name,
36
- # metric="cosine",
37
- # dimension=768,
38
- # spec=ServerlessSpec(
39
- # cloud="aws",
40
- # region="us-east-1"
41
- # ))
42
-
43
- # docsearch = langchain_community.vectorstores.Pinecone.from_documents(docs, embeddings, index_name=index_name)
44
- # else:
45
- # # Link to the existing index
46
- # docsearch = langchain_community.vectorstores.Pinecone.from_existing_index(index_name, embeddings)
47
-
48
- # # Define the repo ID and connect to Mixtral model on Huggingface
49
- # repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
50
- # llm = HuggingFaceEndpoint(
51
- # repo_id=repo_id,
52
- # temperature = 0.8,
53
- # top_k = 50,
54
- # huggingfacehub_api_token=os.environ.get("HUGGINGFACE_API_KEY")
55
- # )
56
- # # Creating prompt response template
57
- # template = """
58
- # You are a employee. These Human will ask you a questions about our company NoQs Digital.
59
- # Use following piece of context to answer the question.
60
- # If you don't know the answer, just say you don't know.
61
- # Keep the answer within 3 sentences and concise.
62
-
63
- # Context: {context}
64
- # Question: {question}
65
- # Answer:
66
-
67
- # """
68
-
69
- # prompt = PromptTemplate(
70
- # template=template,
71
- # input_variables=["context", "question"]
72
- # )
73
-
74
- # # Chaining llm and prompt
75
- # rag_chain = (
76
- # {"context": docsearch.as_retriever(), "question": RunnablePassthrough()}
77
- # | prompt
78
- # | llm
79
- # | StrOutputParser()
80
- # )
81
-
82
-
83
- # bot = ChatBot()
84
- # input = input("Ask me anything: ")
85
- # result = bot.rag_chain.invoke(input)
86
- # print(result)
87
- from langchain.text_splitter import CharacterTextSplitter
88
- from langchain_community.document_loaders import TextLoader
89
- from langchain_huggingface import HuggingFaceEmbeddings
90
- from langchain_groq import ChatGroq # Assuming these classes exist
91
- import langchain_community.vectorstores
92
- from pinecone import Pinecone, ServerlessSpec
93
- from dotenv import load_dotenv
94
- import os
95
- from langchain_core.prompts import PromptTemplate
96
- from langchain.schema.runnable import RunnablePassthrough
97
- from langchain.schema.output_parser import StrOutputParser
98
- import gradio as gr
99
-
100
- class ChatBot():
101
- load_dotenv()
102
- loader = TextLoader('data.txt')
103
- documents = loader.load()
104
- text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=400)
105
- docs = text_splitter.split_documents(documents)
106
-
107
- embeddings = HuggingFaceEmbeddings()
108
-
109
- load_dotenv()
110
- # Initialize Pinecone client
111
- pc = Pinecone(
112
- api_key=os.environ.get("PINECONE_API_KEY")
113
- )
114
-
115
- # Define Index Name
116
- index_name = "noqs-chatbot"
117
-
118
- # Checking Index
119
- if index_name not in pc.list_indexes().names():
120
- # Create new Index
121
- pc.create_index(name=index_name,
122
- metric="cosine",
123
- dimension=768,
124
- spec=ServerlessSpec(
125
- cloud="aws",
126
- region="us-east-1"
127
- ))
128
-
129
- docsearch = langchain_community.vectorstores.Pinecone.from_documents(docs, embeddings, index_name=index_name)
130
- else:
131
- # Link to the existing index
132
- docsearch = langchain_community.vectorstores.Pinecone.from_existing_index(index_name, embeddings)
133
-
134
- # Define the repo ID and connect to a model on Groq API
135
- model_id = "llama3-8b-8192"
136
- llm = ChatGroq(
137
- model=model_id,
138
- temperature=0.1,
139
- max_tokens=500
140
- )
141
-
142
- # Creating prompt response template
143
- template = """
144
- You are a knowledgeable assistant for NoQs Digital. The Manager of our company is Mr. Adit Agarwal. Users will ask you questions about our company, and you must use the given context to answer their questions accurately. Follow these guidelines:
145
- Always base your answers on the provided context. Do not make up information.
146
- If the context does not contain the answer, simply say, "I don't know based on the provided information."
147
- Offer detailed and thorough responses, but stay relevant to the user's question.
148
- Maintain a professional tone in your responses.
149
- You have to answer like a Customer Care Specialist.
150
-
151
- Context: {context}
152
-
153
- User Question: {question}
154
-
155
- Answer:
156
- """
157
-
158
-
159
- prompt = PromptTemplate(
160
- template=template,
161
- input_variables=["context", "question"]
162
- )
163
-
164
- # Chaining llm and prompt
165
- rag_chain = (
166
- {"context": docsearch.as_retriever(), "question": RunnablePassthrough()}
167
- | prompt
168
- | llm
169
- | StrOutputParser()
170
- )
171
-
172
- bot = ChatBot()
173
-
174
- # def chat_function(prompt):
175
- # user_input = prompt
176
- # result = bot.rag_chain.invoke(user_input)
177
- # return result
178
-
179
- def chat_function(prompts,history):
180
- user_input = prompts
181
- result = bot.rag_chain.invoke(user_input)
182
- return result
183
-
184
-
185
- iface = gr.ChatInterface(
186
- fn=chat_function,
187
- textbox = gr.Textbox(placeholder="Enter message here", container=False, scale = 7),
188
- chatbot= gr.Chatbot(height=400),
189
- title="NoQs Chatbot",
190
- examples=[
191
- ["Hello, how are you?"],
192
- ["What services do you offer?"],
193
- ]
194
- )
195
- iface.launch()
 
1
+
2
+ from langchain.text_splitter import CharacterTextSplitter
3
+ from langchain_community.document_loaders import TextLoader
4
+ from langchain_huggingface import HuggingFaceEmbeddings
5
+ from langchain_groq import ChatGroq # Assuming these classes exist
6
+ import langchain_community.vectorstores
7
+ from pinecone import Pinecone, ServerlessSpec
8
+ from dotenv import load_dotenv
9
+ import os
10
+ from langchain_core.prompts import PromptTemplate
11
+ from langchain.schema.runnable import RunnablePassthrough
12
+ from langchain.schema.output_parser import StrOutputParser
13
+ import gradio as gr
14
+
15
+ class ChatBot():
16
+ load_dotenv()
17
+ loader = TextLoader('data.txt')
18
+ documents = loader.load()
19
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=400)
20
+ docs = text_splitter.split_documents(documents)
21
+
22
+ embeddings = HuggingFaceEmbeddings()
23
+
24
+ load_dotenv()
25
+ # Initialize Pinecone client
26
+ pc = Pinecone(
27
+ api_key=os.environ.get("PINECONE_API_KEY")
28
+ )
29
+
30
+ # Define Index Name
31
+ index_name = "noqs-chatbot"
32
+
33
+ # Checking Index
34
+ if index_name not in pc.list_indexes().names():
35
+ # Create new Index
36
+ pc.create_index(name=index_name,
37
+ metric="cosine",
38
+ dimension=768,
39
+ spec=ServerlessSpec(
40
+ cloud="aws",
41
+ region="us-east-1"
42
+ ))
43
+
44
+ docsearch = langchain_community.vectorstores.Pinecone.from_documents(docs, embeddings, index_name=index_name)
45
+ else:
46
+ # Link to the existing index
47
+ docsearch = langchain_community.vectorstores.Pinecone.from_existing_index(index_name, embeddings)
48
+
49
+ # Define the repo ID and connect to a model on Groq API
50
+ model_id = "llama3-8b-8192"
51
+ llm = ChatGroq(
52
+ model=model_id,
53
+ temperature=0.1,
54
+ max_tokens=500
55
+ )
56
+
57
+ # Creating prompt response template
58
+ template = """
59
+ You are a knowledgeable assistant for NoQs Digital. The Manager of our company is Mr. Adit Agarwal. Users will ask you questions about our company, and you must use the given context to answer their questions accurately. Follow these guidelines:
60
+ Always base your answers on the provided context. Do not make up information.
61
+ If the context does not contain the answer, simply say, "I don't know based on the provided information."
62
+ Offer detailed and thorough responses, but stay relevant to the user's question.
63
+ Maintain a professional tone in your responses.
64
+ You have to answer like a Customer Care Specialist.
65
+
66
+ Context: {context}
67
+
68
+ User Question: {question}
69
+
70
+ Answer:
71
+ """
72
+
73
+
74
+ prompt = PromptTemplate(
75
+ template=template,
76
+ input_variables=["context", "question"]
77
+ )
78
+
79
+ # Chaining llm and prompt
80
+ rag_chain = (
81
+ {"context": docsearch.as_retriever(), "question": RunnablePassthrough()}
82
+ | prompt
83
+ | llm
84
+ | StrOutputParser()
85
+ )
86
+
87
+ bot = ChatBot()
88
+
89
+ # def chat_function(prompt):
90
+ # user_input = prompt
91
+ # result = bot.rag_chain.invoke(user_input)
92
+ # return result
93
+
94
+ def chat_function(prompts,history):
95
+ user_input = prompts
96
+ result = bot.rag_chain.invoke(user_input)
97
+ return result
98
+
99
+
100
+ iface = gr.ChatInterface(
101
+ fn=chat_function,
102
+ textbox = gr.Textbox(placeholder="Enter message here", container=False, scale = 7),
103
+ chatbot= gr.Chatbot(height=400),
104
+ title="NoQs Chatbot",
105
+ examples=[
106
+ ["Hello, how are you?"],
107
+ ["What services do you offer?"],
108
+ ]
109
+ )
110
+ iface.launch()