You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel电商集成Deep Infra CLIP API遇'A contents key is required'错误求助

解决Deep Infra CLIP API集成时"A 'contents' key is required"错误

在Laravel电商应用中集成Deep Infra的openai/clip-vit-large-patch14-336图像识别API时,遇到错误提示"A 'contents' key is required",原代码如下:

public function imageSearch(Request $request){
    try {
        $api = 'UMIDKJCkmZBRmCmmbzTWB8JPq3wMbd1b';
        // Instantiate a Guzzle HTTP client
        $client = new Client();

        $image = time().rand(1,1000).'.'.$request->image->extension();
        $request->image->move(public_path('search_images'),$image);

        $candidateLabels = ['cat', 'dog','phone','laptop'];
        $imagePath = public_path('search_images') . '/' . $image; // Assuming $image contains the filename

        $imageFile = fopen($imagePath, 'r');
        if (!$imageFile) {
            throw new Exception("Failed to open image file: $imagePath");
        }

        // Prepare the request body
        $body = [
            'multipart' => [
                [
                    'name' => 'image',
                    'contents' => fopen($imagePath, 'r'), // Read the image file
                    
                ],
                [
                    'name' => 'candidate_labels',
                    'contents' => json_encode($candidateLabels),
                ],
            ],
        ];

        // Prepare the request headers
        $headers = [
            'Authorization' => 'Bearer ' . $api,
        ];
        $response = $client->post('https://api.deepinfra.com/v1/inference/openai/clip-vit-base-patch32', [
            'headers' => $headers,
            'multipart' => $body,
        ]);

        // Handle the response
        $statusCode = $response->getStatusCode();
        $responseData = json_decode($response->getBody(), true);
        dd($responseData);
    } catch (\Throwable $th) {
        return $th->getMessage();
    }
}

问题原因

错误提示表明请求结构不符合Deep Infra CLIP API的要求:

  • 该API不接受multipart/form-data格式的请求,需要用application/json格式
  • 核心参数应为inputs,而非自定义的image和candidate_labels字段
  • 图像需要以base64编码的形式传入,而非直接上传文件

修正后的代码

public function imageSearch(Request $request){
    try {
        $apiKey = 'UMIDKJCkmZBRmCmmbzTWB8JPq3wMbd1b';
        $client = new Client();

        // 读取上传的图像文件并转为base64编码
        $imageBase64 = base64_encode(file_get_contents($request->image->getPathname()));
        $imageData = "data:image/{$request->image->extension()};base64,$imageBase64";

        $candidateLabels = ['cat', 'dog','phone','laptop'];

        // 构造符合API要求的JSON请求体
        $requestBody = [
            'inputs' => [
                'image' => $imageData,
                'text' => $candidateLabels
            ]
        ];

        $headers = [
            'Authorization' => "Bearer $apiKey",
            'Content-Type' => 'application/json'
        ];

        // 注意修正API端点为目标模型
        $response = $client->post('https://api.deepinfra.com/v1/inference/openai/clip-vit-large-patch14-336', [
            'headers' => $headers,
            'json' => $requestBody
        ]);

        $responseData = json_decode($response->getBody(), true);
        dd($responseData);
    } catch (\Throwable $th) {
        return $th->getMessage();
    }
}

关键修改点

  • 请求格式:从multipart改为json格式,添加Content-Type: application/json请求头
  • 图像处理:直接读取上传文件的内容转为base64,无需保存到本地,减少IO操作
  • 参数结构:使用API要求的inputs字段,包含image(base64格式)和text(候选标签数组)
  • API端点:修正为目标模型openai/clip-vit-large-patch14-336,原代码使用的是基础版模型

内容的提问来源于stack exchange,提问作者nadeesha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 23:42:46