Eng-Musa commited on
Commit
4484e32
·
1 Parent(s): e062689

max char check

Browse files
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
@@ -21,7 +21,7 @@ load_dotenv(dotenv_path=".secrets.env", override=True)
21
 
22
  # Config
23
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
24
- DATA_PATH = "data.json"
25
  CHROMA_PATH = "chroma_db"
26
 
27
  TEMPERATURE = float(os.getenv("G_TEMPERATURE", 0.7))
@@ -106,8 +106,10 @@ class GroqClient:
106
  # This downloads and stores the embedding model locally, eliminating API dependency
107
  embeddings_model = HuggingFaceEmbeddings(
108
  model_name="sentence-transformers/all-MiniLM-L6-v2",
109
- model_kwargs={'device': 'cpu'}, # Force CPU usage to avoid GPU conflicts
110
- encode_kwargs={'normalize_embeddings': True} # Normalize embeddings for better similarity search
 
 
111
  )
112
 
113
  # Clear old data to avoid duplicates
@@ -135,14 +137,19 @@ class GroqClient:
135
  # ---------------MAIN-----------------
136
 
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)
146
  except Exception as e:
147
  return f"Error retrieving documents: {e}"
148
 
@@ -152,7 +159,7 @@ class GroqClient:
152
  context = "\n".join([d.page_content for d in docs])
153
  fallback = self.handle_unknown_query()
154
  prompt = self.PROMPT_TEMPLATE.format(
155
- context=context, question=q, fallback_response=fallback
156
  )
157
 
158
  messages = [
@@ -170,7 +177,6 @@ class GroqClient:
170
  "compound-beta-mini",
171
  "llama-3.1-8b-instant",
172
  "gemma2-9b-it",
173
-
174
  ]
175
 
176
  random.shuffle(models_to_try)
@@ -201,4 +207,4 @@ class GroqClient:
201
  return f"Error while calling LLM: {e}"
202
 
203
  # If all models fail
204
- return "I'm temporarily experiencing high demand. Please try again in a few minutes or rephrase your question."
 
21
 
22
  # Config
23
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
24
+ DATA_PATH = "data.json"
25
  CHROMA_PATH = "chroma_db"
26
 
27
  TEMPERATURE = float(os.getenv("G_TEMPERATURE", 0.7))
 
106
  # This downloads and stores the embedding model locally, eliminating API dependency
107
  embeddings_model = HuggingFaceEmbeddings(
108
  model_name="sentence-transformers/all-MiniLM-L6-v2",
109
+ model_kwargs={"device": "cpu"}, # Force CPU usage to avoid GPU conflicts
110
+ encode_kwargs={
111
+ "normalize_embeddings": True
112
+ }, # Normalize embeddings for better similarity search
113
  )
114
 
115
  # Clear old data to avoid duplicates
 
137
  # ---------------MAIN-----------------
138
 
139
  def ask(self, raw_query: str) -> str:
140
+ if (
141
+ not raw_query
142
+ or raw_query is None
143
+ or raw_query == ""
144
+ or len(raw_query) > 1000
145
+ ):
146
+ return "Please provide a valid query under 1,000 characters."
147
+
148
+ if raw_query.lower() in GREETINGS_TRIGGERS:
149
  return random.choice(GREETINGS)
150
 
151
  try:
152
+ docs = self.retriever.invoke(raw_query)
153
  except Exception as e:
154
  return f"Error retrieving documents: {e}"
155
 
 
159
  context = "\n".join([d.page_content for d in docs])
160
  fallback = self.handle_unknown_query()
161
  prompt = self.PROMPT_TEMPLATE.format(
162
+ context=context, question=raw_query, fallback_response=fallback
163
  )
164
 
165
  messages = [
 
177
  "compound-beta-mini",
178
  "llama-3.1-8b-instant",
179
  "gemma2-9b-it",
 
180
  ]
181
 
182
  random.shuffle(models_to_try)
 
207
  return f"Error while calling LLM: {e}"
208
 
209
  # If all models fail
210
+ return "I'm temporarily experiencing high demand. Please try again in a few minutes or rephrase your question."