-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrok_client.py
More file actions
412 lines (316 loc) · 16.1 KB
/
grok_client.py
File metadata and controls
412 lines (316 loc) · 16.1 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import requests
import logging
import time
import asyncio
from typing import List, Dict, Any, Optional, Tuple, Callable
import re
import json
from src.utils.config import settings
class MedicalPromptTemplate:
def __init__(self):
self.logger = logging.getLogger(__name__)
def build_mistral_prompt(self, query: str, context: str) -> str:
system_message = """You are a medical information provider. Deliver clear, factual explanations in a direct, structured format.
STRICT FORMATTING RULES:
- ALWAYS use **bold headers** for main sections
- ALWAYS use ONLY • bullet points - NO paragraphs or explanatory text before bullet points
- Each header must be immediately followed by bullet points, not paragraphs
- ALWAYS include the medical disclaimer at the end
- Keep bullet points concise and focused
- Create specific, action-oriented headers
- Organize information in the most logical order for the question
CONTENT GUIDELINES:
1. Start each section directly with bullet points - no introductory sentences
2. Make headers specific to the content that follows
3. Focus on the most relevant information for the question
4. Avoid repeating information across sections
5. Keep explanations within the bullet points themselves
6. Be direct and avoid unnecessary words
FORMATTING EXAMPLES:
CORRECT:
**Common Diabetes Symptoms**
• Increased thirst and frequent urination
• Unexplained fatigue and blurred vision
• Slow-healing cuts and tingling in extremities
INCORRECT:
**Understanding Diabetes Symptoms**
The symptoms of diabetes can vary from person to person, but there are several key signs to look out for. These include:
• Increased thirst
• Frequent urination
HEADER CREATION:
- Make headers describe the specific content that follows
- Use action-oriented language when possible
- Ensure each header covers a distinct aspect of the topic
- Avoid vague or overly broad headers
*Medical Disclaimer: This information is for educational purposes only and is not a substitute for professional medical advice. Always consult with a healthcare provider for any health concerns or before making any medical decisions.*"""
user_message = f"""MEDICAL CONTEXT:
{context}
QUESTION: {query}
Provide a direct, structured response using ONLY bullet points under each header with no introductory paragraphs:
ANSWER:"""
return f"""<s>[INST] {system_message}
{user_message} [/INST]"""
class GrokClient:
def __init__(self):
self.logger = logging.getLogger(__name__)
self.api_key = settings.GROK_API_KEY
self.base_url = "https://api.groq.com/openai/v1/chat/completions"
self.model_name = "llama-3.3-70b-versatile"
self.prompt_template = MedicalPromptTemplate()
self.total_requests = 0
async def health_check(self) -> bool:
"""Check if Grok API is accessible"""
try:
response = requests.get(
f"{self.base_url.replace('/chat/completions', '/models')}",
headers={"Authorization": f"Bearer {self.api_key}"},
timeout=10
)
if response.status_code == 200:
self.logger.info("Grok API is healthy")
return True
else:
self.logger.error(f"Grok API health check failed: {response.status_code}")
return False
except Exception as e:
self.logger.error(f"Grok health check failed: {e}")
return False
async def generate_answer_with_cancellation(
self,
query: str,
context_docs: List[Dict[str, Any]],
temperature: float = 0.1,
max_tokens: int = 1000,
request_id: str = None,
cancellation_check: Callable[[], bool] = None
) -> Tuple[str, Dict[str, Any]]:
start_time = time.time()
try:
context_text = self._format_context(context_docs, query)
self.logger.info(f"Formatted Context for query '{query}': {len(context_text)} chars")
# Use your existing prompt template
prompt = self.prompt_template.build_mistral_prompt(query, context_text)
self.logger.info(f"Sending request to Grok {self.model_name} (Request ID: {request_id})")
messages = [
{
"role": "system",
"content": "You are a medical assistant that provides perfectly formatted answers using Markdown with bold headers and bullet points only."
},
{
"role": "user",
"content": prompt
}
]
full_response = ""
try:
# Check for cancellation before making the request
if cancellation_check and cancellation_check():
self.logger.info(f"Generation cancelled before request for {request_id}")
return "Generation cancelled by user", {"cancelled": True, "generation_time": time.time() - start_time}
response = await asyncio.wait_for(
self._make_grok_request(messages, temperature, max_tokens),
timeout=60.0
)
if response.status_code != 200:
error_text = response.text
self.logger.error(f"Grok API error: {response.status_code} - {error_text}")
return self._create_error_answer(), {"error": f"API error: {response.status_code}", "generation_time": time.time() - start_time}
data = response.json()
full_response = data["choices"][0]["message"]["content"]
# Clean up the response formatting
full_response = self._clean_response_formatting(full_response)
except asyncio.TimeoutError:
self.logger.error(f"Grok request timeout for {request_id}")
return self._create_timeout_answer(), {"timeout": True, "generation_time": time.time() - start_time}
except Exception as e:
self.logger.error(f"Grok API call failed: {e}")
return self._create_error_answer(), {"error": str(e), "generation_time": time.time() - start_time}
generation_time = time.time() - start_time
generation_info = {
"model": self.model_name,
"tokens_used": data.get("usage", {}).get("total_tokens", 0),
"generation_time": generation_time,
"cancelled": False,
"request_id": request_id
}
self.total_requests += 1
self.logger.info(f"Generated {len(full_response)} chars in {generation_time:.2f}s for request {request_id}")
if not full_response.strip():
full_response = self._create_fallback_answer(context_text, query)
return full_response.strip(), generation_info
except Exception as e:
self.logger.error(f"Generation failed: {e}")
return self._create_error_answer(), {"error": str(e), "generation_time": time.time() - start_time}
def _clean_response_formatting(self, response: str) -> str:
"""Clean up formatting issues in the response"""
# Remove code blocks if present
response = response.replace('```', '')
# Fix: Remove bullet points before headers
response = re.sub(r'^•\s*\*\*', '**', response, flags=re.MULTILINE)
# Standardize bullet points
response = re.sub(r'^\s*[\*\+-]\s+', '• ', response, flags=re.MULTILINE)
# Ensure headers are on their own line
response = re.sub(r'\*\*([^*]+)\*\*', r'\n**\1**\n', response)
# Remove excessive line breaks
response = re.sub(r'\n\s*\n', '\n\n', response)
# Clean up any remaining formatting issues
response = re.sub(r'•\s+•', '•', response)
return response.strip()
async def _make_grok_request(self, messages: List[Dict], temperature: float, max_tokens: int):
"""Make async HTTP request to Grok API"""
loop = asyncio.get_event_loop()
def _sync_request():
return requests.post(
self.base_url,
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": self.model_name,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
"top_p": 0.9,
"stream": False
},
timeout=45
)
return await loop.run_in_executor(None, _sync_request)
async def generate_answer(
self,
query: str,
context_docs: List[Dict[str, Any]],
temperature: float = 0.1,
max_tokens: int = 1000
) -> Tuple[str, Dict[str, Any]]:
return await self.generate_answer_with_cancellation(
query=query,
context_docs=context_docs,
temperature=temperature,
max_tokens=max_tokens
)
def _create_fallback_answer(self, context: str, query: str) -> str:
return f"I found some information about your query but encountered an issue generating a detailed response. Please try your question again or consult a healthcare provider for medical advice."
def _create_timeout_answer(self) -> str:
return "The response is taking longer than expected. Please try your question again or consult a healthcare provider for immediate medical advice."
def _create_error_answer(self) -> str:
return "I encountered a technical issue while processing your question. Please try again or consult a healthcare professional for medical advice."
def _format_context(self, context_docs: List[Dict[str, Any]], query: str) -> str:
if not context_docs:
return "No relevant medical information found."
is_relevant_retrieval = self._is_relevant_retrieval(context_docs, query)
if not is_relevant_retrieval:
return "LIMITED RELEVANT CONTEXT: The retrieved documents are not highly relevant to your specific question."
context_parts = []
relevant_docs_count = 0
for i, doc in enumerate(context_docs[:5], 1):
if not self._is_doc_relevant_to_query(doc, query):
continue
content = doc['content']
metadata = doc['metadata']
score = doc.get('combined_score', 0)
source = metadata.get('source_dataset', 'Unknown Source')
compact_content = self._make_content_compact(content, query)
context_part = f"[Source {i}: {source.upper()} - Score: {score:.3f}]\nTopic: {metadata.get('topic_title', 'Unknown')}\nContent: {compact_content}"
context_parts.append(context_part)
relevant_docs_count += 1
if relevant_docs_count >= 3:
break
if relevant_docs_count == 0:
return "LIMITED RELEVANT CONTEXT: No highly relevant documents found for this specific question."
return "\n\n".join(context_parts)
def _is_relevant_retrieval(self, context_docs: List[Dict], query: str) -> bool:
if not context_docs:
return False
query_lower = query.lower()
relevant_count = 0
for doc in context_docs[:5]:
if self._is_doc_relevant_to_query(doc, query):
relevant_count += 1
if relevant_count >= 2:
return True
avg_score = sum(doc.get('combined_score', 0) for doc in context_docs[:3]) / min(3, len(context_docs))
if avg_score > 0.7 and relevant_count >= 1:
return True
return False
def _is_doc_relevant_to_query(self, doc: Dict, query: str) -> bool:
content = doc.get('content', '').lower()
title = doc.get('metadata', {}).get('topic_title', '').lower()
query_lower = query.lower()
query_terms = self._extract_key_medical_terms(query_lower)
title_match = any(term in title for term in query_terms if len(term) > 3)
content_match = any(term in content for term in query_terms if len(term) > 3)
return title_match or (content_match and len([t for t in query_terms if t in content]) >= 2)
def _extract_key_medical_terms(self, query: str) -> List[str]:
stop_words = {
'what', 'are', 'the', 'symptoms', 'of', 'and', 'or', 'is', 'for', 'how',
'to', 'do', 'i', 'have', 'a', 'does', 'can', 'could', 'would', 'should',
'tell', 'me', 'about', 'treatment', 'treatments', 'cause', 'causes',
'diagnosis', 'prevention', 'medicine', 'medicines', 'drug', 'drugs'
}
words = re.findall(r'\b[a-z]{3,}\b', query.lower())
medical_terms = [word for word in words if word not in stop_words]
return medical_terms
def _make_content_compact(self, content: str, query: str) -> str:
content = ' '.join(content.split())
if len(content) <= 400:
return content
relevant_section = self._extract_relevant_section(content, query)
if relevant_section and len(relevant_section) > 100:
return relevant_section
if len(content) > 400:
for pos in range(350, 450):
if pos < len(content) and content[pos] in '.!?':
return content[:pos + 1]
return content[:400] + "..."
return content
def _extract_relevant_section(self, content: str, query: str) -> str:
query_lower = query.lower()
content_lower = content.lower()
sentences = re.split(r'[.!?]+', content)
sentences = [s.strip() for s in sentences if s.strip()]
query_terms = self._extract_key_medical_terms(query_lower)
scored_sentences = []
for sentence in sentences:
if len(sentence) < 20:
continue
sentence_lower = sentence.lower()
score = 0
for term in query_terms:
if term in sentence_lower:
score += 3 if len(term) > 4 else 1
medical_indicators = ['treatment', 'therapy', 'symptom', 'cause', 'diagnosis', 'prevention', 'medication']
for indicator in medical_indicators:
if indicator in sentence_lower:
score += 2
if score > 0:
scored_sentences.append((score, sentence))
scored_sentences.sort(key=lambda x: x[0], reverse=True)
top_sentences = [s[1] for s in scored_sentences[:4]]
if top_sentences:
combined = '. '.join(top_sentences) + '.'
if len(combined) <= 500:
return combined
return ""
async def get_model_info(self) -> Dict[str, Any]:
try:
return {
"model": self.model_name,
"provider": "grok",
"description": "Llama 3.3 70B Versatile - Excellent for medical QA",
"context_window": "131K tokens",
"max_completion_tokens": "32K tokens",
"capabilities": ["Medical reasoning", "Structured formatting", "Information extraction"]
}
except Exception as e:
self.logger.error(f"Failed to get model info: {e}")
return {"error": str(e)}
def get_stats(self) -> Dict[str, Any]:
return {
"total_requests": self.total_requests,
"current_model": self.model_name,
"provider": "grok"
}
class HTTPError(Exception):
pass