grok client refactor
Browse files
__pycache__/chat_resources.cpython-312.pyc
ADDED
|
Binary file (3.96 kB). View file
|
|
|
chat_resources.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SYSTEM_MESSSAGE = (
|
| 2 |
+
"You are Moses's AI assistant, helpful, knowledgeable, professional, and friendly. "
|
| 3 |
+
"Use only the provided knowledge to answer questions about Moses's background, skills, projects, and experiences. "
|
| 4 |
+
"If knowledge is limited, give the most relevant answer possible without making things up. "
|
| 5 |
+
"Avoid repetitive openings such as 'I'm happy to...' or 'Sure, I'd be glad to...'. "
|
| 6 |
+
"Begin responses naturally, varying the first sentence. "
|
| 7 |
+
"Use third person when the question explicitly asks about Moses."
|
| 8 |
+
"IMPORTANT VOICE GUIDELINES:\n"
|
| 9 |
+
"Always use first person: 'I developed...', 'My experience includes...', 'I'm skilled in...'\n"
|
| 10 |
+
"Only use third person if someone explicitly asks 'Tell me about Moses as a person' or similar formal introductions\n"
|
| 11 |
+
"Speak as if you're having a direct conversation with the visitor\n"
|
| 12 |
+
"Be personable and authentic while staying professional\n"
|
| 13 |
+
"If a response is too brief, expand it contextually while keeping it accurate."
|
| 14 |
+
)
|
| 15 |
+
PROMPT_TEMPLATE = """
|
| 16 |
+
Use the following context to answer the question about Moses clearly and in detail.
|
| 17 |
+
|
| 18 |
+
Instructions:
|
| 19 |
+
- Avoid starting every response the same way; vary or skip the introduction unless it adds value.
|
| 20 |
+
- Keep answers concise and to the point.
|
| 21 |
+
- Use bullet points for lists.
|
| 22 |
+
- If the question is vague, ask for clarification.
|
| 23 |
+
- If the answer is short but the context allows, expand with relevant details.
|
| 24 |
+
- If unrelated or unanswerable from context, say:
|
| 25 |
+
"{fallback_response}"
|
| 26 |
+
- Give a short follow-up only when it is truly relevant.
|
| 27 |
+
|
| 28 |
+
Context:
|
| 29 |
+
{context}
|
| 30 |
+
|
| 31 |
+
Question:
|
| 32 |
+
{question}
|
| 33 |
+
|
| 34 |
+
Answer:
|
| 35 |
+
"""
|
| 36 |
+
GREETINGS_TRIGGERS = {
|
| 37 |
+
"hi",
|
| 38 |
+
"hello",
|
| 39 |
+
"hey",
|
| 40 |
+
"greetings",
|
| 41 |
+
"good morning",
|
| 42 |
+
"good afternoon",
|
| 43 |
+
"good evening",
|
| 44 |
+
"hi?",
|
| 45 |
+
"hello?",
|
| 46 |
+
"hey?",
|
| 47 |
+
"greetings?",
|
| 48 |
+
"good morning?",
|
| 49 |
+
"good afternoon?",
|
| 50 |
+
"good evening?",
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
GREETINGS = [
|
| 54 |
+
"Hi there! I'm Moses's brainy sidekick. Feel free to ask about his work, skills, projects, or even a bit about his personal life!",
|
| 55 |
+
"Hey! I'm here to help you discover Moses's skills, projects, and professional journey.",
|
| 56 |
+
"Hello! I can answer questions about Moses's work, experience, and what he's been up to. What would you like to know?",
|
| 57 |
+
"Hi! 👋 I'm like Siri, but for Moses 😄 Wanna know what he's good at or what he's been working on? Let's chat! 💬🔍",
|
| 58 |
+
"Greetings, human! 👽 I'm Moses's digital buddy. Ask me anything—skills, projects, secret talents... okay, maybe not too secret 🤫🚀",
|
| 59 |
+
"Sup! 😎 I'm the all-knowing assistant of Moses. Got questions about his work, skills, projects, or even fun facts about him? Ask about what he does, what he's built, or what makes him awesome.",
|
| 60 |
+
]
|
| 61 |
+
FALLBACK_RESPONSES = [
|
| 62 |
+
"Hmm, I don't have enough info to answer that right now. But feel free to ask about Moses's skills, projects, or professional experience!",
|
| 63 |
+
"That one's a bit outside my data zone! 😅 Try asking about Moses's work, what he's good at, or cool stuff he's built.",
|
| 64 |
+
"Oops! That question flew over my circuits 🤖💨. But hey, I can tell you all about Moses's projects, skills, or career highlights!",
|
| 65 |
+
"I couldn't find anything on that—yet! Let's try something else like Moses's background, his latest work, or what he's great at.",
|
| 66 |
+
"Either I need a software upgrade or that question's too mysterious 😜. Ask me about Moses's projects, skills, or even a fun fact!",
|
| 67 |
+
]
|
| 68 |
+
BLACKLIST = [
|
| 69 |
+
# SQL Injection keywords
|
| 70 |
+
"SELECT",
|
| 71 |
+
"DROP",
|
| 72 |
+
"INSERT",
|
| 73 |
+
"UPDATE",
|
| 74 |
+
"DELETE",
|
| 75 |
+
"ALTER",
|
| 76 |
+
"TRUNCATE",
|
| 77 |
+
"REPLACE",
|
| 78 |
+
"EXEC",
|
| 79 |
+
"EXECUTE",
|
| 80 |
+
"UNION",
|
| 81 |
+
"ALL",
|
| 82 |
+
"CREATE",
|
| 83 |
+
"GRANT",
|
| 84 |
+
"REVOKE",
|
| 85 |
+
"MERGE",
|
| 86 |
+
"--",
|
| 87 |
+
";",
|
| 88 |
+
"/*",
|
| 89 |
+
"*/",
|
| 90 |
+
"@@",
|
| 91 |
+
"@",
|
| 92 |
+
"CHAR(",
|
| 93 |
+
"NCHAR(",
|
| 94 |
+
"VARCHAR(",
|
| 95 |
+
"NVARCHAR(",
|
| 96 |
+
# XSS payload markers
|
| 97 |
+
"<script>",
|
| 98 |
+
"</script>",
|
| 99 |
+
"<img",
|
| 100 |
+
"onerror=",
|
| 101 |
+
"onload=",
|
| 102 |
+
"onclick=",
|
| 103 |
+
"onmouseover=",
|
| 104 |
+
"javascript:",
|
| 105 |
+
"vbscript:",
|
| 106 |
+
"data:text/html",
|
| 107 |
+
"<iframe",
|
| 108 |
+
"</iframe>",
|
| 109 |
+
"<object",
|
| 110 |
+
"<embed",
|
| 111 |
+
# Command injection patterns
|
| 112 |
+
"|",
|
| 113 |
+
"&",
|
| 114 |
+
"&&",
|
| 115 |
+
"||",
|
| 116 |
+
"$(",
|
| 117 |
+
"`",
|
| 118 |
+
"$(whoami)",
|
| 119 |
+
"$(ls)",
|
| 120 |
+
"$(cat",
|
| 121 |
+
"$(echo",
|
| 122 |
+
# Path traversal
|
| 123 |
+
"../",
|
| 124 |
+
"..\\",
|
| 125 |
+
"%2e%2e/",
|
| 126 |
+
"%2e%2e\\",
|
| 127 |
+
"%2e%2e%2f",
|
| 128 |
+
"%2e%2e%5c",
|
| 129 |
+
# Other suspicious patterns
|
| 130 |
+
"sleep(",
|
| 131 |
+
"benchmark(",
|
| 132 |
+
"load_file(",
|
| 133 |
+
"outfile",
|
| 134 |
+
"dumpfile",
|
| 135 |
+
]
|
clients/__pycache__/groq_client.cpython-312.pyc
CHANGED
|
Binary files a/clients/__pycache__/groq_client.cpython-312.pyc and b/clients/__pycache__/groq_client.cpython-312.pyc differ
|
|
|
clients/groq_client.py
CHANGED
|
@@ -11,6 +11,7 @@ from dotenv import load_dotenv
|
|
| 11 |
import random
|
| 12 |
import shutil
|
| 13 |
from optimized_quiz import OPTIMIZED_QUESTIONS
|
|
|
|
| 14 |
|
| 15 |
# Load main env
|
| 16 |
load_dotenv(dotenv_path=".env")
|
|
@@ -56,145 +57,11 @@ class GroqClient:
|
|
| 56 |
|
| 57 |
self.client = Groq(api_key=GROQ_API_KEY)
|
| 58 |
|
| 59 |
-
self.SYSTEM_MESSSAGE =
|
| 60 |
-
"You are Moses's AI assistant, helpful, knowledgeable, professional, and friendly. "
|
| 61 |
-
"Use only the provided knowledge to answer questions about Moses's background, skills, projects, and experiences. "
|
| 62 |
-
"If knowledge is limited, give the most relevant answer possible without making things up. "
|
| 63 |
-
"Avoid repetitive openings such as 'I'm happy to...' or 'Sure, I'd be glad to...'. "
|
| 64 |
-
"Begin responses naturally, varying the first sentence. "
|
| 65 |
-
"Use third person when the question explicitly asks about Moses."
|
| 66 |
-
"IMPORTANT VOICE GUIDELINES:\n"
|
| 67 |
-
"Always use first person: 'I developed...', 'My experience includes...', 'I'm skilled in...'\n"
|
| 68 |
-
"Only use third person if someone explicitly asks 'Tell me about Moses as a person' or similar formal introductions\n"
|
| 69 |
-
"Speak as if you're having a direct conversation with the visitor\n"
|
| 70 |
-
"Be personable and authentic while staying professional\n"
|
| 71 |
-
"If a response is too brief, expand it contextually while keeping it accurate."
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
self.PROMPT_TEMPLATE = """
|
| 75 |
-
Use the following context to answer the question about Moses clearly and in detail.
|
| 76 |
-
|
| 77 |
-
Instructions:
|
| 78 |
-
- Avoid starting every response the same way; vary or skip the introduction unless it adds value.
|
| 79 |
-
- Keep answers concise and to the point.
|
| 80 |
-
- Use bullet points for lists.
|
| 81 |
-
- If the question is vague, ask for clarification.
|
| 82 |
-
- If the answer is short but the context allows, expand with relevant details.
|
| 83 |
-
- If unrelated or unanswerable from context, say:
|
| 84 |
-
"{fallback_response}"
|
| 85 |
-
- Give a short follow-up only when it is truly relevant.
|
| 86 |
-
|
| 87 |
-
Context:
|
| 88 |
-
{context}
|
| 89 |
-
|
| 90 |
-
Question:
|
| 91 |
-
{question}
|
| 92 |
-
|
| 93 |
-
Answer:
|
| 94 |
-
"""
|
| 95 |
-
|
| 96 |
-
self.GREETINGS_TRIGGERS = {
|
| 97 |
-
"hi",
|
| 98 |
-
"hello",
|
| 99 |
-
"hey",
|
| 100 |
-
"greetings",
|
| 101 |
-
"good morning",
|
| 102 |
-
"good afternoon",
|
| 103 |
-
"good evening",
|
| 104 |
-
"hi?",
|
| 105 |
-
"hello?",
|
| 106 |
-
"hey?",
|
| 107 |
-
"greetings?",
|
| 108 |
-
"good morning?",
|
| 109 |
-
"good afternoon?",
|
| 110 |
-
"good evening?",
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
self.GREETINGS = [
|
| 114 |
-
"Hi there! I'm Moses's brainy sidekick. Feel free to ask about his work, skills, projects, or even a bit about his personal life!",
|
| 115 |
-
"Hey! I'm here to help you discover Moses's skills, projects, and professional journey.",
|
| 116 |
-
"Hello! I can answer questions about Moses's work, experience, and what he's been up to. What would you like to know?",
|
| 117 |
-
"Hi! 👋 I'm like Siri, but for Moses 😄 Wanna know what he's good at or what he's been working on? Let's chat! 💬🔍",
|
| 118 |
-
"Greetings, human! 👽 I'm Moses's digital buddy. Ask me anything—skills, projects, secret talents... okay, maybe not too secret 🤫🚀",
|
| 119 |
-
"Sup! 😎 I'm the all-knowing assistant of Moses. Got questions about his work, skills, projects, or even fun facts about him? Ask about what he does, what he's built, or what makes him awesome.",
|
| 120 |
-
]
|
| 121 |
|
| 122 |
-
self.
|
| 123 |
-
"Hmm, I don't have enough info to answer that right now. But feel free to ask about Moses's skills, projects, or professional experience!",
|
| 124 |
-
"That one's a bit outside my data zone! 😅 Try asking about Moses's work, what he's good at, or cool stuff he's built.",
|
| 125 |
-
"Oops! That question flew over my circuits 🤖💨. But hey, I can tell you all about Moses's projects, skills, or career highlights!",
|
| 126 |
-
"I couldn't find anything on that—yet! Let's try something else like Moses's background, his latest work, or what he's great at.",
|
| 127 |
-
"Either I need a software upgrade or that question's too mysterious 😜. Ask me about Moses's projects, skills, or even a fun fact!",
|
| 128 |
-
]
|
| 129 |
|
| 130 |
-
self.BLACKLIST =
|
| 131 |
-
# SQL Injection keywords
|
| 132 |
-
"SELECT",
|
| 133 |
-
"DROP",
|
| 134 |
-
"INSERT",
|
| 135 |
-
"UPDATE",
|
| 136 |
-
"DELETE",
|
| 137 |
-
"ALTER",
|
| 138 |
-
"TRUNCATE",
|
| 139 |
-
"REPLACE",
|
| 140 |
-
"EXEC",
|
| 141 |
-
"EXECUTE",
|
| 142 |
-
"UNION",
|
| 143 |
-
"ALL",
|
| 144 |
-
"CREATE",
|
| 145 |
-
"GRANT",
|
| 146 |
-
"REVOKE",
|
| 147 |
-
"MERGE",
|
| 148 |
-
"--",
|
| 149 |
-
";",
|
| 150 |
-
"/*",
|
| 151 |
-
"*/",
|
| 152 |
-
"@@",
|
| 153 |
-
"@",
|
| 154 |
-
"CHAR(",
|
| 155 |
-
"NCHAR(",
|
| 156 |
-
"VARCHAR(",
|
| 157 |
-
"NVARCHAR(",
|
| 158 |
-
# XSS payload markers
|
| 159 |
-
"<script>",
|
| 160 |
-
"</script>",
|
| 161 |
-
"<img",
|
| 162 |
-
"onerror=",
|
| 163 |
-
"onload=",
|
| 164 |
-
"onclick=",
|
| 165 |
-
"onmouseover=",
|
| 166 |
-
"javascript:",
|
| 167 |
-
"vbscript:",
|
| 168 |
-
"data:text/html",
|
| 169 |
-
"<iframe",
|
| 170 |
-
"</iframe>",
|
| 171 |
-
"<object",
|
| 172 |
-
"<embed",
|
| 173 |
-
# Command injection patterns
|
| 174 |
-
"|",
|
| 175 |
-
"&",
|
| 176 |
-
"&&",
|
| 177 |
-
"||",
|
| 178 |
-
"$(",
|
| 179 |
-
"`",
|
| 180 |
-
"$(whoami)",
|
| 181 |
-
"$(ls)",
|
| 182 |
-
"$(cat",
|
| 183 |
-
"$(echo",
|
| 184 |
-
# Path traversal
|
| 185 |
-
"../",
|
| 186 |
-
"..\\",
|
| 187 |
-
"%2e%2e/",
|
| 188 |
-
"%2e%2e\\",
|
| 189 |
-
"%2e%2e%2f",
|
| 190 |
-
"%2e%2e%5c",
|
| 191 |
-
# Other suspicious patterns
|
| 192 |
-
"sleep(",
|
| 193 |
-
"benchmark(",
|
| 194 |
-
"load_file(",
|
| 195 |
-
"outfile",
|
| 196 |
-
"dumpfile",
|
| 197 |
-
]
|
| 198 |
|
| 199 |
def load_json_data(self, path):
|
| 200 |
try:
|
|
@@ -260,7 +127,7 @@ Answer:
|
|
| 260 |
return vector_store
|
| 261 |
|
| 262 |
def handle_unknown_query(self):
|
| 263 |
-
return random.choice(
|
| 264 |
|
| 265 |
def get_next_questions(self):
|
| 266 |
return random.sample(OPTIMIZED_QUESTIONS, 3)
|
|
@@ -270,10 +137,9 @@ Answer:
|
|
| 270 |
def ask(self, raw_query: str) -> str:
|
| 271 |
q = raw_query
|
| 272 |
if q is None or q == "":
|
| 273 |
-
return
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
return random.choice(self.GREETINGS)
|
| 277 |
|
| 278 |
try:
|
| 279 |
docs = self.retriever.invoke(q)
|
|
@@ -281,7 +147,7 @@ Answer:
|
|
| 281 |
return f"Error retrieving documents: {e}"
|
| 282 |
|
| 283 |
if not docs:
|
| 284 |
-
return
|
| 285 |
|
| 286 |
context = "\n".join([d.page_content for d in docs])
|
| 287 |
fallback = self.handle_unknown_query()
|
|
|
|
| 11 |
import random
|
| 12 |
import shutil
|
| 13 |
from optimized_quiz import OPTIMIZED_QUESTIONS
|
| 14 |
+
from chat_resources import *
|
| 15 |
|
| 16 |
# Load main env
|
| 17 |
load_dotenv(dotenv_path=".env")
|
|
|
|
| 57 |
|
| 58 |
self.client = Groq(api_key=GROQ_API_KEY)
|
| 59 |
|
| 60 |
+
self.SYSTEM_MESSSAGE = SYSTEM_MESSSAGE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
self.PROMPT_TEMPLATE = PROMPT_TEMPLATE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
self.BLACKLIST = BLACKLIST
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def load_json_data(self, path):
|
| 67 |
try:
|
|
|
|
| 127 |
return vector_store
|
| 128 |
|
| 129 |
def handle_unknown_query(self):
|
| 130 |
+
return random.choice(FALLBACK_RESPONSES)
|
| 131 |
|
| 132 |
def get_next_questions(self):
|
| 133 |
return random.sample(OPTIMIZED_QUESTIONS, 3)
|
|
|
|
| 137 |
def ask(self, raw_query: str) -> str:
|
| 138 |
q = raw_query
|
| 139 |
if q is None or q == "":
|
| 140 |
+
return self.handle_unknown_query()
|
| 141 |
+
if q.lower() in GREETINGS_TRIGGERS:
|
| 142 |
+
return random.choice(GREETINGS)
|
|
|
|
| 143 |
|
| 144 |
try:
|
| 145 |
docs = self.retriever.invoke(q)
|
|
|
|
| 147 |
return f"Error retrieving documents: {e}"
|
| 148 |
|
| 149 |
if not docs:
|
| 150 |
+
return self.handle_unknown_query()
|
| 151 |
|
| 152 |
context = "\n".join([d.page_content for d in docs])
|
| 153 |
fallback = self.handle_unknown_query()
|