william-ai-dev/Seed-Oil-Meta-Engine
0
1import chainlit as cl2import asyncio3import sys4import os5sys.path.append(os.path.dirname(os.path.abspath(__file__)))6from query import get_retriever, get_chat_engine7 8MAX_MESSAGES_PER_SESSION = 20 9 10@cl.on_chat_start11async def start():12 cl.user_session.set("initialized", False)13 14 retriever = await cl.make_async(get_retriever)()15 chat_engine = await cl.make_async(get_chat_engine)(retriever)16 17 cl.user_session.set("chat_engine", chat_engine)18 cl.user_session.set("message_count", 0)19 20 cl.user_session.set("initialized", True)21 await cl.Message(content="**Seed Oil Research Analyst Ready**\n\nAccess peer-reviewed research on seed oils and related topics. Submit your inquiry for objective data extraction.").send()22 23@cl.on_message24async def main(message: cl.Message):25 if not cl.user_session.get("initialized"):26 return 27 28 chat_engine = cl.user_session.get("chat_engine")29 count = cl.user_session.get("message_count", 0)30 31 if count >= MAX_MESSAGES_PER_SESSION:32 await cl.Message(content=" **Session limit reached.** To preserve API capacity for other researchers, please start a new chat.").send()33 return34 35 cl.user_session.set("message_count", count + 1)36 msg = cl.Message(content="")37 38 try:39 print(f"Processing query: {message.content}")40 streaming_response = await cl.make_async(chat_engine.stream_chat)(message.content)41 42 source_count = len(streaming_response.source_nodes) if streaming_response.source_nodes else 043 print(f"Found {source_count} source nodes")44 45 if not streaming_response.source_nodes:46 print("No source nodes found - returning out of scope message")47 await cl.Message(content="This inquiry falls outside the technical scope of the current library.").send()48 return49 else:50 print(f"Processing response with {source_count} sources")51 52 except Exception as e:53 print(f"Exception in stream_chat: {e}")54 print(f"Exception type: {type(e)}")55 if "429" in str(e):56 retry_msg = cl.Message(content="⚠️ **Rate Limit Reached.** Cooling down... Retrying in 30s.")57 await retry_msg.send()58 await asyncio.sleep(30)59 await retry_msg.remove()60 streaming_response = await cl.make_async(chat_engine.stream_chat)(message.content)61 elif "doc_id" in str(e) or "not found" in str(e):62 await cl.Message(content="This inquiry falls outside the technical scope of the current library.").send()63 return64 elif "json" in str(e).lower() or "Failed to generate JSON" in str(e):65 await cl.Message(content="This inquiry falls outside the technical scope of the current library.").send()66 return67 else:68 await cl.Message(content="This inquiry falls outside the technical scope of the current library.").send()69 return70 71 for token in streaming_response.response_gen:72 await msg.stream_token(token)73 await asyncio.sleep(0.01) 74 75 await msg.update()