Spaces:
Sleeping
Sleeping
Upload src\groq_integration.py with huggingface_hub
Browse files- src//groq_integration.py +143 -0
src//groq_integration.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Groq LLM Integration for Context Thread Agent
|
| 3 |
+
Provides fast, free reasoning without OpenAI API costs
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import Optional
|
| 8 |
+
import json
|
| 9 |
+
from groq import Groq
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class GroqReasoningEngine:
|
| 13 |
+
"""
|
| 14 |
+
Alternative reasoning engine using Groq API
|
| 15 |
+
Faster and free compared to OpenAI
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
def __init__(self, api_key: Optional[str] = None):
|
| 19 |
+
"""Initialize Groq client"""
|
| 20 |
+
self.api_key = api_key or os.getenv("GROQ_API_KEY")
|
| 21 |
+
if not self.api_key or self.api_key == "YOUR_GROQ_API_KEY_HERE":
|
| 22 |
+
raise ValueError("GROQ_API_KEY not found in environment")
|
| 23 |
+
|
| 24 |
+
self.client = Groq(api_key=self.api_key)
|
| 25 |
+
self.model = "llama-3.3-70b-versatile" # Latest Groq model
|
| 26 |
+
|
| 27 |
+
def reason_with_context(
|
| 28 |
+
self,
|
| 29 |
+
question: str,
|
| 30 |
+
context: str,
|
| 31 |
+
system_prompt: Optional[str] = None,
|
| 32 |
+
conversation_history: Optional[list] = None
|
| 33 |
+
) -> dict:
|
| 34 |
+
"""
|
| 35 |
+
Generate answer using Groq with context and conversation history
|
| 36 |
+
Returns: {answer, confidence, citations}
|
| 37 |
+
"""
|
| 38 |
+
if system_prompt is None:
|
| 39 |
+
system_prompt = """You are an expert data science assistant analyzing Jupyter notebooks and Excel documents.
|
| 40 |
+
Answer ONLY based on the provided context. Do not make up or infer information beyond what's shown.
|
| 41 |
+
If asked something not in the context, say "I cannot find that information in the provided document".
|
| 42 |
+
|
| 43 |
+
When referencing specific parts of code or analysis, cite them clearly using [Cell X] format.
|
| 44 |
+
Be precise, detailed, and technical in your answers.
|
| 45 |
+
If you see patterns, trends, or issues in the data/code, highlight them."""
|
| 46 |
+
|
| 47 |
+
messages = [
|
| 48 |
+
{
|
| 49 |
+
"role": "system",
|
| 50 |
+
"content": system_prompt
|
| 51 |
+
}
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
# Add conversation history for context continuity
|
| 55 |
+
if conversation_history:
|
| 56 |
+
for msg in conversation_history[-6:]: # Last 3 exchanges
|
| 57 |
+
messages.append({
|
| 58 |
+
"role": msg["role"],
|
| 59 |
+
"content": msg["content"]
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
# Add current query with context
|
| 63 |
+
messages.append({
|
| 64 |
+
"role": "user",
|
| 65 |
+
"content": f"Context from the document:\n{context}\n\nQuestion: {question}\n\nProvide a detailed answer based on the context:"
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
response = self.client.chat.completions.create(
|
| 70 |
+
model=self.model,
|
| 71 |
+
messages=messages,
|
| 72 |
+
max_tokens=2000,
|
| 73 |
+
temperature=0.2,
|
| 74 |
+
top_p=0.9
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
answer = response.choices[0].message.content
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
"answer": answer,
|
| 81 |
+
"confidence": 0.85,
|
| 82 |
+
"citations": []
|
| 83 |
+
}
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return {
|
| 86 |
+
"answer": f"Error using Groq: {str(e)}",
|
| 87 |
+
"confidence": 0.0,
|
| 88 |
+
"citations": []
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
def generate_keypoints(
|
| 92 |
+
self,
|
| 93 |
+
context: str,
|
| 94 |
+
max_points: int = 10
|
| 95 |
+
) -> dict:
|
| 96 |
+
"""
|
| 97 |
+
Generate key insights and summary points from document
|
| 98 |
+
"""
|
| 99 |
+
system_prompt = """You are an expert at analyzing data science notebooks and Excel documents.
|
| 100 |
+
Generate a comprehensive summary with key insights, findings, and important points.
|
| 101 |
+
Focus on: methodology, data transformations, key findings, issues/concerns, and conclusions.
|
| 102 |
+
Format your response as a clear, bulleted list."""
|
| 103 |
+
|
| 104 |
+
messages = [
|
| 105 |
+
{
|
| 106 |
+
"role": "system",
|
| 107 |
+
"content": system_prompt
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"role": "user",
|
| 111 |
+
"content": f"""Analyze this document and provide {max_points} key points covering:
|
| 112 |
+
1. Purpose and methodology
|
| 113 |
+
2. Data characteristics and transformations
|
| 114 |
+
3. Key findings or patterns
|
| 115 |
+
4. Any issues, concerns, or anomalies
|
| 116 |
+
5. Overall conclusions
|
| 117 |
+
|
| 118 |
+
Document context:
|
| 119 |
+
{context}
|
| 120 |
+
|
| 121 |
+
Provide your analysis:"""
|
| 122 |
+
}
|
| 123 |
+
]
|
| 124 |
+
|
| 125 |
+
try:
|
| 126 |
+
response = self.client.chat.completions.create(
|
| 127 |
+
model=self.model,
|
| 128 |
+
messages=messages,
|
| 129 |
+
max_tokens=2500,
|
| 130 |
+
temperature=0.3
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
keypoints = response.choices[0].message.content
|
| 134 |
+
|
| 135 |
+
return {
|
| 136 |
+
"keypoints": keypoints,
|
| 137 |
+
"success": True
|
| 138 |
+
}
|
| 139 |
+
except Exception as e:
|
| 140 |
+
return {
|
| 141 |
+
"keypoints": f"Error generating keypoints: {str(e)}",
|
| 142 |
+
"success": False
|
| 143 |
+
}
|