跳过内容

文本转图像

TextToImageToolSpec #

基础类: BaseToolSpec

文本到图像工具规范。

源代码位于 llama-index-integrations/tools/llama-index-tools-text-to-image/llama_index/tools/text_to_image/base.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class TextToImageToolSpec(BaseToolSpec):
    """Text to Image tool spec."""

    spec_functions = ["generate_images", "show_images", "generate_image_variation"]

    def __init__(self, api_key: Optional[str] = None) -> None:
        if api_key:
            openai.api_key = api_key

    def generate_images(
        self, prompt: str, n: Optional[int] = 1, size: Optional[str] = "256x256"
    ) -> List[str]:
        """
        Pass a prompt to OpenAIs text to image API to produce an image from the supplied query.

        Args:
            prompt (str): The prompt to generate an image(s) based on
            n (int): The number of images to generate. Defaults to 1.
            size (str): The size of the image(s) to generate. Defaults to 256x256. Other accepted values are 1024x1024 and 512x512

        When handling the urls returned from this function, NEVER strip any parameters or try to modify the url, they are necessary for authorization to view the image

        """
        try:
            response = openai.Image.create(prompt=prompt, n=n, size=size)
            return [image["url"] for image in response["data"]]
        except openai.error.OpenAIError as e:
            return e.error

    def generate_image_variation(
        self, url: str, n: Optional[int] = 1, size: Optional[str] = "256x256"
    ) -> str:
        """
        Accepts the url of an image and uses OpenAIs api to generate a variation of the image.
        This tool can take smaller images and create higher resolution variations, or vice versa.

        When passing a url from "generate_images" ALWAYS pass the url exactly as it was returned from the function, including ALL query parameters
        args:
            url (str): The url of the image to create a variation of
            n (int): The number of images to generate. Defaults to 1.
            size (str): The size of the image(s) to generate. Defaults to 256x256. Other accepted values are 1024x1024 and 512x512
        """
        try:
            response = openai.Image.create_variation(
                image=BytesIO(requests.get(url).content).getvalue(), n=n, size=size
            )
            return [image["url"] for image in response["data"]]
        except openai.error.OpenAIError as e:
            return e.error

    def show_images(self, urls: List[str]):
        """
        Use this function to display image(s) using pyplot and pillow. This works in a jupyter notebook.

        Args:
            urls (str): The url(s) of the image(s) to show

        """
        import matplotlib.pyplot as plt
        from PIL import Image

        for url in urls:
            plt.figure()
            plt.imshow(Image.open(BytesIO(requests.get(url).content)))
        return "images rendered successfully"

generate_images #

generate_images(prompt: str, n: Optional[int] = 1, size: Optional[str] = '256x256') -> List[str]

将提示词传递给 OpenAI 的文本到图像 API,以根据提供的查询生成图像。

参数

名称 类型 描述 默认值
prompt str

用于生成图像的提示词

必需的
n int

要生成的图像数量。默认为 1。

1
size str

要生成的图像尺寸。默认为 256x256。其他可接受的值为 1024x1024 和 512x512。

'256x256'

处理此函数返回的 URL 时,切勿删除或尝试修改 URL 中的任何参数,它们是查看图像的授权必需部分。

源代码位于 llama-index-integrations/tools/llama-index-tools-text-to-image/llama_index/tools/text_to_image/base.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def generate_images(
    self, prompt: str, n: Optional[int] = 1, size: Optional[str] = "256x256"
) -> List[str]:
    """
    Pass a prompt to OpenAIs text to image API to produce an image from the supplied query.

    Args:
        prompt (str): The prompt to generate an image(s) based on
        n (int): The number of images to generate. Defaults to 1.
        size (str): The size of the image(s) to generate. Defaults to 256x256. Other accepted values are 1024x1024 and 512x512

    When handling the urls returned from this function, NEVER strip any parameters or try to modify the url, they are necessary for authorization to view the image

    """
    try:
        response = openai.Image.create(prompt=prompt, n=n, size=size)
        return [image["url"] for image in response["data"]]
    except openai.error.OpenAIError as e:
        return e.error

generate_image_variation #

generate_image_variation(url: str, n: Optional[int] = 1, size: Optional[str] = '256x256') -> str

接受图像 URL,并使用 OpenAI API 生成图像的变体。此工具可以将较小的图像转换为更高分辨率的变体,反之亦然。

从 "generate_images" 函数传递 URL 时,**始终**按照函数返回时的原样传递 URL,包括**所有**查询参数。参数:url (str): 要创建变体的图像 URL n (int): 要生成的图像数量。默认为 1。size (str): 要生成的图像尺寸。默认为 256x256。其他可接受的值为 1024x1024 和 512x512。

源代码位于 llama-index-integrations/tools/llama-index-tools-text-to-image/llama_index/tools/text_to_image/base.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def generate_image_variation(
    self, url: str, n: Optional[int] = 1, size: Optional[str] = "256x256"
) -> str:
    """
    Accepts the url of an image and uses OpenAIs api to generate a variation of the image.
    This tool can take smaller images and create higher resolution variations, or vice versa.

    When passing a url from "generate_images" ALWAYS pass the url exactly as it was returned from the function, including ALL query parameters
    args:
        url (str): The url of the image to create a variation of
        n (int): The number of images to generate. Defaults to 1.
        size (str): The size of the image(s) to generate. Defaults to 256x256. Other accepted values are 1024x1024 and 512x512
    """
    try:
        response = openai.Image.create_variation(
            image=BytesIO(requests.get(url).content).getvalue(), n=n, size=size
        )
        return [image["url"] for image in response["data"]]
    except openai.error.OpenAIError as e:
        return e.error

show_images #

show_images(urls: List[str])

使用此函数通过 pyplot 和 pillow 显示图像。这在 jupyter notebook 中有效。

参数

名称 类型 描述 默认值
urls str

要显示的图像 URL(s)

必需的
源代码位于 llama-index-integrations/tools/llama-index-tools-text-to-image/llama_index/tools/text_to_image/base.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def show_images(self, urls: List[str]):
    """
    Use this function to display image(s) using pyplot and pillow. This works in a jupyter notebook.

    Args:
        urls (str): The url(s) of the image(s) to show

    """
    import matplotlib.pyplot as plt
    from PIL import Image

    for url in urls:
        plt.figure()
        plt.imshow(Image.open(BytesIO(requests.get(url).content)))
    return "images rendered successfully"