gpt服务
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

há 10 meses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #! /usr/bin/python
  2. import tornado.web
  3. import tornado.ioloop
  4. import tornado.httpserver
  5. import tornado.options
  6. import os
  7. import datetime
  8. import openai
  9. import asyncio
  10. from tornado.web import RequestHandler
  11. from tornado.options import define,options
  12. from tornado.websocket import WebSocketHandler
  13. import openai_async
  14. import config
  15. import json
  16. import traceback
  17. import aiohttp
  18. def clearBaiChuanContent(content):
  19. for ck in config.agentBaiChuanFilterMoel['filterKeys']:
  20. content = content.replace(ck,'xx')
  21. return content
  22. async def talkBaiChuanWithStream(self,message):
  23. print("{} says {}".format(self.request.remote_ip,message))
  24. #url = "https://api.baichuan-ai.com/v1/chat/completions"
  25. #api_key = config.agentServer['agentBaiChuanApiKey']
  26. url = "https://api.baichuan-ai.com/v1/chat/completions"
  27. api_key = "9f7d5847897f8090195cdc2c3249d0a7"
  28. try:
  29. _msg = json.loads(message)
  30. try:
  31. if 'model' in _msg :
  32. _baichuanModel = _msg['model']
  33. _real_msg = _msg['messages']
  34. else:
  35. _baichuanModel = 'Baichuan2'
  36. _real_msg = _msg
  37. except Exception as gmex:
  38. _baichuanModel = 'Baichuan2'
  39. _real_msg = _msg
  40. _data = {
  41. "model": _baichuanModel,
  42. "messages": _real_msg,
  43. "stream": True
  44. }
  45. #json_data = json.dumps(_data)
  46. headers = {
  47. "Content-Type": "application/json",
  48. "Authorization": "Bearer " + api_key
  49. }
  50. #http_client = tornado.httpclient.AsyncHTTPClient()
  51. async with aiohttp.ClientSession() as http_client:
  52. async with http_client.post(url,headers=headers,json=_data) as resp:
  53. isDone=False
  54. while True:
  55. chunk = await resp.content.read(2048)
  56. if not chunk:
  57. break
  58. chunk_str = chunk.decode()
  59. #print('chunk_str {}',chunk_str)
  60. con_list = chunk_str.strip('\n').split('data:')
  61. for con in con_list:
  62. con = con.strip()
  63. if con == "":
  64. continue
  65. elif con == '[DONE]':
  66. isDone=True
  67. break
  68. else:
  69. con_dict = json.loads(con)
  70. for con_cho in con_dict['choices']:
  71. self.my_replay_content=self.my_replay_content+con_cho['delta']['content']
  72. #print('resp',resp)
  73. #print("1111111111111111111")
  74. print("response {}",self.my_replay_content)
  75. if config.agentBaiChuanFilterMoel['isOpen']:
  76. if resp is not None:
  77. cContent = clearBaiChuanContent(self.my_replay_content)
  78. self.my_replay_content=''
  79. if isDone:
  80. cContent = cContent+"___talk_end___"
  81. await self.write_message(cContent)
  82. #await self.write_message(currentLineContents)
  83. else:
  84. if role_conent is not None:
  85. print('')
  86. else:
  87. self.my_replay_content=''
  88. await self.write_message("___talk_end___")
  89. else:
  90. if resp is not None:
  91. _cont = self.my_replay_content
  92. if isDone:
  93. _cont = _cont+"___talk_end___"
  94. self.my_replay_content=''
  95. await self.write_message(_cont)
  96. else:
  97. self.my_replay_content=''
  98. await self.write_message("___talk_end___")
  99. else:
  100. self.my_replay_content=''
  101. await self.write_message("___talk_end___")
  102. except Exception as es:
  103. print('onMessage error',es)
  104. self.close()