-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_base.py
More file actions
203 lines (175 loc) · 7.88 KB
/
knowledge_base.py
File metadata and controls
203 lines (175 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Knowledge Base for Vehicle Safety Chatbot
Simple keyword-based RAG system for domain knowledge retrieval.
"""
# --- FIELD-SPECIFIC KNOWLEDGE ---
FIELD_KNOWLEDGE = {
"VIN": {
"description": "Vehicle Identification Number - a unique 17-character code identifying your vehicle",
"where_to_find": [
"Driver's side dashboard (visible through the windshield from outside)",
"Driver's side door jamb - on a sticker when you open the door",
"Vehicle registration document or title",
"Insurance card or policy documents",
"Vehicle owner's manual"
],
"format_rules": "17 alphanumeric characters. Cannot contain letters I, O, or Q.",
"example": "1HGBH41JXMN109186"
},
"Make": {
"description": "The manufacturer/brand of the vehicle",
"common_values": [
"Maruti Suzuki", "Hyundai", "Tata", "Mahindra", "Honda", "Toyota",
"Kia", "Skoda", "Volkswagen", "Renault", "Nissan", "MG",
"BMW", "Mercedes-Benz", "Audi", "Volvo", "Jeep",
"Force Motors", "Ashok Leyland", "Isuzu"
],
"tip": "This is the brand name, not the model. For example, 'Maruti Suzuki' is the make, 'Swift' is the model."
},
"Model": {
"description": "The specific model name of the vehicle",
"tip": "This is the specific name like 'Swift', 'Creta', 'Nexon', 'Fortuner', 'XUV700', etc."
},
"Model_Year": {
"description": "The year the vehicle was manufactured",
"format_rules": "4-digit year between 1980 and current year + 1",
"tip": "This can be found on your Registration Certificate (RC), insurance papers, or chassis plate."
},
"State": {
"description": "Indian State or Union Territory where the incident occurred",
"format_rules": "2-letter vehicle registration code (e.g., MH, DL, KA, TN)",
"valid_codes": [
"AN", "AP", "AR", "AS", "BR", "CG", "CH", "DD", "DL", "DN",
"GA", "GJ", "HP", "HR", "JH", "JK", "KA", "KL", "LA", "LD",
"MH", "ML", "MN", "MP", "MZ", "NL", "OD", "PB", "PY", "RJ",
"SK", "TN", "TR", "TS", "UK", "UP", "WB"
],
"tip": "Use the vehicle registration state code (e.g., MH for Maharashtra, DL for Delhi)."
},
"Component": {
"description": "The vehicle part or system that failed or malfunctioned",
"common_components": [
"Brakes", "Steering", "Airbags", "Seatbelts", "Engine", "Transmission",
"Fuel System", "Electrical System", "Suspension", "Tires", "Wheels",
"Powertrain", "Exhaust", "Cooling System", "HVAC", "Door Latches",
"Windshield Wipers", "Lights/Headlights", "Horn", "Accelerator/Throttle",
"Battery (EV)", "Software/Electronics", "Backup Camera", "Cruise Control"
],
"tip": "Describe which part of the vehicle had the problem."
},
"Speed": {
"description": "Approximate speed of the vehicle when the incident occurred",
"format_rules": "Number in miles per hour (mph), typically 0-200",
"tip": "If parked, enter 0. If unsure, provide your best estimate."
},
"Crash": {
"description": "Whether the incident resulted in a vehicle crash or collision",
"format_rules": "Yes or No"
},
"Fire": {
"description": "Whether there was any fire, smoke, or burning smell",
"format_rules": "Yes or No"
},
"Injured": {
"description": "Number of people injured in the incident",
"format_rules": "Enter 0 if no injuries, otherwise the number of people hurt"
},
"Deaths": {
"description": "Number of fatalities in the incident",
"format_rules": "Enter 0 if no deaths, otherwise the number"
},
"Mileage": {
"description": "Total miles on the vehicle's odometer at time of incident",
"where_to_find": ["Dashboard odometer display", "Maintenance records"],
"tip": "If unsure of exact mileage, provide an estimate"
},
"Description": {
"description": "Detailed description of what happened",
"tip": "Please include: what you were doing, what went wrong, any warning signs, and the outcome. The more detail, the better!"
},
"Date_Complaint": {
"description": "The date when the incident occurred",
"format_rules": "Format: YYYY-MM-DD (e.g., 2024-03-15)"
},
"City": {
"description": "The city where the incident occurred",
"tip": "Enter the city name, e.g., 'Los Angeles', 'Houston', 'New York'"
}
}
# --- GENERAL SAFETY KNOWLEDGE ---
SAFETY_KNOWLEDGE = {
"recall_info": """
Vehicle recalls are issued when a manufacturer or NHTSA determines that a vehicle
or equipment creates a safety risk or fails to meet minimum safety standards.
You can check for recalls at NHTSA.gov using your VIN.
""",
"what_to_do_after_incident": """
After a safety incident:
1. Ensure everyone is safe and seek medical attention if needed
2. Document the incident with photos if possible
3. Note the date, time, location, and circumstances
4. Do not attempt to repair the defect until reported
5. Keep all receipts and repair records
""",
"why_report": """
Reporting safety issues helps:
- NHTSA identify patterns that could lead to recalls
- Protect other consumers from similar issues
- Hold manufacturers accountable for defects
- Improve overall vehicle safety standards
"""
}
def retrieve_context_for_field(field_name):
"""
Retrieves relevant knowledge for a specific field.
Returns formatted context string for LLM prompt.
"""
if field_name not in FIELD_KNOWLEDGE:
return ""
knowledge = FIELD_KNOWLEDGE[field_name]
context_parts = []
# Add description
if "description" in knowledge:
context_parts.append(f"What it is: {knowledge['description']}")
# Add where to find (for VIN, etc.)
if "where_to_find" in knowledge:
locations = ", ".join(knowledge["where_to_find"][:3])
context_parts.append(f"Where to find it: {locations}")
# Add format rules
if "format_rules" in knowledge:
context_parts.append(f"Format: {knowledge['format_rules']}")
# Add example if available
if "example" in knowledge:
context_parts.append(f"Example: {knowledge['example']}")
# Add tip if available
if "tip" in knowledge:
context_parts.append(f"Tip: {knowledge['tip']}")
# Add common values if available (limit to 5)
if "common_values" in knowledge:
values = ", ".join(knowledge["common_values"][:8])
context_parts.append(f"Common values: {values}...")
if "common_components" in knowledge:
components = ", ".join(knowledge["common_components"][:8])
context_parts.append(f"Common options: {components}...")
return " | ".join(context_parts)
def get_valid_state_codes():
"""Returns list of valid US state codes"""
return FIELD_KNOWLEDGE.get("State", {}).get("valid_codes", [])
def get_common_makes():
"""Returns list of common vehicle makes"""
return FIELD_KNOWLEDGE.get("Make", {}).get("common_values", [])
def get_common_components():
"""Returns list of common vehicle components"""
return FIELD_KNOWLEDGE.get("Component", {}).get("common_components", [])
def retrieve_general_context(topic):
"""
Retrieves general safety knowledge based on topic keywords.
"""
topic_lower = topic.lower()
if any(word in topic_lower for word in ["recall", "nhtsa"]):
return SAFETY_KNOWLEDGE["recall_info"]
if any(word in topic_lower for word in ["after", "incident", "what to do", "next"]):
return SAFETY_KNOWLEDGE["what_to_do_after_incident"]
if any(word in topic_lower for word in ["why", "report", "important", "reason"]):
return SAFETY_KNOWLEDGE["why_report"]
return ""