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.

open_ai_bot.py 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # encoding:utf-8
  2. from bot.bot import Bot
  3. from config import conf
  4. from common.log import logger
  5. import openai
  6. # OpenAI对话模型API (可用)
  7. class OpenAIBot(Bot):
  8. def __init__(self):
  9. openai.api_key = conf().get('open_ai_api_key')
  10. def reply(self, query, context=None):
  11. # auto append question mark
  12. query = self.append_question_mark(query)
  13. # acquire reply content
  14. if not context or not context.get('type') or context.get('type') == 'TEXT':
  15. return self.reply_text(query)
  16. elif context.get('type', None) == 'IMAGE_CREATE':
  17. return self.create_img(query)
  18. def reply_text(self, query):
  19. logger.info("[OPEN_AI] query={}".format(query))
  20. try:
  21. response = openai.Completion.create(
  22. model="text-davinci-003", # 对话模型的名称
  23. prompt=query,
  24. temperature=0.9, # 值在[0,1]之间,越大表示回复越具有不确定性
  25. max_tokens=1200, # 回复最大的字符数
  26. top_p=1,
  27. frequency_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容
  28. presence_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容
  29. stop=["#"]
  30. )
  31. res_content = response.choices[0]["text"].strip()
  32. except Exception as e:
  33. logger.exception(e)
  34. return None
  35. logger.info("[OPEN_AI] reply={}".format(res_content))
  36. return res_content
  37. def create_img(self, query):
  38. try:
  39. logger.info("[OPEN_AI] image_query={}".format(query))
  40. response = openai.Image.create(
  41. prompt=query, #图片描述
  42. n=1, #每次生成图片的数量
  43. size="256x256" #图片大小,可选有 256x256, 512x512, 1024x1024
  44. )
  45. image_url = response['data'][0]['url']
  46. logger.info("[OPEN_AI] image_url={}".format(image_url))
  47. except Exception as e:
  48. logger.exception(e)
  49. return None
  50. return image_url
  51. def edit_img(self, query, src_img):
  52. try:
  53. response = openai.Image.create_edit(
  54. image=open(src_img, 'rb'),
  55. mask=open('cat-mask.png', 'rb'),
  56. prompt=query,
  57. n=1,
  58. size='512x512'
  59. )
  60. image_url = response['data'][0]['url']
  61. logger.info("[OPEN_AI] image_url={}".format(image_url))
  62. except Exception as e:
  63. logger.exception(e)
  64. return None
  65. return image_url
  66. def migration_img(self, query, src_img):
  67. try:
  68. response = openai.Image.create_variation(
  69. image=open(src_img, 'rb'),
  70. n=1,
  71. size="512x512"
  72. )
  73. image_url = response['data'][0]['url']
  74. logger.info("[OPEN_AI] image_url={}".format(image_url))
  75. except Exception as e:
  76. logger.exception(e)
  77. return None
  78. return image_url
  79. def append_question_mark(self, query):
  80. end_symbols = [".", "。", "?", "?", "!", "!"]
  81. for symbol in end_symbols:
  82. if query.endswith(symbol):
  83. return query
  84. return query + "?"