Skip to content

Instantly share code, notes, and snippets.

@KyMidd
Created December 27, 2024 20:05
Show Gist options
  • Select an option

  • Save KyMidd/a7f38072d87942c5f79ba1bee9d2e798 to your computer and use it in GitHub Desktop.

Select an option

Save KyMidd/a7f38072d87942c5f79ba1bee9d2e798 to your computer and use it in GitHub Desktop.
# Function to build the content of a conversation
def build_conversation_content(payload, token):
....
# If the payload contains files, iterate through them
if "files" in payload:
# Append the payload files to the content array
for file in payload["files"]:
# Check the mime type of the file is a supported file type
# Commenting out the PDF check until the PDF beta is enabled on bedrock
# if file["mimetype"] in ['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']:
if file["mimetype"] in [
"image/png",
"image/jpeg",
"image/gif",
"image/webp",
]:
# File is a supported type
file_url = file["url_private_download"]
# Fetch the file and continue
file_object = requests.get(
file_url, headers={"Authorization": "Bearer " + token}
)
# Encode the image with base64
encoded_file = base64.b64encode(file_object.content).decode("utf-8")
# Identify the mime type of the file, some require different file types when sending to the model
if file["mimetype"] in [
"image/png",
"image/jpeg",
"image/gif",
"image/webp",
]:
file_type = "image"
else:
file_type = "document"
# Append the file to the content array
content.append(
{
"type": file_type,
"source": {
"type": "base64",
"media_type": file["mimetype"],
"data": encoded_file,
},
}
)
# If the mime type is not supported, set unsupported_file_type_found to True
else:
print(f"Unsupported file type found: {file['mimetype']}")
unsupported_file_type_found = True
continue
# Return
return content, unsupported_file_type_found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment