From 9952eeda7171fe72801d5970ac0ce9a69d37d528 Mon Sep 17 00:00:00 2001 From: OwusuBlessing Date: Mon, 11 Aug 2025 23:16:48 +0100 Subject: [PATCH] updated bot chat api --- .../__pycache__/responses.cpython-311.pyc | Bin 885 -> 863 bytes api/models/responses.py | 4 +- api/routes/__pycache__/chat.cpython-311.pyc | Bin 3032 -> 3151 bytes api/routes/chat.py | 7 +- .../chat_templates.cpython-311.pyc | Bin 16028 -> 16176 bytes src/prompts/templates/chat_templates.py | 1 + test.py | 90 ++++++++++++++++++ test2.py | 32 +++++++ 8 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 test.py create mode 100644 test2.py diff --git a/api/models/__pycache__/responses.cpython-311.pyc b/api/models/__pycache__/responses.cpython-311.pyc index 1935afe09bdfb0bdebe286d26d899552136fc718..fedde2894bb17c466a352070798b7bcf2494d3ad 100644 GIT binary patch delta 195 zcmey$cAt%RIWI340}w38n3Zv7BJY98KN$rk-j!!s$u{wy6>AX-P-?OTV>Pc}gKvXx zBM>+E770u~z?dyx#0iuu;s+4|AVLsC2*U`D$*D}*Y+woD$CL!1k@o;2^Tcy{Oq#3{|5;78Wvu2EXz*$9X$0a1pCbOr zM;WuNG}($cff|ZH+KRYA1P_Sd1raPDf)7OSg9rf-Ap|41Ci^pKvw$ZP-%l`Me( diff --git a/api/models/responses.py b/api/models/responses.py index c954386..ff6d95c 100644 --- a/api/models/responses.py +++ b/api/models/responses.py @@ -3,10 +3,8 @@ from pydantic import BaseModel from typing import Dict class ChatResponse(BaseModel): status: str - message: str + message: Dict - - class SurveyAgentResponse(BaseModel): status: str result: Dict \ No newline at end of file diff --git a/api/routes/__pycache__/chat.cpython-311.pyc b/api/routes/__pycache__/chat.cpython-311.pyc index 9200bca67fad3e4cd45b9be462f225ea7e9df8db..0c3ae4beff8da0833d0b703682ccf46b26d61f97 100644 GIT binary patch delta 616 zcmYL`O=uHA6o6-TGrP;S(>DK+#41T`+LVY14U{PUpP1&#OLleA8>LuBuutv%x$yXWOvh3c1gc)Rf08H32i&N*&Ks`KtWMqOqc? z@E^VJ3cuC$f(*uk@-JJs*$YMv5^oZR*?0NV;9NI2*Eag0*xTIUPFr5H-PoU7}yZBKSC8Xd=6pPSi%Q@qt@tVQ(Xd zBeKf^3DPsyh1Tsw}ffel#4>zO;}P)997=%=w2 zW7k+AT`CpY4HYkD7MUVq^@&Fu$7GMxUhxHTOPC5StF#UA;+iK;NETV* zo^*;2(lbn12lQcag#m!XOBG*I=JD#5FOkP7h+>*1I;S9LgUfFL?{E&ZAQ}@3G(8kh zEta@Qo+mFyuQJc=V~zH)Bl_0b)9mPN#=9>Ijd)trX(9C diff --git a/api/routes/chat.py b/api/routes/chat.py index b5859cf..cb43181 100644 --- a/api/routes/chat.py +++ b/api/routes/chat.py @@ -25,13 +25,12 @@ async def chat_ai( # Initialize DroneBot with history bot = DroneBot(history=history, use_openai_as_fallback=True) - - # Get response result = bot.chat(request.query) - + message = json.loads(result["final_message"]) + print(result) return ChatResponse( status="success", - message=result["final_message"] + message=message ) except Exception as e: raise HTTPException( diff --git a/src/prompts/templates/__pycache__/chat_templates.cpython-311.pyc b/src/prompts/templates/__pycache__/chat_templates.cpython-311.pyc index 6d59944f586da661f8b10d024fc3b44e3138306c..37117f73e47e16bdd1e691b22e52b0aa434d9e27 100644 GIT binary patch delta 227 zcmbPJyP=M6IWI340}$Ay&&nvbo5<(KcyD8@uWh{o5LhX=_$&DNhbTC@xF|UKMTU5I z`nfB(`UJZwg!n6jc(^Kf1^fFcxcLY9I)>;d`1|=pVv45ZE9B*uC?uw&C?q5#WEJP< zDdZP{m>?=Kud*Z~GcR2sHK#aLAt^O2zbF+fE|OM~TBJ~tk*WaJ0Tj ajfdp}L+NB&J3Ga4Hbx*DJ6Xg6^c4W)tU|K@ delta 78 zcmdl`H>Z|wIWI340}%Y#-=6W dict: + """ + Run the drone environmental & safety assessment agent. + + Args: + booking_form (str): Structured booking form input + + Returns: + dict: AI-generated structured output + """ + logger.info("Starting DroneAssessmentAgent run...") + + try: + # Step 1: Fetch weather data + weather_data = await self.weather_extractor.extract_booking_weather_data(booking_form) + booking_form_text = json.dumps(booking_form) + + # Step 2: Generate prompt + prompt = flight_prompt(booking_form_text, weather_data) + messages = [ + SystemMessage(content=prompt), + HumanMessage(content=booking_form_text) + ] + + # Step 3: Invoke LLM + logger.debug("Sending prompt to LLM...") + response = self.llm.invoke(messages) + + if hasattr(response, "content"): + response_text = response.content.strip() + logger.info("Received LLM response.") + logger.debug(f"LLM Raw Output (first 300 chars):\n{response_text[:300]}") + + # Extract JSON block + start_idx = response_text.find('{') + end_idx = response_text.rfind('}') + 1 + + if start_idx == -1 or end_idx == -1: + raise ValueError("No JSON object found in output") + + json_str = response_text[start_idx:end_idx] + return json.loads(json_str) + + else: + raise ValueError("LLM returned no usable content") + + except Exception as e: + logger.exception("Error in DroneAssessmentAgent") + return { + "error": str(e), + "raw_response": response.content if 'response' in locals() and hasattr(response, "content") else None + } + + +async def main(): + """Async main function to test the drone assessment agent.""" + from test2 import booking_form_input + + logger.info("Launching DroneAssessmentAgent from main()...") + agent = DroneAssessmentAgent() + result = await agent.run(booking_form_input) + + logger.info("Drone assessment completed.") + logger.debug("Final structured output:") + logger.debug(json.dumps(result, indent=2)) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..9181558 --- /dev/null +++ b/test2.py @@ -0,0 +1,32 @@ +booking_form_input = { + "job_id": "1043", + "job_overview": { + "job_number": "Job #1043", + "site_name": "Hightower Solar Farm", + "assigned_date": "January 23, 2025", + "status": "Scheduled" + }, + "site_information": { + "site_name": "Hightower Solar Farm", + "region": "North England", + "full_address": "Grange Lane, Manchester M34 7TF", + "gps_coordinates": { + "latitude": "53.4408° N", + "longitude": "2.2426° W" + } + }, + "timing": { + "start_time": "09:00 AM", + "end_time": "10:30 AM", + "survey_duration": "30–45 mins", + "buffer_time": "45 mins" + }, + "form": { + "asset_type": "Solar Farm", + "system_size": "5.2 MW capacity, approximately 16,000 panels across 12 hectares", + "survey_purpose": "Insurance assessment", + "assigned_engineer": "David Wilson - 0161-555-0876", + "contact_person": "Sarah Thompson", + "contact_phone": "0161-555-0234" + } + } \ No newline at end of file