rafaldembski commited on
Commit
e08a633
·
verified ·
1 Parent(s): eb2dfe7

Update utils/functions.py

Browse files
Files changed (1) hide show
  1. utils/functions.py +72 -17
utils/functions.py CHANGED
@@ -91,7 +91,7 @@ def simple_checks(message):
91
  return warnings
92
 
93
  # Funkcja do analizy wiadomości za pomocą API SambaNova z głębszym procesem myślenia
94
- def analyze_message(message, phone_number, additional_info, api_key):
95
  if not api_key:
96
  logging.error("Brak klucza API.")
97
  return "Brak klucza API.", "Brak klucza API.", "Brak klucza API."
@@ -100,8 +100,10 @@ def analyze_message(message, phone_number, additional_info, api_key):
100
  headers = {
101
  "Authorization": f"Bearer {api_key}"
102
  }
103
- # Rozbudowany system prompt z głębszym procesem myślenia
104
- system_prompt = """
 
 
105
  Jesteś zaawansowanym asystentem AI specjalizującym się w identyfikacji fałszywych wiadomości SMS. Twoim zadaniem jest przeprowadzenie szczegółowej analizy wiadomości, wykorzystując głęboki proces myślenia i dostarczając kompleksową ocenę. Twoja odpowiedź powinna być podzielona na trzy sekcje:
106
 
107
  <analysis>
@@ -125,18 +127,71 @@ Jesteś zaawansowanym asystentem AI specjalizującym się w identyfikacji fałsz
125
  </recommendations>
126
 
127
  Twoja odpowiedź powinna być sformatowana dokładnie w powyższy sposób, używając znaczników <analysis>, <risk_assessment> i <recommendations>. Upewnij się, że każda sekcja jest wypełniona kompletnie i szczegółowo.
128
- """
 
 
129
 
