EmbeddingEngine.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tika.inference;

import java.io.IOException;
import java.util.List;

import org.apache.tika.exception.TikaException;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.inference.Engine;

/** An engine that turns images into vectors, many per request. */
public interface EmbeddingEngine extends Engine {

    /** One request; the vectors come back in the order of the images. */
    List<float[]> embedImages(List<byte[]> images, List<String> mimeTypes, ParseContext context)
            throws IOException, TikaException;

    /** One request; the vectors come back in the order of the texts. */
    List<float[]> embedTexts(List<String> texts, ParseContext context)
            throws IOException, TikaException;

    int getMaxBatchSize();

    /** Whether {@link #embedMedia} is implemented; a MEDIA binding on an engine without it fails at load. */
    default boolean supportsMedia() {
        return false;
    }

    /** One request; the vectors come back in the order of the inputs. */
    default List<float[]> embedMedia(List<MediaInput> inputs, ParseContext context)
            throws IOException, TikaException {
        throw new TikaException("this engine does not embed audio or video");
    }
}