SatyamD31 commited on
Commit
f8d190b
·
verified ·
1 Parent(s): b9e5f92

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +47 -40
rag.py CHANGED
@@ -84,55 +84,62 @@ class FinancialChatbot:
84
  BLOCKED_WORDS = re.compile(r"\b(hack|bypass|illegal|exploit|scam|kill|laundering|murder|suicide|self-harm)\b", re.IGNORECASE)
85
  return not bool(BLOCKED_WORDS.search(query))
86
 
87
- # def generate_answer(self, context, question):
88
- # messages = [
89
- # {"role": "system", "content": "You are a financial assistant. Answer only finance-related questions. If the question is not related to finance, reply: 'I'm sorry, but I can only answer financial-related questions.' If the user greets you (e.g., 'Hello', 'Hi', 'Good morning'), respond politely with 'Hello! How can I assist you today?'."},
90
- # {"role": "user", "content": f"{question} - related contect extracted form db {context}"}
91
- # ]
92
-
93
- # # Use Qwen's chat template
94
- # input_text = self.qwen_tokenizer.apply_chat_template(
95
- # messages, tokenize=False, add_generation_prompt=True
96
- # )
97
-
98
- # # Tokenize and move input to device
99
- # inputs = self.qwen_tokenizer([input_text], return_tensors="pt").to(self.device)
100
- # self.qwen_model.config.pad_token_id = self.qwen_tokenizer.eos_token_id
101
-
102
- # # Generate response
103
- # outputs = self.qwen_model.generate(
104
- # inputs.input_ids,
105
- # max_new_tokens=50,
106
- # pad_token_id=self.qwen_tokenizer.eos_token_id,
107
- # )
108
-
109
- # # Extract only the newly generated part
110
- # generated_ids = outputs[:, inputs.input_ids.shape[1]:] # Remove prompt part
111
- # response = self.qwen_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
112
-
113
- # return response
114
-
115
-
116
  def generate_answer(self, context, question):
117
- prompt = f"""
118
- You are a financial assistant. If the user greets you (e.g., "Hello," "Hi," "Good morning"), respond politely with 'Hello! How can I assist you today? without requiring context.
 
 
119
 
120
  For financial-related questions, answer based on the context provided. If the context lacks information, say "I don't know."
121
 
122
  Context: {context}
123
  User Query: {question}
124
- Answer:
125
- """
126
-
127
- input_text = prompt
128
- # f"Context: {context}\nQuestion: {question}\nAnswer:"
129
- inputs = self.qwen_tokenizer.encode(input_text, return_tensors="pt")
130
- # outputs = self.qwen_model.generate(inputs, max_length=100)
131
- outputs = self.qwen_model.generate(inputs, max_new_tokens=100)
132
- generated_ids = outputs[:, inputs.shape[1]:] # Remove prompt part
 
 
 
 
 
 
 
 
 
 
 
 
133
  response = self.qwen_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
134
  return response
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  def get_answer(self, query):
138
  """Main function to process a user query and return an answer."""
 
84
  BLOCKED_WORDS = re.compile(r"\b(hack|bypass|illegal|exploit|scam|kill|laundering|murder|suicide|self-harm)\b", re.IGNORECASE)
85
  return not bool(BLOCKED_WORDS.search(query))
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def generate_answer(self, context, question):
88
+ messages = [
89
+ # {"role": "system", "content": "You are a financial assistant. Answer only finance-related questions. If the question is not related to finance, reply: 'I'm sorry, but I can only answer financial-related questions.' If the user greets you (e.g., 'Hello', 'Hi', 'Good morning'), respond politely with 'Hello! How can I assist you today?'."},
90
+ # {"role": "user", "content": f"{question} - related contect extracted form db {context}"}
91
+ {"role": "user", "content": f"""You are a financial assistant. If the user greets you (e.g., "Hello," "Hi," "Good morning"), respond politely with 'Hello! How can I assist you today? without requiring context.
92
 
93
  For financial-related questions, answer based on the context provided. If the context lacks information, say "I don't know."
94
 
95
  Context: {context}
96
  User Query: {question}
97
+ Answer:"""}
98
+ ]
99
+
100
+ # Use Qwen's chat template
101
+ input_text = self.qwen_tokenizer.apply_chat_template(
102
+ messages, tokenize=False, add_generation_prompt=True
103
+ )
104
+
105
+ # Tokenize and move input to device
106
+ inputs = self.qwen_tokenizer([input_text], return_tensors="pt").to(self.device)
107
+ self.qwen_model.config.pad_token_id = self.qwen_tokenizer.eos_token_id
108
+
109
+ # Generate response
110
+ outputs = self.qwen_model.generate(
111
+ inputs.input_ids,
112
+ max_new_tokens=50,
113
+ pad_token_id=self.qwen_tokenizer.eos_token_id,
114
+ )
115
+
116
+ # Extract only the newly generated part
117
+ generated_ids = outputs[:, inputs.input_ids.shape[1]:] # Remove prompt part
118
  response = self.qwen_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
119
+
120
  return response
121
 
122
+
123
+ # def generate_answer(self, context, question):
124
+ # prompt = f"""
125
+ # You are a financial assistant. If the user greets you (e.g., "Hello," "Hi," "Good morning"), respond politely with 'Hello! How can I assist you today? without requiring context.
126
+
127
+ # For financial-related questions, answer based on the context provided. If the context lacks information, say "I don't know."
128
+
129
+ # Context: {context}
130
+ # User Query: {question}
131
+ # Answer:
132
+ # """
133
+
134
+ # input_text = prompt
135
+ # # f"Context: {context}\nQuestion: {question}\nAnswer:"
136
+ # inputs = self.qwen_tokenizer.encode(input_text, return_tensors="pt")
137
+ # # outputs = self.qwen_model.generate(inputs, max_length=100)
138
+ # outputs = self.qwen_model.generate(inputs, max_new_tokens=100)
139
+ # generated_ids = outputs[:, inputs.shape[1]:] # Remove prompt part
140
+ # response = self.qwen_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
141
+ # return response
142
+
143
 
144
  def get_answer(self, query):
145
  """Main function to process a user query and return an answer."""