tool_text_to_speech.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from tools.toolbase import *
  2. from openai_wrapper import OpenAIWrapper
  3. class Tool_text_to_speech(ToolBase):
  4. """ 工具: text_to_speech 文字转语音"""
  5. def __init__(self, config:Config, oaiw:OpenAIWrapper) -> None:
  6. """初始化"""
  7. super().__init__(config)
  8. self.oaiw = oaiw
  9. @property
  10. def name(self) -> str:
  11. return "text_to_speech"
  12. @property
  13. def desc(self) -> str:
  14. return "用文字生成语音"
  15. @property
  16. def function_json(self) -> dict:
  17. FUNCTION_TTS = {
  18. "name": "text_to_speech",
  19. "parameters": {
  20. "type": "object",
  21. "properties": {
  22. "text": {
  23. "type": "string",
  24. "description": "User provided text for generating the speech audio. 用户提供的文本,用于生成语音"
  25. }
  26. },
  27. "required": ["text"]
  28. },
  29. "description": "Generate audio speech according to user provided text, when user wants you to speak, read out loud, or generate audio speech."
  30. }
  31. return FUNCTION_TTS
  32. def process_toolcall(self, arguments:str, callback_msg:MSG_CALLBACK) -> str:
  33. """ 调用openai TTS"""
  34. args = json.loads(arguments)
  35. text = args['text']
  36. callback_msg(ChatMsg(ContentType.text, f"正在为您生成语音"))
  37. # common.logger().info("正在生成语音:%s", text)
  38. speech_file = self.oaiw.tts(text)
  39. callback_msg(ChatMsg(ContentType.file, speech_file))
  40. note = "成功生成语音并已发送给用户"
  41. return note