orachamp1981 commited on
Commit
9762962
Β·
verified Β·
1 Parent(s): 21b5747

Upload 4 files

Browse files
Files changed (2) hide show
  1. model.py +45 -31
  2. sql_templates.py +18 -0
model.py CHANGED
@@ -1,52 +1,60 @@
1
- # model.py
2
  from sentence_transformers import SentenceTransformer, util
3
- from sql_templates import sql_templates, sql_keyword_aliases, fuzzy_aliases
 
4
  import torch
5
 
6
- # Load training rules (string-to-SQL map)
7
- from data_loader import load_rules # you can split this for cleanliness
 
8
  rules = load_rules()
9
 
10
- # Load embedding model (lightweight, fast)
11
  model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L6-v2")
12
-
13
- # Pre-compute embeddings of training prompts
14
  train_prompts = list(rules.keys())
15
  train_embeddings = model.encode(train_prompts, convert_to_tensor=True)
16
 
 
 
 
 
 
 
17
  def oracle_sql_suggester(prompt):
18
  prompt_clean = prompt.strip().lower()
19
 
20
- # Try direct rule match
21
  if prompt_clean in rules:
22
  return rules[prompt_clean]
23
 
24
- # Check template keywords (first!)
 
 
 
 
 
 
 
 
 
 
25
  for word in prompt_clean.split():
26
- if word in sql_keyword_aliases:
27
- mapped_key = sql_keyword_aliases[word]
28
- return sql_templates[mapped_key]
29
-
 
30
  for key, template in sql_templates.items():
31
- if key.replace("_", " ") in prompt_clean or key in prompt_clean:
32
- return template
33
- # Try keyword aliases (word-level match)
34
- for word in prompt_clean.split():
35
- if word in sql_keyword_aliases:
36
- return sql_templates[sql_keyword_aliases[word]]
37
-
38
- # Try fuzzy alias matches
39
  for fuzzy_phrase, mapped_key in fuzzy_aliases.items():
40
- if fuzzy_phrase in prompt_clean:
41
- return sql_templates[mapped_key]
42
 
43
-
44
-
45
-
46
- # Semantic matching
47
  user_embedding = model.encode(prompt_clean, convert_to_tensor=True)
48
  cosine_scores = util.cos_sim(user_embedding, train_embeddings)
49
-
50
  top_match_index = torch.argmax(cosine_scores).item()
51
  top_score = cosine_scores[0][top_match_index].item()
52
 
@@ -54,6 +62,12 @@ def oracle_sql_suggester(prompt):
54
  matched_prompt = train_prompts[top_match_index]
55
  return rules[matched_prompt]
56
 
57
- # Fallback
58
- return "πŸ€– Sorry, I couldn’t understand that. Please try rephrasing your request."
59
-
 
 
 
 
 
 
 
 
1
  from sentence_transformers import SentenceTransformer, util
2
+ from sql_templates import sql_templates, sql_keyword_aliases, fuzzy_aliases, conflicting_phrases, greeting_templates
3
+ from data_loader import load_rules
4
  import torch
5
 
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
7
+
8
+ # πŸ“˜ Load rules
9
  rules = load_rules()
10
 
11
+ # πŸ” Load semantic model
12
  model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L6-v2")
 
 
13
  train_prompts = list(rules.keys())
14
  train_embeddings = model.encode(train_prompts, convert_to_tensor=True)
15
 
16
+ # πŸ€– Load local LLM model
17
+ llm_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
18
+ tokenizer = AutoTokenizer.from_pretrained(llm_name)
19
+ llm_model = AutoModelForCausalLM.from_pretrained(llm_name, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
20
+ llm_pipeline = pipeline("text-generation", model=llm_model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
21
+
22
  def oracle_sql_suggester(prompt):
23
  prompt_clean = prompt.strip().lower()
24
 
25
+ # βœ… Exact rule
26
  if prompt_clean in rules:
27
  return rules[prompt_clean]
28
 
29
+ # βœ… Greeting handling
30
+ for greet_key, greet_reply in greeting_templates.items():
31
+ if greet_key in prompt_clean:
32
+ return greet_reply
33
+
34
+ # βœ… Conflicting phrase
35
+ for terms, response in conflicting_phrases.items():
36
+ if all(term in prompt_clean for term in terms):
37
+ return response
38
+
39
+ # βœ… Keyword alias
40
  for word in prompt_clean.split():
41
+ if word in sql_keyword_aliases:
42
+ mapped_key = sql_keyword_aliases[word]
43
+ return sql_templates.get(mapped_key)
44
+
45
+ # βœ… Template match
46
  for key, template in sql_templates.items():
47
+ if key in prompt_clean or key.replace("_", " ") in prompt_clean:
48
+ return template
49
+
50
+ # βœ… Fuzzy match
 
 
 
 
51
  for fuzzy_phrase, mapped_key in fuzzy_aliases.items():
52
+ if fuzzy_phrase in prompt_clean:
53
+ return sql_templates.get(mapped_key)
54
 
55
+ # βœ… Semantic match
 
 
 
56
  user_embedding = model.encode(prompt_clean, convert_to_tensor=True)
57
  cosine_scores = util.cos_sim(user_embedding, train_embeddings)
 
58
  top_match_index = torch.argmax(cosine_scores).item()
59
  top_score = cosine_scores[0][top_match_index].item()
60
 
 
62
  matched_prompt = train_prompts[top_match_index]
63
  return rules[matched_prompt]
64
 
65
+ # βœ… Local LLM fallback
66
+ try:
67
+ prompt_text = f"Generate an Oracle SQL query or guidance for the following request:\n{prompt}\n\nSQL:"
68
+ output = llm_pipeline(prompt_text, max_new_tokens=256, do_sample=True, temperature=0.5)[0]["generated_text"]
69
+ return "πŸ€– (Local LLM): " + output.split("SQL:")[-1].strip()
70
+ except Exception as e:
71
+ print("⚠️ Local LLM error:", e)
72
+ return "πŸ€– Sorry, I couldn’t process that locally. Please try a simpler prompt."
73
+
sql_templates.py CHANGED
@@ -39,3 +39,21 @@ fuzzy_aliases = {
39
  "change row": "update",
40
  "erase record": "delete"
41
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  "change row": "update",
40
  "erase record": "delete"
41
  }
42
+
43
+ conflicting_phrases = {
44
+ ("modify", "new"): "⚠️ You cannot modify a new record. Consider using INSERT instead.",
45
+ ("update", "new"): "⚠️ You cannot update new data. Did you mean to INSERT?",
46
+ ("delete", "new"): "⚠️ You cannot delete something that doesn't exist yet.",
47
+ }
48
+
49
+ # πŸ€– Greeting phrases and responses
50
+ greeting_templates = {
51
+ "hello": "πŸ‘‹ Hello! How can I assist you with SQL or PL/SQL today?",
52
+ "hi": "πŸ‘‹ Hi there! Need help with Oracle SQL or PL/SQL?",
53
+ "hey": "πŸ‘‹ Hey! Let me know your SQL or PLSQL question.",
54
+ "good morning": "πŸŒ… Good morning! What Oracle task can I help with?",
55
+ "good afternoon": "🌞 Good afternoon! Ready to write some SQL?",
56
+ "good evening": "πŸŒ† Good evening! Need SQL/PLSQL guidance?",
57
+ "how are you": "πŸ€– I'm a bot but I'm always ready to help! What do you need?",
58
+ "what can you do": "πŸ’‘ I can help you generate Oracle SQL/PLSQL from descriptions, explain syntax, suggest queries, and more.",
59
+ }