Skip to content

[Feature/功能]: AxonHub 目前在处理multipart/form-data格式请求的时候,请求体显示异常;上传图片大小固定为4Mb,没有环境变量方便调整 #1047

@fz007-hhh

Description

@fz007-hhh

Pre-submission Checklist / 提交前检查

  • I have searched the existing issues and this feature has not been requested / 我已搜索现有 issues,此功能尚未被提出
  • I have read the documentation / 我已阅读文档

Problem Statement / 问题描述

在处理 OpenAI 兼容的图像编辑/变体请求(multipart/form-data)时,AxonHub 目前存在两个实际限制:

  1. 对于multipart/form-data传入的请求体,默认执行json.Marshal(body)会出错,导致“请求页面”查看请求体时只能显示invalid text错误;
  2. 单张图像上传大小固定限制为 4MB,没有对应的环境变量方便修改;

Proposed Solution / 期望方案

  1. 希望修改image_inbound.gorequest.go,增加对multipart/form-data格式的处理,在“请求”页面能够正确看到请求体;
  2. 希望将image_inbound.goconst maxImageFileSize变量改为从环境变量中加载值;

Alternatives Considered / 备选方案

我本地修改了代码,主要修改了 internal\server\biz\request.gollm\transformer\openai\image_inbound.go 这两个文件。

  1. 上传图片的默认大小我这里是直接修改了maxImageFileSize 变量:
Image
  1. 对于multipart/form-data格式的请求体问题,我在image_inbound.go中增加了这个函数:
// buildMultipartJSONBody builds a JSON representation of a multipart/form-data request
// suitable for logging. Binary image/mask data is replaced with a human-readable
// metadata string (content-type + size) to avoid storing raw binary in the database.
func buildMultipartJSONBody(fields map[string]string, images []multipartFile, mask *multipartFile) ([]byte, error) {
	body := make(map[string]any, len(fields)+2)

	for k, v := range fields {
		if v != "" {
			body[k] = v
		}
	}

	switch len(images) {
	case 1:
		body["image"] = fmt.Sprintf("[%s, %d bytes]", images[0].ContentType, len(images[0].Data))
	case 0:
		// no image
	default:
		descs := make([]string, len(images))
		for i, img := range images {
			descs[i] = fmt.Sprintf("[%s, %d bytes]", img.ContentType, len(img.Data))
		}
		body["image"] = descs
	}

	if mask != nil {
		body["mask"] = fmt.Sprintf("[%s, %d bytes]", mask.ContentType, len(mask.Data))
	}

	return json.Marshal(body)
}

然后分别在 transformEditRequest方法和transformVariationRequest中添加了如下代码:

// Build JSONBody for logging: replace binary image data with metadata descriptions.
if jsonBody, err := buildMultipartJSONBody(formData.Fields, formData.Images, formData.Mask); err == nil {
	httpReq.JSONBody = jsonBody
}
Image Image
  1. 然后,我首先修改了 request.go ,作用是判断httpRequest是否携带上一步得到的JSONBody:
Image

Feature Category / 功能分类

API Compatibility / API 兼容性

Additional Context / 其他补充信息

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions