Spaces:
Sleeping
Sleeping
Update chatbot-gradio.py
Browse files- chatbot-gradio.py +10 -15
chatbot-gradio.py
CHANGED
|
@@ -64,7 +64,7 @@ class NL2SQLPlugin:
|
|
| 64 |
chat_history.add_user_message(f"""Convert to Cosmos DB SQL: {question}
|
| 65 |
Collection: converters (alias 'c')
|
| 66 |
Fields:
|
| 67 |
-
- c.type (e.g., '350mA') - for queries related to current (mA) always refer to c.type
|
| 68 |
- c.artnr (numeric (int) article number e.g., 930546)
|
| 69 |
- c.output_voltage_v: dictionary with min/max values for output voltage
|
| 70 |
- c.output_voltage_v.min (e.g., 15)
|
|
@@ -78,7 +78,6 @@ class NL2SQLPlugin:
|
|
| 78 |
- c.class (safety class)
|
| 79 |
- c.dimmability (e.g. if not dimmable 'NOT DIMMABLE'. if supports dimming, 'DALI/TOUCHDIM','MAINS DIM LC', '1-10V','CASAMBI' etc)
|
| 80 |
- c.listprice (e.g., 58)
|
| 81 |
-
- c.lifecycle (e.g., 'Active')
|
| 82 |
- c.size (e.g., '150x30x30')
|
| 83 |
- c.dimlist_type (e.g., 'DALI')
|
| 84 |
- c.pdf_link (link to product PDF)
|
|
@@ -107,7 +106,6 @@ class NL2SQLPlugin:
|
|
| 107 |
"name": "POWERLED REMOTE CONVERTER (18.2W) TOUCH DALI DIM 350mA",
|
| 108 |
"listprice": 47,
|
| 109 |
"unit": "PC",
|
| 110 |
-
"lifecycle": "A",
|
| 111 |
"pdf_link": "...",
|
| 112 |
"lamps": {{
|
| 113 |
"Single led XPE": {{"min": 3, "max": 15}},
|
|
@@ -130,8 +128,9 @@ class NL2SQLPlugin:
|
|
| 130 |
}}
|
| 131 |
SQL Guidelines (if needed):
|
| 132 |
- Always use SELECT * and never individual fields
|
| 133 |
-
- When current like 350mA is detected, always query the c.type field
|
| 134 |
- Always refer to fields in SELECT or WHERE clause using c.<field_name>
|
|
|
|
| 135 |
- For exact matches use: WHERE c.[field] = value
|
| 136 |
- For ranges use: WHERE c.[field].min = X AND c.[field].max = Y
|
| 137 |
- Check for dimmability support by using either != "NOT DIMMABLE" or = "NOT DIMMABLE"
|
|
@@ -153,14 +152,6 @@ class NL2SQLPlugin:
|
|
| 153 |
chat_history=chat_history,
|
| 154 |
settings=AzureChatPromptExecutionSettings()
|
| 155 |
)
|
| 156 |
-
# logger.info(f"Response dB schema{response}")
|
| 157 |
-
log_sql_func = kernel.get_function("ChatMemoryPlugin", "log_sql_query")
|
| 158 |
-
|
| 159 |
-
await kernel.invoke(
|
| 160 |
-
function=log_sql_func,
|
| 161 |
-
original_question=question,
|
| 162 |
-
generated_sql=response
|
| 163 |
-
)
|
| 164 |
|
| 165 |
return str(response)
|
| 166 |
|
|
@@ -209,12 +200,13 @@ async def handle_query(user_input: str):
|
|
| 209 |
3. Use simple functions if query matches these patterns:
|
| 210 |
- "lamps for [artnr]" β get_compatible_lamps
|
| 211 |
- "converters for [lamp type]" β get_converters_by_lamp_type
|
| 212 |
-
- "min/max [lamp] for [artnr]" β get_lamp_limits
|
| 213 |
- "drivers on 24V output" β get_converters_by_voltage_current
|
| 214 |
- "drivers on 350ma" β get_converters_by_voltage_current
|
| 215 |
|
| 216 |
4. Use SQL generation ONLY when:
|
| 217 |
- Query contains schema keywords: price, type, ip, efficiency, size, class, strain relief, lifecycle,
|
|
|
|
| 218 |
- Combining multiple conditions (AND/OR/NOT)
|
| 219 |
- Needs complex filtering/sorting
|
| 220 |
- Requesting technical specifications for a specific converter like "dimming type of converter [artnr]", "size of [artnr]"
|
|
@@ -244,19 +236,22 @@ async def handle_query(user_input: str):
|
|
| 244 |
User: "Dimming type of 930581" β generate_sql
|
| 245 |
User: "List of dali drivers on 24V output?" β get_converters_by_dimming"
|
| 246 |
User: 'List of 24V drivers for ledline medium power β get_converters_by_dimming(dimming_type=None, lamp_type="ledline medium power",voltage_current="24V")(or) get_converters_by_lamp_type(lamp_type="ledline medium power") β inspect returned converters '
|
|
|
|
| 247 |
"""
|
| 248 |
try:
|
| 249 |
result = await kernel.invoke_prompt(
|
| 250 |
prompt=prompt,
|
| 251 |
settings=settings
|
| 252 |
)
|
| 253 |
-
|
|
|
|
|
|
|
| 254 |
log_func = kernel.get_function("ChatMemoryPlugin", "log_interaction")
|
| 255 |
await log_func.invoke(
|
| 256 |
kernel=kernel,
|
| 257 |
session_id=session_id,
|
| 258 |
question=user_input,
|
| 259 |
-
function_used=
|
| 260 |
answer=str(result)
|
| 261 |
)
|
| 262 |
|
|
|
|
| 64 |
chat_history.add_user_message(f"""Convert to Cosmos DB SQL: {question}
|
| 65 |
Collection: converters (alias 'c')
|
| 66 |
Fields:
|
| 67 |
+
- c.type (e.g., '350mA','180mA','700mA','24V DC','48V') - for queries related to current (mA) always refer to c.type
|
| 68 |
- c.artnr (numeric (int) article number e.g., 930546)
|
| 69 |
- c.output_voltage_v: dictionary with min/max values for output voltage
|
| 70 |
- c.output_voltage_v.min (e.g., 15)
|
|
|
|
| 78 |
- c.class (safety class)
|
| 79 |
- c.dimmability (e.g. if not dimmable 'NOT DIMMABLE'. if supports dimming, 'DALI/TOUCHDIM','MAINS DIM LC', '1-10V','CASAMBI' etc)
|
| 80 |
- c.listprice (e.g., 58)
|
|
|
|
| 81 |
- c.size (e.g., '150x30x30')
|
| 82 |
- c.dimlist_type (e.g., 'DALI')
|
| 83 |
- c.pdf_link (link to product PDF)
|
|
|
|
| 106 |
"name": "POWERLED REMOTE CONVERTER (18.2W) TOUCH DALI DIM 350mA",
|
| 107 |
"listprice": 47,
|
| 108 |
"unit": "PC",
|
|
|
|
| 109 |
"pdf_link": "...",
|
| 110 |
"lamps": {{
|
| 111 |
"Single led XPE": {{"min": 3, "max": 15}},
|
|
|
|
| 128 |
}}
|
| 129 |
SQL Guidelines (if needed):
|
| 130 |
- Always use SELECT * and never individual fields
|
| 131 |
+
- When current like 350mA is detected, always query the c.type field
|
| 132 |
- Always refer to fields in SELECT or WHERE clause using c.<field_name>
|
| 133 |
+
- Do NOT use LIMIT. Instead use TOP <value> in SELECT statement like SELECT TOP 1 instead of LIMIT 1
|
| 134 |
- For exact matches use: WHERE c.[field] = value
|
| 135 |
- For ranges use: WHERE c.[field].min = X AND c.[field].max = Y
|
| 136 |
- Check for dimmability support by using either != "NOT DIMMABLE" or = "NOT DIMMABLE"
|
|
|
|
| 152 |
chat_history=chat_history,
|
| 153 |
settings=AzureChatPromptExecutionSettings()
|
| 154 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
return str(response)
|
| 157 |
|
|
|
|
| 200 |
3. Use simple functions if query matches these patterns:
|
| 201 |
- "lamps for [artnr]" β get_compatible_lamps
|
| 202 |
- "converters for [lamp type]" β get_converters_by_lamp_type
|
| 203 |
+
- "min/max [lamp] for [artnr]", "most [lamp]/ least [lamp]" β get_lamp_limits
|
| 204 |
- "drivers on 24V output" β get_converters_by_voltage_current
|
| 205 |
- "drivers on 350ma" β get_converters_by_voltage_current
|
| 206 |
|
| 207 |
4. Use SQL generation ONLY when:
|
| 208 |
- Query contains schema keywords: price, type, ip, efficiency, size, class, strain relief, lifecycle,
|
| 209 |
+
- Avoid using SQL generation for lamp related questions
|
| 210 |
- Combining multiple conditions (AND/OR/NOT)
|
| 211 |
- Needs complex filtering/sorting
|
| 212 |
- Requesting technical specifications for a specific converter like "dimming type of converter [artnr]", "size of [artnr]"
|
|
|
|
| 236 |
User: "Dimming type of 930581" β generate_sql
|
| 237 |
User: "List of dali drivers on 24V output?" β get_converters_by_dimming"
|
| 238 |
User: 'List of 24V drivers for ledline medium power β get_converters_by_dimming(dimming_type=None, lamp_type="ledline medium power",voltage_current="24V")(or) get_converters_by_lamp_type(lamp_type="ledline medium power") β inspect returned converters '
|
| 239 |
+
User: 'Which converter supports the most haloled lamps' β get_converters_by_lamp_type(lamp_type="haloled) β get_lamp_limits for each converter returned
|
| 240 |
"""
|
| 241 |
try:
|
| 242 |
result = await kernel.invoke_prompt(
|
| 243 |
prompt=prompt,
|
| 244 |
settings=settings
|
| 245 |
)
|
| 246 |
+
|
| 247 |
+
func_name = result.model_dump()["metadata"]["messages"]["messages"][2]["items"][0]["name"]
|
| 248 |
+
print(func_name)
|
| 249 |
log_func = kernel.get_function("ChatMemoryPlugin", "log_interaction")
|
| 250 |
await log_func.invoke(
|
| 251 |
kernel=kernel,
|
| 252 |
session_id=session_id,
|
| 253 |
question=user_input,
|
| 254 |
+
function_used=func_name,
|
| 255 |
answer=str(result)
|
| 256 |
)
|
| 257 |
|