lib_nlp_grpc.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import requests
  2. import json
  3. import config
  4. from logger_config import logger
  5. APPID = config.config["SCKJ_APPID"]
  6. API_KEY = config.config["SCKJ_APIKey"]
  7. NLP_URL = config.config["SCKJ_NLP_URL"]
  8. def create_dialog():
  9. # 定义要发送的数据
  10. data = {"code": APPID, "appKey": API_KEY}
  11. # 将数据转换为 JSON 格式
  12. json_data = json.dumps(data)
  13. # 设置请求头部
  14. headers = {
  15. "Content-Type": "application/json"
  16. }
  17. # 发送 POST 请求
  18. response = requests.post(NLP_URL + "/create", data=json_data, headers=headers)
  19. # 检查响应状态码
  20. if response.status_code == 200:
  21. # 打印响应内容
  22. logger.info(f"创建对话成功:{response.json()}")
  23. return {"did": response.json()['data']['id'], "hotwords": response.json()['data']['hotwords']}
  24. else:
  25. logger.info(f"创建对话失败,Error http code: {response.status_code}")
  26. return {}
  27. def get_nlp_result(did, text):
  28. # 定义要发送的数据
  29. data = {"id": did, "input": text, "timeout": False}
  30. # 将数据转换为 JSON 格式
  31. json_data = json.dumps(data)
  32. # 设置请求头部
  33. headers = {
  34. "Content-Type": "application/json"
  35. }
  36. # 发送 POST 请求
  37. response = requests.post(NLP_URL + "/input", data=json_data, headers=headers)
  38. # 检查响应状态码
  39. if response.status_code == 200:
  40. # 打印响应内容
  41. logger.info(f"语义理解结果:{response.text}")
  42. return response.text
  43. else:
  44. logger.info(f"语义理解失败,Error http code: {response.status_code}")
  45. return ''
  46. def end_dialog(did):
  47. # 定义要发送的数据
  48. data = {"id": did}
  49. # 将数据转换为 JSON 格式
  50. json_data = json.dumps(data)
  51. # 设置请求头部
  52. headers = {
  53. "Content-Type": "application/json"
  54. }
  55. # 发送 POST 请求
  56. response = requests.post(NLP_URL + "/end", data=json_data, headers=headers)
  57. # 检查响应状态码
  58. if response.status_code == 200:
  59. # 打印响应内容
  60. print("结束对话结果:", response.json())
  61. return response.text
  62. else:
  63. print("结束对话失败,Error http code:", response.status_code)
  64. return ''