nileshhanotia commited on
Commit
d6ed2ba
1 Parent(s): 8a184c2

Create intent.py

Browse files
Files changed (1) hide show
  1. intent.py +53 -0
intent.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+
4
+ # Load pre-trained model and tokenizer from Hugging Face
5
+ MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english" # Example, change to other open-source models if necessary
6
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+
9
+ # Define the intents
10
+ intents = {0: "database_query", 1: "product_description"}
11
+
12
+ # Function to classify query intent
13
+ def classify_intent(query):
14
+ inputs = tokenizer(query, return_tensors="pt", truncation=True, padding=True)
15
+ outputs = model(**inputs)
16
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
17
+ predicted_class = torch.argmax(probabilities).item()
18
+ return intents[predicted_class], probabilities[0][predicted_class].item()
19
+
20
+ # Example usage
21
+ query_1 = "Fetch all products with the keyword 'T-shirt' from the database."
22
+ query_2 = "Can you tell me about the description of this Shopify store?"
23
+
24
+ intent_1, confidence_1 = classify_intent(query_1)
25
+ intent_2, confidence_2 = classify_intent(query_2)
26
+
27
+ print(f"Query 1: '{query_1}'\nIntent: {intent_1} with confidence {confidence_1}\n")
28
+ print(f"Query 2: '{query_2}'\nIntent: {intent_2} with confidence {confidence_2}\n")
29
+
30
+ # Further routing based on classified intent
31
+ def handle_query(query):
32
+ intent, confidence = classify_intent(query)
33
+ if intent == "database_query":
34
+ # Call the natural language to SQL engine
35
+ return execute_database_query(query)
36
+ elif intent == "product_description":
37
+ # Call the RAG engine for product descriptionß
38
+ return execute_rag_query(query)
39
+ else:
40
+ return "Intent not recognized."
41
+
42
+ # Placeholder functions for database and RAG query handling
43
+ def execute_database_query(query):
44
+ # Integrate with SQL-based natural language query generator
45
+ return "Executing database query..."
46
+
47
+ def execute_rag_query(query):
48
+ # Integrate with RAG pipeline to retrieve product descriptions
49
+ return "Executing RAG product description query..."
50
+
51
+ # Test the function with different queries
52
+ print(handle_query(query_1))
53
+ print(handle_query(query_2))