shiva9876 commited on
Commit
c8c21cb
·
verified ·
1 Parent(s): 970df01

Upload 3 files

Browse files
Files changed (3) hide show
  1. core/llm.py +32 -0
  2. core/memory.py +15 -0
  3. core/utils.py +64 -0
core/llm.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from langchain_groq import ChatGroq
4
+
5
+ # Load environment variables
6
+ load_dotenv()
7
+
8
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
9
+ GROQ_MODEL = os.getenv("GROQ_MODEL", "llama2-70b-4096")
10
+ MAX_LENGTH = int(os.getenv("MAX_LENGTH", "512"))
11
+ TEMPERATURE = float(os.getenv("TEMPERATURE", "0.2"))
12
+
13
+
14
+ def create_llm():
15
+ if not GROQ_API_KEY or not GROQ_MODEL:
16
+ raise ValueError("GROQ_API_KEY and GROQ_MODEL must be set in the environment.")
17
+ return ChatGroq(
18
+ model=GROQ_MODEL,
19
+ api_key=GROQ_API_KEY,
20
+ temperature=TEMPERATURE,
21
+ max_tokens=MAX_LENGTH,
22
+ )
23
+
24
+ llm = None
25
+ try:
26
+ if GROQ_API_KEY and GROQ_MODEL:
27
+ llm = create_llm()
28
+ else:
29
+ print("LLM will not be initialized due to missing GROQ_API_KEY or GROQ_MODEL.")
30
+ except ValueError as e:
31
+ print(f"❌ Failed to initialize LLM: {e}")
32
+ llm = None
core/memory.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.memory import ConversationBufferWindowMemory
2
+ from typing import Dict
3
+
4
+ conversation_memories: Dict[str, ConversationBufferWindowMemory] = {}
5
+
6
+ def get_memory(session_id: str, max_history: int = 10) -> ConversationBufferWindowMemory:
7
+ """Get or create conversation memory for a session."""
8
+ if session_id not in conversation_memories:
9
+ conversation_memories[session_id] = ConversationBufferWindowMemory(
10
+ k=max_history,
11
+ return_messages=True,
12
+ input_key="input",
13
+ output_key="output"
14
+ )
15
+ return conversation_memories[session_id]
core/utils.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from typing import Any, Dict, List
3
+
4
+ def classify_message(message: str) -> str:
5
+ """Classify message type using LangChain approach."""
6
+ message_lower = message.lower().strip()
7
+
8
+ # Programming-related keywords
9
+ code_keywords = [
10
+ "write code", "python", "function", "class", "algorithm",
11
+ "programming", "code", "script", "loop", "variable", "import",
12
+ "def ", "return", "if ", "for ", "while ", "try:", "except:",
13
+ "list", "dict", "array", "dataframe", "numpy", "pandas"
14
+ ]
15
+
16
+ # Conversational keywords
17
+ conversation_keywords = [
18
+ "hi", "hello", "how are you", "what's up", "good morning",
19
+ "good afternoon", "good evening", "bye", "goodbye", "thank you",
20
+ "thanks", "help", "who are you", "what can you do"
21
+ ]
22
+
23
+ # Check for code-related content
24
+ if any(keyword in message_lower for keyword in code_keywords):
25
+ return "code"
26
+
27
+ # Check for conversational content
28
+ if any(keyword in message_lower for keyword in conversation_keywords):
29
+ return "conversation"
30
+
31
+ # Check for question marks or short messages
32
+ if message_lower.endswith("?") or len(message_lower.split()) <= 5:
33
+ return "conversation"
34
+
35
+ # Default to conversation for ambiguous cases
36
+ return "conversation"
37
+
38
+
39
+ def process_response(raw_response: str, response_type: str) -> Dict[str, Any]:
40
+ """Process and format the model's response."""
41
+ if response_type == "conversation":
42
+ return {"response": raw_response.strip()}
43
+ elif response_type == "code":
44
+ code_match = re.search(r"```python\n(.*?)\n```", raw_response, re.DOTALL)
45
+ if code_match:
46
+ return {"generated_code": code_match.group(1).strip()}
47
+ return {"generated_code": raw_response.strip()}
48
+ elif response_type == "explanation":
49
+ explanation = re.sub(r"```python\n.*?\n```", "", raw_response, flags=re.DOTALL).strip()
50
+ return {"explanation": explanation}
51
+ else: # "both"
52
+ code = None
53
+ explanation = raw_response
54
+ code_match = re.search(r"```python\n(.*?)\n```", raw_response, re.DOTALL)
55
+ if code_match:
56
+ code = code_match.group(1).strip()
57
+ explanation = re.sub(r"```python\n.*?\n```", "", raw_response, flags=re.DOTALL).strip()
58
+ explanation = re.sub(r"\[EXPLANATION\]\s*", "", explanation, flags=re.IGNORECASE).strip()
59
+ explanation = re.sub(r"\[CODE\]\s*", "", explanation, flags=re.IGNORECASE).strip()
60
+ return {
61
+ "response": raw_response,
62
+ "generated_code": code,
63
+ "explanation": explanation
64
+ }