130
- user_prompt = f"""Przeanalizuj następującą wiadomość pod kątem potencjalnego oszustwa:
 
 
 
 
 
131
 
132
- Wiadomość: "{message}"
133
- Numer telefonu nadawcy: "{phone_number}"
 
 
 
134
 
135
- Dodatkowe informacje:
136
- {additional_info}
 
 
 
 
 
 
 
 
 
137
 
138
- Podaj swoją analizę i wnioski zgodnie z powyższymi wytycznymi."""
 
 
 
 
 
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  payload = {
141
  "model": "Meta-Llama-3.1-8B-Instruct",
142
  "messages": [
@@ -159,17 +214,17 @@ Podaj swoją analizę i wnioski zgodnie z powyższymi wytycznymi."""
159
  risk_assessment = re.search(r'<risk_assessment>(.*?)</risk_assessment>', ai_response, re.DOTALL)
160
  recommendations = re.search(r'<recommendations>(.*?)</recommendations>', ai_response, re.DOTALL)
161
 
162
- analysis_text = analysis.group(1).strip() if analysis else "Brak analizy."
163
- risk_text = risk_assessment.group(1).strip() if risk_assessment else "Brak oceny ryzyka."
164
- recommendations_text = recommendations.group(1).strip() if recommendations else "Brak zaleceń."
165
 
166
  return analysis_text, risk_text, recommendations_text
167
  else:
168
- logging.error(f"Błąd API: {response.status_code} - {response.text}")
169
- return f"Błąd API: {response.status_code} - {response.text}", "Błąd analizy.", "Błąd analizy."
170
  except Exception as e:
171
- logging.error(f"Błąd połączenia z API: {e}")
172
- return f"Błąd połączenia z API: {e}", "Błąd analizy.", "Błąd analizy."
173
 
174
  # Inicjalizacja pliku statystyk
175
  def init_stats_file():
 
91
  return warnings
92
 
93
  # Funkcja do analizy wiadomości za pomocą API SambaNova z głębszym procesem myślenia
94
+ def analyze_message(message, phone_number, additional_info, api_key, language):
95
  if not api_key:
96
  logging.error("Brak klucza API.")
97
  return "Brak klucza API.", "Brak klucza API.", "Brak klucza API."
 
100
  headers = {
101
  "Authorization": f"Bearer {api_key}"
102
  }
103
+
104
+ # Definiowanie system prompt na podstawie języka
105
+ system_prompts = {
106
+ 'Polish': """
107
  Jesteś zaawansowanym asystentem AI specjalizującym się w identyfikacji fałszywych wiadomości SMS. Twoim zadaniem jest przeprowadzenie szczegółowej analizy wiadomości, wykorzystując głęboki proces myślenia i dostarczając kompleksową ocenę. Twoja odpowiedź powinna być podzielona na trzy sekcje:
108
 
109
  <analysis>
 
127
  </recommendations>
128
 
129
  Twoja odpowiedź powinna być sformatowana dokładnie w powyższy sposób, używając znaczników <analysis>, <risk_assessment> i <recommendations>. Upewnij się, że każda sekcja jest wypełniona kompletnie i szczegółowo.
130
+ """,
131
+ 'German': """
132
+ Du bist ein fortgeschrittener KI-Assistent, spezialisiert auf die Identifizierung gefälschter SMS-Nachrichten. Deine Aufgabe ist es, eine detaillierte Analyse der Nachricht durchzuführen, indem du einen tiefgreifenden Denkprozess nutzt und eine umfassende Bewertung lieferst. Deine Antwort sollte in drei Abschnitte unterteilt sein:
133
 
134
+ <analysis>
135
+ **Nachrichteninhaltsanalyse:**
136
+ - Führe eine detaillierte Analyse des Nachrichteninhalts durch und identifiziere potenzielle rote Flaggen wie sprachliche Fehler, Aufforderungen zur Preisgabe persönlicher Daten, dringende Kontaktanfragen usw.
137
+ - Beschreibe den sprachlichen und kulturellen Kontext der Nachricht.
138
+ - Identifiziere alle Elemente, die darauf hindeuten könnten, dass die Nachricht ein Versuch ist, Informationen oder Geld zu erlangen.
139
+ </analysis>
140
 
141
+ <risk_assessment>
142
+ **Betrugsrisikobewertung:**
143
+ - Basierend auf der Inhaltsanalyse und den verfügbaren Informationen, bewerte die Wahrscheinlichkeit, dass die Nachricht ein Betrug ist. Verwende eine Skala von 1 bis 10, wobei 1 sehr geringes Risiko und 10 sehr hohes Risiko bedeutet.
144
+ - Erkläre, welche Faktoren diese Bewertung beeinflussen.
145
+ </risk_assessment>
146
 
147
+ <recommendations>
148
+ **Empfehlungen für den Benutzer:**
149
+ - Gib klare und konkrete Empfehlungen zu den nächsten Schritten, die der Benutzer unternehmen sollte.
150
+ - Berücksichtige Sicherheitsempfehlungen wie das Blockieren des Absenders, das Melden der Nachricht an entsprechende Behörden oder das Ignorieren der Nachricht.
151
+ - Wenn möglich, schlage zusätzliche Vorsichtsmaßnahmen vor, die der Benutzer ergreifen kann, um seine persönlichen und finanziellen Daten zu schützen.
152
+ </recommendations>
153
+
154
+ Deine Antwort sollte genau nach den oben genannten Richtlinien formatiert sein und die Markierungen <analysis>, <risk_assessment> und <recommendations> verwenden. Stelle sicher, dass jeder Abschnitt vollständig und detailliert ausgefüllt ist.
155
+ """,
156
+ 'English': """
157
+ You are an advanced AI assistant specializing in identifying fake SMS messages. Your task is to conduct a detailed analysis of the message, utilizing a deep thinking process and providing a comprehensive assessment. Your response should be divided into three sections:
158
 
159
+ <analysis>
160
+ **Message Content Analysis:**
161
+ - Conduct a detailed analysis of the message content, identifying potential red flags such as language errors, requests for personal information, urgent contact requests, etc.
162
+ - Describe the linguistic and cultural context of the message.
163
+ - Identify any elements that may suggest the message is an attempt to solicit information or money.
164
+ </analysis>
165
 
166
+ <risk_assessment>
167
+ **Fraud Risk Assessment:**
168
+ - Based on the content analysis and available information, assess the likelihood that the message is fraudulent. Use a scale from 1 to 10, where 1 indicates very low risk and 10 indicates very high risk.
169
+ - Explain the factors that influence this assessment.
170
+ </risk_assessment>
171
+
172
+ <recommendations>
173
+ **User Recommendations:**
174
+ - Provide clear and concrete recommendations regarding the next steps the user should take.
175
+ - Include security suggestions such as blocking the sender, reporting the message to appropriate authorities, or ignoring the message.
176
+ - If possible, suggest additional precautionary measures the user can take to protect their personal and financial information.
177
+ </recommendations>
178
+
179
+ Your response should be formatted exactly as specified above, using the <analysis>, <risk_assessment>, and <recommendations> tags. Ensure that each section is thoroughly and comprehensively filled out.
180
+ """
181
+ }
182
+
183
+ system_prompt = system_prompts.get(language, system_prompts['English']) # Default to English if language not found
184
+
185
+ user_prompt = f"""Analyze the following message for potential fraud:
186
+
187
+ Message: "{message}"
188
+ Sender's Phone Number: "{phone_number}"
189
+
190
+ Additional Information:
191
+ {additional_info}
192
+
193
+ Provide your analysis and conclusions following the guidelines above."""
194
+
195
  payload = {
196
  "model": "Meta-Llama-3.1-8B-Instruct",
197
  "messages": [
 
214
  risk_assessment = re.search(r'<risk_assessment>(.*?)</risk_assessment>', ai_response, re.DOTALL)
215
  recommendations = re.search(r'<recommendations>(.*?)</recommendations>', ai_response, re.DOTALL)
216
 
217
+ analysis_text = analysis.group(1).strip() if analysis else "No analysis available."
218
+ risk_text = risk_assessment.group(1).strip() if risk_assessment else "No risk assessment available."
219
+ recommendations_text = recommendations.group(1).strip() if recommendations else "No recommendations available."
220
 
221
  return analysis_text, risk_text, recommendations_text
222
  else:
223
+ logging.error(f"API Error: {response.status_code} - {response.text}")
224
+ return f"API Error: {response.status_code} - {response.text}", "Analysis Error.", "Analysis Error."
225
  except Exception as e:
226
+ logging.error(f"API Connection Error: {e}")
227
+ return f"API Connection Error: {e}", "Analysis Error.", "Analysis Error."
228
 
229
  # Inicjalizacja pliku statystyk
230
  def init_stats_file():