Update app/main.py
Browse files- app/main.py +14 -56
app/main.py
CHANGED
@@ -16,30 +16,6 @@ import nltk # Importing NLTK
|
|
16 |
import time
|
17 |
|
18 |
# Set writable paths for cache and data
|
19 |
-
cache_dir = '/tmp'
|
20 |
-
nltk_data_path = os.path.join(cache_dir, 'nltk_data')
|
21 |
-
|
22 |
-
# Configure NLTK and other library paths
|
23 |
-
os.environ['TRANSFORMERS_CACHE'] = os.path.join(cache_dir, 'transformers_cache')
|
24 |
-
os.environ['HF_HOME'] = os.path.join(cache_dir, 'huggingface')
|
25 |
-
os.environ['XDG_CACHE_HOME'] = cache_dir
|
26 |
-
|
27 |
-
# Add NLTK data path
|
28 |
-
nltk.data.path.append(nltk_data_path)
|
29 |
-
|
30 |
-
# Ensure the directory exists
|
31 |
-
try:
|
32 |
-
os.makedirs(nltk_data_path, exist_ok=True)
|
33 |
-
except OSError as e:
|
34 |
-
print(f"Error creating directory {nltk_data_path}: {e}")
|
35 |
-
raise
|
36 |
-
|
37 |
-
# Download required NLTK resources
|
38 |
-
try:
|
39 |
-
nltk.download('punkt', download_dir=nltk_data_path)
|
40 |
-
print("NLTK 'punkt' resource downloaded successfully.")
|
41 |
-
except Exception as e:
|
42 |
-
print(f"Error downloading NLTK resources: {e}")
|
43 |
raise
|
44 |
|
45 |
def clean_response(response):
|
@@ -58,34 +34,27 @@ def clean_response(response):
|
|
58 |
return cleaned
|
59 |
|
60 |
app = FastAPI()
|
61 |
-
|
62 |
-
app.add_middleware(
|
63 |
-
CORSMiddleware,
|
64 |
-
allow_origins=["*"],
|
65 |
-
allow_credentials=True,
|
66 |
-
allow_methods=["*"],
|
67 |
-
allow_headers=["*"],
|
68 |
-
)
|
69 |
-
|
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 |
-
max_tokens=200
|
76 |
)
|
77 |
|
|
|
|
|
78 |
@app.get("/")
|
79 |
def read_root():
|
80 |
return {"Hello": "World"}
|
81 |
|
82 |
class Query(BaseModel):
|
|
|
83 |
query_text: str
|
84 |
|
85 |
prompt = ChatPromptTemplate.from_template(
|
86 |
"""
|
87 |
-
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
|
88 |
-
but avoid sounding boastful or exaggerating. Stay focused on the context provided.
|
89 |
If the query is not related to TIET or falls outside the context of education, respond with:
|
90 |
"Sorry, I cannot help with that. I'm specifically designed to answer questions about the Thapar Institute of Engineering and Technology.
|
91 |
For more information, please contact at our toll-free number: 18002024100 or E-mail us at [email protected]
|
@@ -97,16 +66,6 @@ Question: {input}
|
|
97 |
)
|
98 |
|
99 |
def vector_embedding():
|
100 |
-
try:
|
101 |
-
file_path = "./data/Data.docx"
|
102 |
-
if not os.path.exists(file_path):
|
103 |
-
print(f"The file {file_path} does not exist.")
|
104 |
-
return {"response": "Error: Data file not found"}
|
105 |
-
|
106 |
-
loader = DocxLoader(file_path)
|
107 |
-
documents = loader.load()
|
108 |
-
|
109 |
-
print(f"Loaded document: {file_path}")
|
110 |
|
111 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
112 |
chunks = text_splitter.split_documents(documents)
|
@@ -123,19 +82,16 @@ def vector_embedding():
|
|
123 |
print("Vector store created and saved successfully.")
|
124 |
return {"response": "Vector Store DB Is Ready"}
|
125 |
|
126 |
-
except Exception as e:
|
127 |
-
print(f"An error occurred: {str(e)}")
|
128 |
-
return {"response": f"Error: {str(e)}"}
|
129 |
-
|
130 |
-
def get_embeddings():
|
131 |
-
model_name = "BAAI/bge-base-en"
|
132 |
-
encode_kwargs = {'normalize_embeddings': True}
|
133 |
model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
|
134 |
return model_norm
|
135 |
|
136 |
@app.post("/chat") # Changed from /anthropic to /chat
|
137 |
def read_item(query: Query):
|
138 |
try:
|
|
|
|
|
|
|
|
|
139 |
embeddings = get_embeddings()
|
140 |
vectors = FAISS.load_local("./vectors_db", embeddings, allow_dangerous_deserialization=True)
|
141 |
except Exception as e:
|
@@ -149,6 +105,7 @@ def read_item(query: Query):
|
|
149 |
retriever = vectors.as_retriever()
|
150 |
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
151 |
response = retrieval_chain.invoke({'input': prompt1})
|
|
|
152 |
print("Response time:", time.process_time() - start)
|
153 |
|
154 |
# Apply the cleaning function to the response
|
@@ -163,8 +120,9 @@ def read_item(query: Query):
|
|
163 |
|
164 |
@app.get("/setup")
|
165 |
def setup():
|
166 |
-
return vector_embedding()
|
167 |
|
168 |
if __name__ == "__main__":
|
169 |
import uvicorn
|
170 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
16 |
import time
|
17 |
|
18 |
# Set writable paths for cache and data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
raise
|
20 |
|
21 |
def clean_response(response):
|
|
|
34 |
return cleaned
|
35 |
|
36 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
38 |
llm = ChatOpenAI(
|
39 |
api_key=openai_api_key,
|
40 |
model_name="gpt-4-turbo-preview", # or "gpt-3.5-turbo" for a more economical option
|
41 |
+
temperature=0.7
|
|
|
42 |
)
|
43 |
|
44 |
+
|
45 |
+
|
46 |
@app.get("/")
|
47 |
def read_root():
|
48 |
return {"Hello": "World"}
|
49 |
|
50 |
class Query(BaseModel):
|
51 |
+
|
52 |
query_text: str
|
53 |
|
54 |
prompt = ChatPromptTemplate.from_template(
|
55 |
"""
|
56 |
+
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.
|
57 |
+
You may elaborate on your answers slightly to provide more information, but avoid sounding boastful or exaggerating. Stay focused on the context provided.
|
58 |
If the query is not related to TIET or falls outside the context of education, respond with:
|
59 |
"Sorry, I cannot help with that. I'm specifically designed to answer questions about the Thapar Institute of Engineering and Technology.
|
60 |
For more information, please contact at our toll-free number: 18002024100 or E-mail us at [email protected]
|
|
|
66 |
)
|
67 |
|
68 |
def vector_embedding():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
71 |
chunks = text_splitter.split_documents(documents)
|
|
|
82 |
print("Vector store created and saved successfully.")
|
83 |
return {"response": "Vector Store DB Is Ready"}
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
|
86 |
return model_norm
|
87 |
|
88 |
@app.post("/chat") # Changed from /anthropic to /chat
|
89 |
def read_item(query: Query):
|
90 |
try:
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
embeddings = get_embeddings()
|
96 |
vectors = FAISS.load_local("./vectors_db", embeddings, allow_dangerous_deserialization=True)
|
97 |
except Exception as e:
|
|
|
105 |
retriever = vectors.as_retriever()
|
106 |
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
107 |
response = retrieval_chain.invoke({'input': prompt1})
|
108 |
+
|
109 |
print("Response time:", time.process_time() - start)
|
110 |
|
111 |
# Apply the cleaning function to the response
|
|
|
120 |
|
121 |
@app.get("/setup")
|
122 |
def setup():
|
|
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
import uvicorn
|
126 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
127 |
+
|
128 |
+
|