Commit ·
527d73d
1
Parent(s): 0bf22fe
1234567891011121314151617181920212223242526272829303132333435363738395 unmodified lines454647484950515253545556575859606110 unmodified lines727374757677787980818283848586878889909192import asyncioimport gradio as grfrom fastapi import FastAPIfrom config import settingsfrom listener import RabbitListenerBasefrom rabbit_repo import RabbitRepofrom service import LLMServicefrom runners.base import ILLMRunner# --- Runner factory (stub) ---class EchoRunner(ILLMRunner):Type = "EchoRunner"async def StartProcess(self, llmServiceObj: dict): passasync def RemoveProcess(self, sessionId: str): passasync def StopRequest(self, sessionId: str): passasync def SendInputAndGetResponse(self, llmServiceObj: dict):# Emits a message back (you can choose queue names per your topology)passasync def runner_factory(llmServiceObj: dict) -> ILLMRunner:# Use llmServiceObj["LLMRunnerType"] to instantiate different runnersreturn EchoRunner()# --- Publisher and Service ---publisher = RabbitRepo(external_source="https://space.external") # put your ExternalUrl if you have oneservice = LLMService(publisher, runner_factory)# --- Handlers mapping .NET FuncName -> service method ---async def h_start(data): await service.StartProcess(data or {})async def h_user(data): await service.UserInput(data or {})async def h_remove(data): await service.RemoveSession(data or {})async def h_stop(data): await service.StopRequest(data or {})async def h_qir(data): await service.QueryIndexResult(data or {})async def h_getreg(data): await service.GetFunctionRegistry(False)async def h_getreg_f(data): await service.GetFunctionRegistry(True)handlers = {"llmStartSession": h_start,5 unmodified lines"getFunctionRegistryFiltered": h_getreg_f,}listener = RabbitListenerBase(service_id=settings.SERVICE_ID, handlers=handlers)# Declarations mirror your C# InitRabbitMQObjs()DECLS = [{"ExchangeName":"llmStartSession"+settings.SERVICE_ID, "FuncName":"llmStartSession", "MessageTimeout":600000, "RoutingKeys":[settings.RABBIT_ROUTING_KEY]},{"ExchangeName":"llmUserInput"+settings.SERVICE_ID, "FuncName":"llmUserInput", "MessageTimeout":600000, "RoutingKeys":[settings.RABBIT_ROUTING_KEY]},{"ExchangeName":"llmRemoveSession"+settings.SERVICE_ID, "FuncName":"llmRemoveSession", "MessageTimeout":60000, "RoutingKeys":[settings.RABBIT_ROUTING_KEY]},{"ExchangeName":"llmStopRequest"+settings.SERVICE_ID, "FuncName":"llmStopRequest", "MessageTimeout":60000, "RoutingKeys":[settings.RABBIT_ROUTING_KEY]},{"ExchangeName":"queryIndexResult"+settings.SERVICE_ID, "FuncName":"queryIndexResult", "MessageTimeout":60000, "RoutingKeys":[settings.RABBIT_ROUTING_KEY]},{"ExchangeName":"getFunctionRegistry"+settings.SERVICE_ID, "FuncName":"getFunctionRegistry", "MessageTimeout":60000, "RoutingKeys":[settings.RABBIT_ROUTING_KEY]},{"ExchangeName":"getFunctionRegistryFiltered"+settings.SERVICE_ID, "FuncName":"getFunctionRegistryFiltered", "MessageTimeout":60000, "RoutingKeys":[settings.RABBIT_ROUTING_KEY]},]# --- Gradio UI (for smoke test) ---10 unmodified linesapp = FastAPI()app = gr.mount_gradio_app(app, demo, path="/")@get("/health")async def health():return {"status":"ok"}@on_event("startup")async def on_start():await publisher.connect()await service.init()await listener.start(DECLS)@on_event("shutdown")async def on_stop():# aio-pika RobustConnection closes on GC; optionally add explicit closes if you add referencespassif __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)123456789101112131415161718192021222324252627282930313233343536373839405 unmodified lines46474849505152535455565758596061626364656667686970717273747510 unmodified lines8687888990919293949596979899100101102103104105106107# app.pyimport asyncioimport gradio as grfrom fastapi import FastAPIfrom config import settingsfrom rabbit_base import RabbitBasefrom listener import RabbitListenerBasefrom rabbit_repo import RabbitRepofrom service import LLMServicefrom runners.base import ILLMRunner# --- Runner factory (stub) ---class EchoRunner(ILLMRunner):Type = "EchoRunner"async def StartProcess(self, llmServiceObj: dict): passasync def RemoveProcess(self, sessionId: str): passasync def StopRequest(self, sessionId: str): passasync def SendInputAndGetResponse(self, llmServiceObj: dict): passasync def runner_factory(llmServiceObj: dict) -> ILLMRunner:return EchoRunner()# --- Publisher and Service ---publisher = RabbitRepo(external_source="https://space.external")service = LLMService(publisher, runner_factory)# --- Handlers mapping .NET FuncName -> service method ---async def h_start(data): await service.StartProcess(data or {})async def h_user(data): await service.UserInput(data or {})async def h_remove(data): await service.RemoveSession(data or {})async def h_stop(data): await service.StopRequest(data or {})async def h_qir(data): await service.QueryIndexResult(data or {})async def h_getreg(_): await service.GetFunctionRegistry(False)async def h_getreg_f(_): await service.GetFunctionRegistry(True)handlers = {"llmStartSession": h_start,5 unmodified lines"getFunctionRegistryFiltered": h_getreg_f,}# --- Listener wiring (fix: needs base + instance_name) ---base = RabbitBase()listener = RabbitListenerBase(base,instance_name=settings.RABBIT_INSTANCE_NAME, # <- queue prefix, like your .NET instancehandlers=handlers,)# Declarations mirror your C# InitRabbitMQObjs()DECLS = [{"ExchangeName": f"llmStartSession{settings.SERVICE_ID}", "FuncName": "llmStartSession","MessageTimeout": 600_000, "RoutingKeys": [settings.RABBIT_ROUTING_KEY]},{"ExchangeName": f"llmUserInput{settings.SERVICE_ID}", "FuncName": "llmUserInput","MessageTimeout": 600_000, "RoutingKeys": [settings.RABBIT_ROUTING_KEY]},{"ExchangeName": f"llmRemoveSession{settings.SERVICE_ID}", "FuncName": "llmRemoveSession","MessageTimeout": 60_000, "RoutingKeys": [settings.RABBIT_ROUTING_KEY]},{"ExchangeName": f"llmStopRequest{settings.SERVICE_ID}", "FuncName": "llmStopRequest","MessageTimeout": 60_000, "RoutingKeys": [settings.RABBIT_ROUTING_KEY]},{"ExchangeName": f"queryIndexResult{settings.SERVICE_ID}", "FuncName": "queryIndexResult","MessageTimeout": 60_000, "RoutingKeys": [settings.RABBIT_ROUTING_KEY]},{"ExchangeName": f"getFunctionRegistry{settings.SERVICE_ID}", "FuncName": "getFunctionRegistry","MessageTimeout": 60_000, "RoutingKeys": [settings.RABBIT_ROUTING_KEY]},{"ExchangeName": f"getFunctionRegistryFiltered{settings.SERVICE_ID}", "FuncName": "getFunctionRegistryFiltered","MessageTimeout": 60_000, "RoutingKeys": [settings.RABBIT_ROUTING_KEY]},]# --- Gradio UI (for smoke test) ---10 unmodified linesapp = FastAPI()app = gr.mount_gradio_app(app, demo, path="/")@app.get("/health")async def health():return {"status": "ok"}@app.on_event("startup")async def on_start():await publisher.connect()await service.init()await listener.start(DECLS)@app.on_event("shutdown")async def on_stop():# Optionally close connections/channels if you track thempassif __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)
app.py+39-24