gpt壳项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

91 lines
3.3 KiB

  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 re
  16. openai.api_key = config.agentServer['agentApiKey'];
  17. def clearContent(content):
  18. for ck in config.agentFilterMoel['filterKeys']:
  19. content = content.replace(ck,'SmartChat')
  20. return content
  21. async def talkGPTWithStream(self,message):
  22. print("{} says {}".format(self.request.remote_ip,message))
  23. try:
  24. #currentLineContents=''
  25. #currentIsSend=False
  26. async for chunk in await openai.ChatCompletion.acreate(
  27. model='gpt-3.5-turbo',
  28. #model='gpt-4-0314',
  29. messages=[{'role': 'user', 'content': message}],
  30. temperature=0.8,
  31. #max_tokens=400,
  32. stream=True # this time, we set stream=True
  33. ):
  34. content = chunk["choices"][0].get("delta", {}).get("content")
  35. role_conent = chunk["choices"][0].get("delta", {}).get("role")
  36. #print("response {}",content)
  37. if config.agentFilterMoel['isOpen']:
  38. if content is not None:
  39. ###如果是结尾
  40. if content in config.agentFilterMoel['oneMessageSplit']:
  41. self.my_replay_content = self.my_replay_content+content
  42. self.currentIsSend=True
  43. else:
  44. self.my_replay_content = self.my_replay_content + content
  45. self.currentIsSend=False
  46. if self.currentIsSend:
  47. ####过滤关键字
  48. cContent = clearContent(self.my_replay_content)
  49. self.my_replay_content='';
  50. await self.write_message(cContent)
  51. #await self.write_message(currentLineContents)
  52. else:
  53. if role_conent is not None:
  54. print('')
  55. else:
  56. await self.write_message("___talk_end___")
  57. else:
  58. if content is not None:
  59. await self.write_message(content)
  60. else:
  61. if role_conent is not None:
  62. print('')
  63. else:
  64. await self.write_message("___talk_end___")
  65. except Exception as es:
  66. print('onMessage error',es)
  67. self.close()
  68. async def talkGPT(self,message):
  69. print("{} says {}".format(self.request.remote_ip,message))
  70. response = await openai.Completion.acreate(
  71. model="text-davinci-003",
  72. prompt= message,
  73. temperature=0,
  74. max_tokens=4000,
  75. top_p=1,
  76. frequency_penalty=0.5,
  77. presence_penalty=0
  78. )
  79. #print(f"response:{response}")
  80. response_text = response['choices'][0]['text']
  81. await self.write_message(response_text+"___talk_end___")