AiBot.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, metrics
  26. #from prometheus_client import Summary
  27. logger=get_logger("log")
  28. app = FastAPI()
  29. # 服务监控,qps,latent, cpu利用率
  30. Instrumentator().instrument(app).expose(app)
  31. @app.post("/botservice")
  32. @timetic
  33. def botservice(reqbot:reqRobot):
  34. bid, uid, code, asr = reqbot.taskId, reqbot.userId, reqbot.nodeId, reqbot.asrText
  35. session_id, record_id = reqbot.sessionId, reqbot.recordId
  36. logger.info("智能客服:botId: {},session:{}, uid: {}, nodeId: {}, ASR: {}".format(bid, session_id, uid, code, asr))
  37. scene = get_status_cache(uid, bid, "status", session_id)
  38. #scene.case.code="1.00"
  39. if scene is None:
  40. scene = Dialog()
  41. try:
  42. cur_code = scene.get_current_code(bid)
  43. logger.info(f"智能客服:uid:{uid},sessionId:{session_id},code:{code}, cur_code:{cur_code}")
  44. if code == cur_code:
  45. option = scene.mapping_semantics(code, asr, bid, uid, session_id)
  46. logger.info(f"智能客服:uid:{uid},sessionId:{session_id},code:{code},opt:{option}")
  47. if not option:
  48. return ChatResponse(code=Error.error_intent.value, message=INTENT_ERROR).to_json_string()
  49. msg = scene.dialogue(Msg(code=code,
  50. option=option,
  51. asr=asr,
  52. session=session_id,
  53. record=record_id,
  54. ),bid, uid,session_id)
  55. if msg.code == END:
  56. with_status_cache(uid, bid, 'result', session_id, scene)
  57. del_cache(uid, bid, 'status', session_id)
  58. insert_log(bid, uid, session_id, scene)
  59. return ChatResponse(code=200, message="success").to_json_string()
  60. else:
  61. with_status_cache(uid, bid, 'status', session_id, scene)
  62. insert_log(bid, uid, session_id, scene)
  63. if msg.action in [HANGUP, TRANSFER]:
  64. del_cache(uid, bid, 'status', session_id)
  65. with_status_cache(uid, bid, 'result', session_id, scene)
  66. return ChatResponse().from_msg(msg).to_json_string()
  67. else:
  68. msg = scene.get_current_content(bid, uid)
  69. if code == "start" or code is None:
  70. scene.case.code = cur_code
  71. with_status_cache(uid, bid, 'status', session_id, scene)
  72. insert_log(bid, uid, session_id, scene)
  73. return ChatResponse().from_msg(msg).to_json_string()
  74. except Exception as e:
  75. logger.error(e)
  76. #del_cache(uid, bid, 'status', session_id)
  77. return ChatResponse(code=Error.error.value, message="internal error").to_json_string()
  78. if __name__ == '__main__':
  79. uvicorn.run("AiBot:app", host='0.0.0.0', port=40072,workers=4)