create_images.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from __future__ import annotations
  2. import re
  3. import asyncio
  4. from .. import debug
  5. from ..typing import CreateResult, Messages
  6. from ..base_provider import BaseProvider, ProviderType
  7. system_message = """
  8. You can generate custom images with the DALL-E 3 image generator.
  9. To generate an image with a prompt, do this:
  10. <img data-prompt=\"keywords for the image\">
  11. Don't use images with data uri. It is important to use a prompt instead.
  12. <img data-prompt=\"image caption\">
  13. """
  14. class CreateImagesProvider(BaseProvider):
  15. """
  16. Provider class for creating images based on text prompts.
  17. This provider handles image creation requests embedded within message content,
  18. using provided image creation functions.
  19. Attributes:
  20. provider (ProviderType): The underlying provider to handle non-image related tasks.
  21. create_images (callable): A function to create images synchronously.
  22. create_images_async (callable): A function to create images asynchronously.
  23. system_message (str): A message that explains the image creation capability.
  24. include_placeholder (bool): Flag to determine whether to include the image placeholder in the output.
  25. __name__ (str): Name of the provider.
  26. url (str): URL of the provider.
  27. working (bool): Indicates if the provider is operational.
  28. supports_stream (bool): Indicates if the provider supports streaming.
  29. """
  30. def __init__(
  31. self,
  32. provider: ProviderType,
  33. create_images: callable,
  34. create_async: callable,
  35. system_message: str = system_message,
  36. include_placeholder: bool = True
  37. ) -> None:
  38. """
  39. Initializes the CreateImagesProvider.
  40. Args:
  41. provider (ProviderType): The underlying provider.
  42. create_images (callable): Function to create images synchronously.
  43. create_async (callable): Function to create images asynchronously.
  44. system_message (str, optional): System message to be prefixed to messages. Defaults to a predefined message.
  45. include_placeholder (bool, optional): Whether to include image placeholders in the output. Defaults to True.
  46. """
  47. self.provider = provider
  48. self.create_images = create_images
  49. self.create_images_async = create_async
  50. self.system_message = system_message
  51. self.include_placeholder = include_placeholder
  52. self.__name__ = provider.__name__
  53. self.url = provider.url
  54. self.working = provider.working
  55. self.supports_stream = provider.supports_stream
  56. def create_completion(
  57. self,
  58. model: str,
  59. messages: Messages,
  60. stream: bool = False,
  61. **kwargs
  62. ) -> CreateResult:
  63. """
  64. Creates a completion result, processing any image creation prompts found within the messages.
  65. Args:
  66. model (str): The model to use for creation.
  67. messages (Messages): The messages to process, which may contain image prompts.
  68. stream (bool, optional): Indicates whether to stream the results. Defaults to False.
  69. **kwargs: Additional keywordarguments for the provider.
  70. Yields:
  71. CreateResult: Yields chunks of the processed messages, including image data if applicable.
  72. Note:
  73. This method processes messages to detect image creation prompts. When such a prompt is found,
  74. it calls the synchronous image creation function and includes the resulting image in the output.
  75. """
  76. messages.insert(0, {"role": "system", "content": self.system_message})
  77. buffer = ""
  78. for chunk in self.provider.create_completion(model, messages, stream, **kwargs):
  79. if buffer or "<" in chunk:
  80. buffer += chunk
  81. if ">" in buffer:
  82. match = re.search(r'<img data-prompt="(.*?)">', buffer)
  83. if match:
  84. placeholder, prompt = match.group(0), match.group(1)
  85. start, append = buffer.split(placeholder, 1)
  86. if start:
  87. yield start
  88. if self.include_placeholder:
  89. yield placeholder
  90. if debug.logging:
  91. print(f"Create images with prompt: {prompt}")
  92. yield from self.create_images(prompt)
  93. if append:
  94. yield append
  95. else:
  96. yield buffer
  97. buffer = ""
  98. else:
  99. yield chunk
  100. async def create_async(
  101. self,
  102. model: str,
  103. messages: Messages,
  104. **kwargs
  105. ) -> str:
  106. """
  107. Asynchronously creates a response, processing any image creation prompts found within the messages.
  108. Args:
  109. model (str): The model to use for creation.
  110. messages (Messages): The messages to process, which may contain image prompts.
  111. **kwargs: Additional keyword arguments for the provider.
  112. Returns:
  113. str: The processed response string, including asynchronously generated image data if applicable.
  114. Note:
  115. This method processes messages to detect image creation prompts. When such a prompt is found,
  116. it calls the asynchronous image creation function and includes the resulting image in the output.
  117. """
  118. messages.insert(0, {"role": "system", "content": self.system_message})
  119. response = await self.provider.create_async(model, messages, **kwargs)
  120. matches = re.findall(r'(<img data-prompt="(.*?)">)', response)
  121. results = []
  122. placeholders = []
  123. for placeholder, prompt in matches:
  124. if placeholder not in placeholders:
  125. if debug.logging:
  126. print(f"Create images with prompt: {prompt}")
  127. results.append(self.create_images_async(prompt))
  128. placeholders.append(placeholder)
  129. results = await asyncio.gather(*results)
  130. for idx, result in enumerate(results):
  131. placeholder = placeholder[idx]
  132. if self.include_placeholder:
  133. result = placeholder + result
  134. response = response.replace(placeholder, result)
  135. return response