0504ankitsharma commited on
Commit
679a1e8
·
verified ·
1 Parent(s): ac4e9ed

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +42 -44
app/main.py CHANGED
@@ -9,10 +9,10 @@ from langchain.chains import create_retrieval_chain
9
  from langchain_community.vectorstores import FAISS
10
  from langchain_community.document_loaders import UnstructuredWordDocumentLoader as DocxLoader
11
  from fastapi.middleware.cors import CORSMiddleware
12
- from fastapi import FastAPI, Request
13
  from pydantic import BaseModel
14
  from langchain_community.embeddings import HuggingFaceBgeEmbeddings
15
- import nltk
16
  import time
17
 
18
  # Set writable paths for cache and data
@@ -43,10 +43,18 @@ except Exception as e:
43
  raise
44
 
45
  def clean_response(response):
 
46
  cleaned = response.strip()
47
- cleaned = re.sub(r'^\"|\"$', '', cleaned)
 
 
 
 
48
  cleaned = re.sub(r'\n+', '\n', cleaned)
 
 
49
  cleaned = cleaned.replace('\\n', '')
 
50
  return cleaned
51
 
52
  app = FastAPI()
@@ -62,32 +70,29 @@ app.add_middleware(
62
  openai_api_key = os.environ.get('OPENAI_API_KEY')
63
  llm = ChatOpenAI(
64
  api_key=openai_api_key,
65
- model_name="gpt-4-turbo-preview",
66
  temperature=0.7
67
  )
68
 
69
- conversation_history = {} # Dictionary to maintain contextual memory
70
-
71
  @app.get("/")
72
  def read_root():
73
  return {"Hello": "World"}
74
 
75
  class Query(BaseModel):
76
- session_id: str # Unique identifier for user session
77
  query_text: str
78
 
79
- prompt_template = ChatPromptTemplate.from_template(
80
- """
81
- You are a helpful assistant designed specifically for the Thapar Institute of Engineering and Technology (TIET), a renowned technical college. Your task is to answer all queries related to TIET. Every response you provide should be relevant to the context of TIET. If a question falls outside of this context, please decline by stating, 'Sorry, I cannot help with that.'
82
-
83
- If the query is not related to TIET or falls outside the context of education, respond with:
84
- "Sorry, I cannot help with that. I'm specifically designed to answer questions about the Thapar Institute of Engineering and Technology. For more information, please contact at our toll-free number: 18002024100 or E-mail us at admissions@thapar.edu"
85
-
86
- <context>
87
- {context}
88
- </context>
89
- Question: {input}
90
- """
91
  )
92
 
93
  def vector_embedding():
@@ -104,16 +109,16 @@ def vector_embedding():
104
 
105
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
106
  chunks = text_splitter.split_documents(documents)
107
-
108
  print(f"Created {len(chunks)} chunks.")
109
 
110
  model_name = "BAAI/bge-base-en"
111
  encode_kwargs = {'normalize_embeddings': True}
112
  model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
113
-
114
  db = FAISS.from_documents(chunks, model_norm)
115
  db.save_local("./vectors_db")
116
-
117
  print("Vector store created and saved successfully.")
118
  return {"response": "Vector Store DB Is Ready"}
119
 
@@ -127,40 +132,33 @@ def get_embeddings():
127
  model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
128
  return model_norm
129
 
130
- @app.post("/chat")
131
- def chat_endpoint(query: Query):
132
  try:
133
- session_id = query.session_id
134
- if session_id not in conversation_history:
135
- conversation_history[session_id] = []
136
-
137
  embeddings = get_embeddings()
138
  vectors = FAISS.load_local("./vectors_db", embeddings, allow_dangerous_deserialization=True)
139
  except Exception as e:
140
  print(f"Error loading vector store: {str(e)}")
141
  return {"response": "Vector Store Not Found or Error Loading. Please run /setup first."}
142
-
143
  prompt1 = query.query_text
144
  if prompt1:
145
  start = time.process_time()
146
- document_chain = create_stuff_documents_chain(llm, prompt_template)
147
  retriever = vectors.as_retriever()
148
  retrieval_chain = create_retrieval_chain(retriever, document_chain)
149
-
150
- # Combine context from conversation history
151
- context = "\n".join(conversation_history[session_id])
152
- response = retrieval_chain.invoke({'input': prompt1, 'context': context})
153
-
154
- cleaned_response = clean_response(response['answer'])
155
-
156
- # Update conversation history
157
- conversation_history[session_id].append(f"User: {prompt1}")
158
- conversation_history[session_id].append(f"Assistant: {cleaned_response}")
159
-
160
  print("Response time:", time.process_time() - start)
161
- return {"response": cleaned_response}
 
 
 
 
 
 
 
162
  else:
163
- return {"response": "No Query Found"}
164
 
165
  @app.get("/setup")
166
  def setup():
@@ -168,4 +166,4 @@ def setup():
168
 
169
  if __name__ == "__main__":
170
  import uvicorn
171
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
9
  from langchain_community.vectorstores import FAISS
10
  from langchain_community.document_loaders import UnstructuredWordDocumentLoader as DocxLoader
11
  from fastapi.middleware.cors import CORSMiddleware
12
+ from fastapi import FastAPI
13
  from pydantic import BaseModel
14
  from langchain_community.embeddings import HuggingFaceBgeEmbeddings
15
+ import nltk # Importing NLTK
16
  import time
17
 
18
  # Set writable paths for cache and data
 
43
  raise
44
 
45
  def clean_response(response):
46
+ # Remove any leading/trailing whitespace, including newlines
47
  cleaned = response.strip()
48
+
49
+ # Remove any enclosing quotation marks
50
+ cleaned = re.sub(r'^["\']+|["\']+$', '', cleaned)
51
+
52
+ # Replace multiple newlines with a single newline
53
  cleaned = re.sub(r'\n+', '\n', cleaned)
54
+
55
+ # Remove any remaining '\n' characters
56
  cleaned = cleaned.replace('\\n', '')
57
+
58
  return cleaned
59
 
60
  app = FastAPI()
 
70
  openai_api_key = os.environ.get('OPENAI_API_KEY')
71
  llm = ChatOpenAI(
72
  api_key=openai_api_key,
73
+ model_name="gpt-4-turbo-preview", # or "gpt-3.5-turbo" for a more economical option
74
  temperature=0.7
75
  )
76
 
 
 
77
  @app.get("/")
78
  def read_root():
79
  return {"Hello": "World"}
80
 
81
  class Query(BaseModel):
 
82
  query_text: str
83
 
84
+ prompt = ChatPromptTemplate.from_template(
85
+ """
86
+ You are a helpful assistant designed specifically for the Thapar Institute of Engineering and Technology (TIET), a renowned technical college. Your task is to answer all queries related to TIET. Every response you provide should be relevant to the context of TIET. If a question falls outside of this context, please decline by stating, 'Sorry, I cannot help with that.' If you do not know the answer to a question, do not attempt to fabricate a response; instead, politely decline.
87
+ You may elaborate on your answers slightly to provide more information, but avoid sounding boastful or exaggerating. Stay focused on the context provided.
88
+ If the query is not related to TIET or falls outside the context of education, respond with:
89
+ "Sorry, I cannot help with that. I'm specifically designed to answer questions about the Thapar Institute of Engineering and Technology.
90
+ For more information, please contact at our toll-free number: 18002024100 or E-mail us at admissions@thapar.edu
91
+ <context>
92
+ {context}
93
+ </context>
94
+ Question: {input}
95
+ """
96
  )
97
 
98
  def vector_embedding():
 
109
 
110
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
111
  chunks = text_splitter.split_documents(documents)
112
+
113
  print(f"Created {len(chunks)} chunks.")
114
 
115
  model_name = "BAAI/bge-base-en"
116
  encode_kwargs = {'normalize_embeddings': True}
117
  model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
118
+
119
  db = FAISS.from_documents(chunks, model_norm)
120
  db.save_local("./vectors_db")
121
+
122
  print("Vector store created and saved successfully.")
123
  return {"response": "Vector Store DB Is Ready"}
124
 
 
132
  model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
133
  return model_norm
134
 
135
+ @app.post("/chat") # Changed from /anthropic to /chat
136
+ def read_item(query: Query):
137
  try:
 
 
 
 
138
  embeddings = get_embeddings()
139
  vectors = FAISS.load_local("./vectors_db", embeddings, allow_dangerous_deserialization=True)
140
  except Exception as e:
141
  print(f"Error loading vector store: {str(e)}")
142
  return {"response": "Vector Store Not Found or Error Loading. Please run /setup first."}
143
+
144
  prompt1 = query.query_text
145
  if prompt1:
146
  start = time.process_time()
147
+ document_chain = create_stuff_documents_chain(llm, prompt)
148
  retriever = vectors.as_retriever()
149
  retrieval_chain = create_retrieval_chain(retriever, document_chain)
150
+ response = retrieval_chain.invoke({'input': prompt1})
 
 
 
 
 
 
 
 
 
 
151
  print("Response time:", time.process_time() - start)
152
+
153
+ # Apply the cleaning function to the response
154
+ cleaned_response = clean_response(response['answer'])
155
+
156
+ # For debugging, print the cleaned response
157
+ print("Cleaned response:", repr(cleaned_response))
158
+
159
+ return cleaned_response
160
  else:
161
+ return "No Query Found"
162
 
163
  @app.get("/setup")
164
  def setup():
 
166
 
167
  if __name__ == "__main__":
168
  import uvicorn
169
+ uvicorn.run(app, host="0.0.0.0", port=8000)