-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
145 lines (119 loc) · 5.13 KB
/
practice.py
File metadata and controls
145 lines (119 loc) · 5.13 KB
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
import os
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# --- Environment Setup ---
print(f"🔄 Loading environment from: {os.getcwd()}")
load_dotenv(override=True) # Force reload environment variables
# --- API Key Validation ---
GROQ_KEY = os.getenv("GROQ_API_KEY", "").strip()
# Validation checks
if not GROQ_KEY:
raise ValueError("""
❌ GROQ_API_KEY not found in .env file!
Create a .env file with:
GROQ_API_KEY=your_actual_key_here
""")
if not GROQ_KEY.startswith("gsk_") or len(GROQ_KEY) != 56:
raise ValueError(f"""
❌ Invalid API key format!
→ Received: '{GROQ_KEY[:12]}...' (Length: {len(GROQ_KEY)})
→ Key should start with 'gsk_' and be 56 characters long
→ Get a valid key: https://console.groq.com/keys
""")
# --- Model Configuration ---
LLM_CONFIG = {
"groq_api_key": GROQ_KEY,
"model_name": "Llama3-8b-8192",
"temperature": 0.7,
"max_tokens": 1024,
"max_retries": 3,
"request_timeout": 30
}
llm = ChatGroq(**LLM_CONFIG)
# --- Interview Chain ---
interview_chain = (
ChatPromptTemplate.from_template(
"""Act as a senior {role} hiring manager. Generate 10 technical interview questions.
Format as a numbered list with one question per line. Include both coding and system design questions.
Role: {role}"""
)
| llm
| StrOutputParser()
)
# --- Core Functionality ---
# --- CLI Interface ---
def main():
print("\n🤖 Welcome! I’m PrepNexus – your personal AI Interview Guide 🤝")
print("--------------------------------------------------------------")
while True:
try:
cmd = input("\n📩 How can I assist you today? (Type 'questions' or 'exit'): ").lower().strip()
if cmd in ('exit', 'quit'):
print("\n🎉 Thank you for chatting with PrepNexus! Best of luck with your interviews! 🚀")
print("💡 Remember: Confidence + Consistency = Success 💼")
break
elif cmd in ('questions', 'q'):
generate_interview_questions()
else:
print("❔ Hmm, I didn’t get that. Try typing 'questions' to begin or 'exit' to quit.")
except KeyboardInterrupt:
print("\n👋 Session ended. Thank you for trusting PrepNexus!")
break
# --- Core Functionality ---
def generate_interview_questions():
"""Generate and display interview questions with error handling"""
try:
role = input("\n🎯 Which role are you preparing for?\n💬 PrepNexus: I'm here to help! Just type the role: ").strip()
if not role:
print("⚠️ Oops! Please enter a valid role so I can guide you better.")
return
print(f"\n⏳ PrepNexus is preparing tailored questions for the role: {role}...")
technical_roles = {'developer', 'engineer', 'programmer', 'swe', 'data scientist'}
is_technical = any(term in role.lower() for term in technical_roles)
prompt_template = """Act as a senior {role} hiring manager. Generate 10 {question_type} interview questions.
Format as a numbered list with one question per line. Questions should be specific to {role} responsibilities.
Role: {role}"""
raw_questions = interview_chain.invoke({
"role": role,
"question_type": "technical" if is_technical else "role-specific competency-based"
})
questions = []
for q in raw_questions.split("\n"):
q = q.strip()
if not q:
continue
q = q.split(". ", 1)[-1].replace("**", "").replace("*", "")
if q.lower().startswith(('here are', 'certainly')):
continue
questions.append(q)
print(f"\n📋 PrepNexus has crafted the top 10 interview questions for {role}:")
for i, q in enumerate(questions[:10], 1):
print(f"{i}. {q}")
print("\n✅ All the best! Let me know if you'd like more help.")
except Exception as e:
print(f"\n❌ PrepNexus ran into an issue: {str(e)}")
print("🛠️ Troubleshooting Tips:")
print("- Check https://status.groq.com/ for API status")
print("- Verify your API key in the .env file")
print("- Make sure your internet connection is active")
# --- CLI Interface ---
def main():
print("\n PrepNexus")
print("----------------------------")
while True:
try:
cmd = input("\n🔧 Command (questions/exit): ").lower().strip()
if cmd in ('exit', 'quit'):
print("\n🎉 Good luck with your interviews! Thank You For Connecting with PrepNexus")
break
elif cmd in ('questions', 'q'):
generate_interview_questions()
else:
print("❔ Unknown command. Try 'questions' or 'exit'")
except KeyboardInterrupt:
print("\n👋 Session ended gracefully. You've got this!")
break
if __name__ == "__main__":
main()