langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
52.13k stars 7.58k forks source link

How to input base64 image #10889

Closed mcgrimm closed 2 hours ago

mcgrimm commented 2 hours ago

Self Checks

1. Is this request related to a challenge you're experiencing? Tell me about your story.

Hello , i have an ERP system which want to send image to DIFY , but the image should in format of base64, currently i can't find any documentation about the base64 image input, i would like to run API to pass the base64 file to workflow. can anyone have any suggestion or hints? i can't find any documentation in the API introduction so far. thank you very much. Dify is the great tool , i hope i can use it in the industrial areas, the the integration ability should be very important. thanks.

2. Additional context or comments

No response

3. Can you help us with this feature?

dosubot[bot] commented 2 hours ago

Hey @mcgrimm! 👋 I'm here to help you with bug fixes, answer questions, and even guide you on becoming a contributor. If you need anything, just give me a shout!

I found some relevant information that might help you with handling base64 images in Dify. The Dify API documentation provides methods for decoding base64 encoded images, which include checking if the encoded string is plain base64 without a MIME type prefix, and extracting MIME type and image data from a base64 encoded string with a MIME type prefix. Here's a snippet of the relevant code:

@staticmethod
def _decode_image(base64_image: str) -> tuple[str, bytes]:
    if DallE3Tool._is_plain_base64(base64_image):
        return "image/png", base64.b64decode(base64_image)
    else:
        return DallE3Tool._extract_mime_and_data(base64_image)

@staticmethod
def _is_plain_base64(encoded_str: str) -> bool:
    return not encoded_str.startswith("data:image")

@staticmethod
def _extract_mime_and_data(encoded_str: str) -> tuple[str, bytes]:
    mime_type = encoded_str.split(";")[0].split(":")[1]
    image_data_base64 = encoded_str.split(",")[1]
    decoded_data = base64.b64decode(image_data_base64)
    return mime_type, decoded_data

These methods can help you convert base64 images to a format suitable for API upload [1].

Additionally, there are similar discussions and issues that might provide more context or solutions:

  1. Beginner Question: How to send Base64 encoded JPG from Make to Dify and process it with LLM? - This is an unsolved discussion.
  2. I can't upload the image on dify workflow via api - Another unsolved discussion.

These might offer additional insights or approaches to your problem.

To continue talking to Dosu, mention @dosu.