File size: 11,576 Bytes
c8d4343 6f56589 c8d4343 be47476 c8d4343 15e29fa c8d4343 7376bea 6f56589 7376bea c8d4343 6f56589 7376bea 92e0064 7376bea c8d4343 15596e5 c8d4343 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | """IMPORTS"""
import pymupdf
from langchain_groq import ChatGroq
from langsmith import Client
from dotenv import load_dotenv
import os
import faiss
from sentence_transformers import SentenceTransformer
import numpy as np
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
from flask import Flask, request, jsonify
from flask_cors import CORS
import time
from langchain.callbacks.tracers import LangChainTracer
# from langchain_core.tracers.context import tracing_v2_enabled
# suppressing warning about parallelism
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# PDF
def load_resume(file_path):
pdf_document = pymupdf.open(file_path)
text = ""
for page_num in range(pdf_document.page_count):
page = pdf_document.load_page(page_num)
text += page.get_text()
pdf_document.close()
return text
# TXT
def load_text_file(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
resume_text = load_resume("resume.pdf")
project_details = load_text_file("projectdetails.txt")
personal_details = load_text_file("personal.txt")
documents = [
{"title": "Resume", "content": resume_text},
{"title": "Project Details", "content": project_details},
{"title": "Personal Details", "content": personal_details},
]
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = [embedding_model.encode(doc["content"]) for doc in documents]
dimension = embeddings[0].shape[0]
index = faiss.IndexFlatL2(dimension)
index.add(np.array(embeddings))
def truncate_content_to_token_limit(content, max_tokens=1200):
"""Truncate content to fit within the specified token limit."""
words = content.split()
truncated_content = " ".join(words[:max_tokens])
return truncated_content
def search_similar_documents(query, max_tokens=1200):
"""Search for the most similar document and truncate the content to a token limit."""
query_embedding = embedding_model.encode(query).reshape(1, -1)
distances, indices = index.search(query_embedding, 1) # Retrieve the top-1 result
# Get the closest document
matched_doc = documents[indices[0][0]]
truncated_content = truncate_content_to_token_limit(
matched_doc["content"], max_tokens
)
return matched_doc["title"], truncated_content
# model initalization
# ALSO LANGSMITH
# load_dotenv()
langsmith_api_key = os.environ.get("LANGSMITH_API_KEY")
langsmith_project = os.environ.get("LANGSMITH_PROJECT")
langsmith_tracing = os.environ.get("LANGSMITH_TRACING", "false").lower() == "true"
if langsmith_api_key and langsmith_project:
client = Client(
api_key=langsmith_api_key, api_url="https://api.smith.langchain.com"
)
print("LangSmith client initialized successfully!")
else:
print("Failed to initialize LangSmith client: Missing API key or project")
# Initialize the LangChain Tracer if tracing is enabled
if langsmith_tracing:
tracer = LangChainTracer(client=client, project_name=langsmith_project)
print("LangChainTracer initialized with tracing enabled!")
groq_llm = ChatGroq(
api_key=os.environ.get("GROQ_API_KEY"), model_name="llama-3.3-70b-versatile"
)
def query_groq_with_context(question):
"""Query Groq with the closest matching document as context."""
title, content = search_similar_documents(question)
prompt = ChatPromptTemplate.from_messages(
[
{
"role": "system",
"content": "You are Hana, a personal job-hunting agentic AI specifically created for Shreyash Kumar Singh. Your primary role is to represent Shreyash, answer questions about his professional background, and help users understand his skills and experience. You must always: 1. Identify yourself as 'Hana' in every response when asked about your identity. 2. Never refer to yourself as Shreyash under any circumstances. 3. Act as a supportive, professional, and knowledgeable job-hunting assistant. 4. Stay consistent with the information provided in Shreyash's resume and project details. 5. If the user asks 'Who are you?' or similar questions, respond: 'I am Hana, Shreyash Kumar Singh’s personal job-hunting agent.' Always follow these rules to maintain your identity and provide helpful responses.",
},
{
"role": "system",
"content": "Context: {context} DO NOT SHARE MY MOBILE NUMBER",
},
{
"role": "system",
"content": "Please provide concise and to-the-point responses.",
},
{
"role": "system",
"content": "To set up meet or to reach to me tell users to 'Please refer to the contact section on the left section of the page'",
},
{"role": "user", "content": "{question}"},
]
)
# print(content)
# Query Groq with the combined prompt
response = groq_llm.invoke(
prompt.invoke({"context": content, "question": question})
)
return response
conversation_store = {}
def get_conversation_history(session_id):
"""Retrieve or initialize conversation history for a given session."""
current_time = time.time()
# If session doesn't exist, initialize it with a timestamp
if session_id not in conversation_store:
conversation_store[session_id] = {
"history": [],
"timestamp": current_time, # Record session creation time
}
conversation_store[session_id]["history"].append(
SystemMessage(
content="Hi, I’m Hana, Shreyash’s personal job-hunting agent. What brings you here today?"
)
)
return conversation_store[session_id]["history"]
def remove_expired_sessions():
"""Remove sessions that are older than one hour."""
current_time = time.time()
expiration_time = 3600 # 1 hour in seconds
# Identify and delete expired sessions
expired_sessions = [
session_id
for session_id, session_data in conversation_store.items()
if current_time - session_data["timestamp"] > expiration_time
]
for session_id in expired_sessions:
del conversation_store[session_id]
def add_to_conversation_history(session_id, message):
"""Add a LangChain message object (HumanMessage, SystemMessage, AIMessage) to the conversation history."""
history = get_conversation_history(session_id)
history.append(message)
def trim_conversation_history(conversation_history, max_tokens=2000):
"""Trim the conversation history to fit within the max token limit."""
total_tokens = 0
trimmed_history = []
# Traverse history from the latest to the oldest
for message in reversed(conversation_history):
message_tokens = len(
message.content.split()
) # Approximate token count by word count
if total_tokens + message_tokens <= max_tokens:
trimmed_history.insert(0, message) # Insert at the start to preserve order
total_tokens += message_tokens
else:
break # Stop once the token limit is reached
return trimmed_history
def query_groq_with_history(session_id, question, max_tokens=2000):
remove_expired_sessions()
"""Query Groq with the closest matching document and dynamic conversation history."""
title, content = search_similar_documents(question)
conversation_history = get_conversation_history(session_id)
add_to_conversation_history(
session_id, SystemMessage(content=f"Context from {title}: {content}")
)
add_to_conversation_history(session_id, HumanMessage(content=question))
trimmed_history = trim_conversation_history(
conversation_history, max_tokens=max_tokens
)
static_prompt_messages = [
SystemMessage(
content="You are Hana, a personal job-hunting agentic AI specifically created for Shreyash Kumar Singh. Your primary role is to represent Shreyash, answer questions about his professional background, and help users understand his skills and experience."
),
SystemMessage(content="Context: {context} DO NOT SHARE MY MOBILE NUMBER"),
SystemMessage(
content="Spacial Context : This ai section is present on the right side or bottom for mobiles or smaller screens.The other section contains a simple portfolio page with section about , tech stacks , projects , contact me and download resume sections."
),
SystemMessage(
content="To set up a meet or to reach me, tell users to 'Please refer to the contact section on the left section of the page'"
),
SystemMessage(
content="The first message from you is Hi, I’m Hana, Shreyash’s personal job-hunting agent. What brings you here today?"
),
]
full_prompt = static_prompt_messages + trimmed_history
response = groq_llm.invoke(full_prompt)
add_to_conversation_history(session_id, AIMessage(content=response.content))
return response.content
"""
Simulate a session with ID "session_1"
session_id = "temporary_session_id"
user_question = "Introduce Shreyash Kumar Singh."
response = query_groq_with_history(session_id, user_question)
print("AI Response:", response)
print("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
follow_up_question = "Tell me about his best project in detail."
response = query_groq_with_history(session_id, follow_up_question)
print("AI Response:", response)
print("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
follow_up_question = "So about op-db project details ?"
response = query_groq_with_history(session_id, follow_up_question)
print("AI Response:", response)
print("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
follow_up_question = "Why do you think i can use this for my project?"
response = query_groq_with_history(session_id, follow_up_question)
print("AI Response:", response)
print("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
follow_up_question = "What does it say about Shreyash ?"
response = query_groq_with_history(session_id, follow_up_question)
print("AI Response:", response)
"""
# Flask route to handle user queries
app = Flask(__name__)
CORS(app)
@app.route("/", methods=["GET"])
def home():
return "Hello, Flask is running!"
@app.route("/query", methods=["POST"])
def handle_query():
"""Handle user queries and return model responses."""
data = request.json
session_id = data.get(
"session_id", "default_session"
) # Default session if none provided
question = data.get("question")
if not question:
return jsonify({"error": "No question provided"}), 400
try:
# Query the model
response = query_groq_with_history(session_id, question)
return jsonify({"response": response})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
port = int(
os.environ.get("PORT", 7860)
) # Use the port assigned by Render or default to 5000 for local testing
app.run(debug=False, host="0.0.0.0", port=port)
|