@@ -111,7 +111,7 @@ def RAG_relevant_incidents(query, similarity_threshold=0.7):
111111 return incidents
112112
113113
114- def filter_relevant_incidents_with_llm (incidents , user_description ):
114+ async def filter_relevant_incidents_with_llm (incidents , user_description ):
115115 """
116116 Uses the LLM to filter out only the incidents relevant to the user's system.
117117 """
@@ -132,7 +132,7 @@ def filter_relevant_incidents_with_llm(incidents, user_description):
132132 )
133133
134134 structured_llm = llm .with_structured_output (IncidentIDList , method = "json_mode" )
135- incident_ids_obj = structured_llm .invoke (prompt )
135+ incident_ids_obj = await structured_llm .ainvoke (prompt )
136136
137137 logging .info (f"🔍 Incident filtering response: { incident_ids_obj } " )
138138
@@ -149,7 +149,7 @@ def filter_relevant_incidents_with_llm(incidents, user_description):
149149 return incidents
150150
151151
152- def generate_fmea_from_articles (incidents , user_description ):
152+ async def generate_fmea_from_articles (incidents , user_description ):
153153 """
154154 Generates a Software FMEA table using incidents linked to the retrieved articles.
155155 """
@@ -172,7 +172,7 @@ def generate_fmea_from_articles(incidents, user_description):
172172 )
173173
174174 logging .info ("Generating FMEA grounded in article-linked incidents..." )
175- response = conversation_chain .invoke ({"input" : prompt })["response" ]
175+ response = ( await conversation_chain .ainvoke ({"input" : prompt }) )["response" ]
176176 logging .info (f"FMEA Response:\n { response } " )
177177
178178 return response
@@ -218,13 +218,13 @@ async def on_message(message: cl.Message):
218218 incidents = await sync_to_async (RAG_relevant_incidents )(system_description )
219219
220220 await cl .Message (content = f"🔎 Found { len (incidents )} incidents. Filtering with LLM for most relevant incidents..." ).send ()
221- filtered_incidents = await sync_to_async ( filter_relevant_incidents_with_llm ) (incidents , system_description )
221+ filtered_incidents = await filter_relevant_incidents_with_llm (incidents , system_description )
222222
223223 filtered_incidents_str = "\n " .join ([f"- ID: { inc ['ID' ]} , Title: { inc ['Title' ]} " for inc in filtered_incidents ])
224224 await cl .Message (content = f"📋 **Filtered Incidents:**\n { filtered_incidents_str } " ).send ()
225225
226226 await cl .Message (content = f"📊 Generating FMEA from { len (filtered_incidents )} filtered incidents..." ).send ()
227- fmea_output = await sync_to_async ( generate_fmea_from_articles ) (filtered_incidents , system_description )
227+ fmea_output = await generate_fmea_from_articles (filtered_incidents , system_description )
228228
229229 cl .user_session .set ("fmea_context" , fmea_output )
230230 cl .user_session .set ("state" , "fmea_generated" )
@@ -247,7 +247,8 @@ async def on_message(message: cl.Message):
247247 incidents = await sync_to_async (RAG_relevant_incidents )(message .content )
248248
249249 await cl .Message (content = f"🔎 Found { len (incidents )} incidents. Filtering with LLM for most relevant incidents..." ).send ()
250- filtered_incidents = await sync_to_async (filter_relevant_incidents_with_llm )(incidents , system_description )
250+ system_description = cl .user_session .get ("system_description" , message .content )
251+ filtered_incidents = await filter_relevant_incidents_with_llm (incidents , system_description )
251252
252253 filtered_incidents_str = "\n " .join ([f"- ID: { inc ['ID' ]} , Title: { inc ['Title' ]} " for inc in filtered_incidents ])
253254 await cl .Message (content = f"📋 **Filtered Incidents:**\n { filtered_incidents_str } " ).send ()
@@ -256,7 +257,7 @@ async def on_message(message: cl.Message):
256257
257258 if not incidents :
258259 await cl .Message (content = f"🔍 No relevant incidents found." ).send ()
259- response = (await sync_to_async ( conversation_chain .invoke )({ ' input' : message .content }))[' response' ]
260+ response = (await conversation_chain .ainvoke ({ " input" : message .content }))[" response" ]
260261 await cl .Message (content = response ).send ()
261262 return
262263
@@ -272,12 +273,12 @@ async def on_message(message: cl.Message):
272273 "Cite incident IDs when you use information from them."
273274 )
274275
275- response = (await sync_to_async ( conversation_chain .invoke )({ ' input' : prompt }))[' response' ]
276+ response = (await conversation_chain .ainvoke ({ " input" : prompt }))[" response" ]
276277 await cl .Message (content = response ).send ()
277278
278279 elif state == "fmea_generated" :
279280 follow_up = message .content
280- response = (await sync_to_async ( conversation_chain .invoke )({ ' input' : follow_up }))[' response' ]
281+ response = (await conversation_chain .ainvoke ({ " input" : follow_up }))[" response" ]
281282 await cl .Message (content = response ).send ()
282283
283284 else : # state is "initial" or None
@@ -307,4 +308,4 @@ async def on_restart(action):
307308
308309
309310#TODO:
310- # - Display relevant incidents and link them to the website
311+ # - Display relevant incidents and link them to the website
0 commit comments