Use the latest format

This commit is contained in:
oobabooga 2025-06-05 11:42:12 -07:00
parent 7f7909be54
commit 0783f5c891
3 changed files with 24 additions and 11 deletions

View file

@ -150,7 +150,7 @@ def convert_history(history):
# Process multimodal content # Process multimodal content
processed_content, images = process_multimodal_content(content) processed_content, images = process_multimodal_content(content)
if images: if images:
image_refs = "".join(f"[img-{img['image_id']}]" for img in images) image_refs = "".join("<__media__>" for img in images)
processed_content = f"{processed_content} {image_refs}" processed_content = f"{processed_content} {image_refs}"
user_input = processed_content user_input = processed_content

View file

@ -225,7 +225,7 @@ def generate_chat_prompt(user_input, state, **kwargs):
for attachment in metadata[user_key]["attachments"]: for attachment in metadata[user_key]["attachments"]:
if attachment.get("type") == "image": if attachment.get("type") == "image":
# Add image reference for multimodal models # Add image reference for multimodal models
image_refs += f"[img-{attachment['image_id']}]" image_refs += "<__media__>"
else: else:
# Handle text/PDF attachments as before # Handle text/PDF attachments as before
filename = attachment.get("name", "file") filename = attachment.get("name", "file")
@ -260,7 +260,7 @@ def generate_chat_prompt(user_input, state, **kwargs):
for attachment in metadata[user_key]["attachments"]: for attachment in metadata[user_key]["attachments"]:
if attachment.get("type") == "image": if attachment.get("type") == "image":
image_refs += f"[img-{attachment['image_id']}]" image_refs += "<__media__>"
else: else:
filename = attachment.get("name", "file") filename = attachment.get("name", "file")
content = attachment.get("content", "") content = attachment.get("content", "")
@ -517,17 +517,30 @@ def add_message_attachment(history, row_idx, file_path, is_user=True):
with open(path, 'rb') as f: with open(path, 'rb') as f:
image_data = base64.b64encode(f.read()).decode('utf-8') image_data = base64.b64encode(f.read()).decode('utf-8')
# Determine MIME type from extension
mime_type_map = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.webp': 'image/webp',
'.bmp': 'image/bmp',
'.gif': 'image/gif'
}
mime_type = mime_type_map.get(file_extension, 'image/jpeg')
# Format as data URL
data_url = f"data:{mime_type};base64,{image_data}"
# Generate unique image ID # Generate unique image ID
image_id = len([att for att in history['metadata'][key]["attachments"] if att.get("type") == "image"]) + 1 image_id = len([att for att in history['metadata'][key]["attachments"] if att.get("type") == "image"]) + 1
attachment = { attachment = {
"name": filename, "name": filename,
"type": "image", "type": "image",
"image_data": image_data, "image_data": data_url,
"image_id": image_id, "image_id": image_id,
"file_path": str(path) # For UI preview "file_path": str(path) # For UI preview
} }
elif file_extension == '.pdf': elif file_extension == '.pdf':
# Process PDF file # Process PDF file
content = extract_pdf_text(path) content = extract_pdf_text(path)

View file

@ -123,15 +123,15 @@ class LlamaServer:
# Add image data if present # Add image data if present
if 'image_attachments' in state: if 'image_attachments' in state:
image_data = [] medias = []
for attachment in state['image_attachments']: for attachment in state['image_attachments']:
image_data.append({ medias.append({
"data": attachment['image_data'], "type": "image",
"id": attachment['image_id'] "data": attachment['image_data']
}) })
if image_data: if medias:
payload["image_data"] = image_data payload["medias"] = medias
return payload return payload