AiBot.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Time : 2024/10/18 10:04
  5. @File : AiBot.py
  6. @Desc :
  7. """
  8. import sys
  9. sys.path.append("..")
  10. from config import (
  11. get_logger,
  12. INTENT_ERROR,
  13. END,
  14. HANGUP,
  15. TRANSFER
  16. )
  17. from fastapi import FastAPI
  18. import uvicorn
  19. from interface import reqRobot, ChatResponse
  20. from database import *
  21. from scene import Dialog,Msg
  22. from entity import Error
  23. import traceback
  24. from util import insert_log, timetic
  25. from prometheus_fastapi_instrumentator import Instrumentator
  26. import time
  27. logger=get_logger("log")
  28. app = FastAPI()
  29. # 服务监控,qps,latent, cpu利用率
  30. Instrumentator().instrument(app).expose(app)
  31. from util.registry import AI_BOT_REQUEST_LATENCY, AI_BOT_REQUEST_COUNT
  32. @app.post("/botservice")
  33. @timetic
  34. def botservice(reqbot:reqRobot):
  35. start_time = time.time()
  36. bid, uid, code, asr = reqbot.taskId, reqbot.userId, reqbot.nodeId, reqbot.asrText
  37. session_id, record_id = reqbot.sessionId, reqbot.recordId
  38. logger.info("智能客服:botId: {},session:{}, uid: {}, nodeId: {}, ASR: {}".format(bid, session_id, uid, code, asr))
  39. scene = get_status_cache(uid, bid, "status", session_id)
  40. #scene.case.code="1.00"
  41. if scene is None:
  42. scene = Dialog()
  43. try:
  44. cur_code = scene.get_current_code(bid)
  45. logger.info(f"智能客服:uid:{uid},sessionId:{session_id},code:{code}, cur_code:{cur_code}")
  46. if code == cur_code:
  47. option = scene.mapping_semantics(code, asr, bid, uid, session_id)
  48. logger.info(f"智能客服:uid:{uid},sessionId:{session_id},code:{code},opt:{option}")
  49. if not option:
  50. return ChatResponse(code=Error.error_intent.value, message=INTENT_ERROR).to_json_string()
  51. msg = scene.dialogue(Msg(code=code,
  52. option=option,
  53. asr=asr,
  54. session=session_id,
  55. record=record_id,
  56. ),bid, uid,session_id)
  57. if msg.code == END:
  58. with_status_cache(uid, bid, 'result', session_id, scene)
  59. del_cache(uid, bid, 'status', session_id)
  60. insert_log(bid, uid, session_id, scene)
  61. return ChatResponse(code=200, message="success").to_json_string()
  62. else:
  63. with_status_cache(uid, bid, 'status', session_id, scene)
  64. insert_log(bid, uid, session_id, scene)
  65. if msg.action in [HANGUP, TRANSFER]:
  66. del_cache(uid, bid, 'status', session_id)
  67. with_status_cache(uid, bid, 'result', session_id, scene)
  68. return ChatResponse().from_msg(msg).to_json_string()
  69. else:
  70. msg = scene.get_current_content(bid, uid)
  71. if code == "start" or code is None:
  72. scene.case.code = cur_code
  73. with_status_cache(uid, bid, 'status', session_id, scene)
  74. insert_log(bid, uid, session_id, scene)
  75. return ChatResponse().from_msg(msg).to_json_string()
  76. except Exception as e:
  77. logger.error(e)
  78. #del_cache(uid, bid, 'status', session_id)
  79. return ChatResponse(code=Error.error.value, message="internal error").to_json_string()
  80. finally:
  81. latency = (time.time() - start_time)
  82. print(f"Latency for {bid}: {latency}") # 日志记录
  83. AI_BOT_REQUEST_COUNT.inc()
  84. AI_BOT_REQUEST_LATENCY.labels(bid=bid).observe(latency)
  85. if __name__ == '__main__':
  86. uvicorn.run("AiBot:app", host='0.0.0.0', port=40072,workers=4)