-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from cescoffier/in-process-model
Add support for in-process embedding models
- Loading branch information
Showing
42 changed files
with
1,462 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...ment/src/main/java/io/quarkiverse/langchain4j/deployment/InProcessEmbeddingProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package io.quarkiverse.langchain4j.deployment; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
import dev.langchain4j.model.embedding.EmbeddingModel; | ||
import io.quarkiverse.langchain4j.deployment.items.InProcessEmbeddingBuildItem; | ||
import io.quarkiverse.langchain4j.runtime.InProcessEmbeddingRecorder; | ||
import io.quarkus.arc.deployment.SyntheticBeanBuildItem; | ||
import io.quarkus.bootstrap.classloading.QuarkusClassLoader; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.annotations.ExecutionTime; | ||
import io.quarkus.deployment.annotations.Record; | ||
|
||
/** | ||
* Generate a local embedding build item for each local embedding model available in the classpath. | ||
* Note that the user must have the dependency for the model in their pom.xml/build.gradle. | ||
*/ | ||
public class InProcessEmbeddingProcessor { | ||
|
||
// https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2 | ||
@BuildStep | ||
InProcessEmbeddingBuildItem all_minilm_l6_v2_q() { | ||
if (QuarkusClassLoader | ||
.isClassPresentAtRuntime("dev.langchain4j.model.embedding.AllMiniLmL6V2QuantizedEmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("all-minilm-l6-v2-q", | ||
"dev.langchain4j.model.embedding.AllMiniLmL6V2QuantizedEmbeddingModel", | ||
"all-minilm-l6-v2-q.onnx", "bert-vocabulary-en.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2 | ||
@BuildStep | ||
InProcessEmbeddingBuildItem all_minilm_l6_v2() { | ||
if (QuarkusClassLoader.isClassPresentAtRuntime("dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("all-minilm-l6-v2", | ||
"dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel", | ||
"all-minilm-l6-v2.onnx", "bert-vocabulary-en.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// https://huggingface.co/neuralmagic/bge-small-en-v1.5-quant | ||
@BuildStep | ||
InProcessEmbeddingBuildItem bge_small_en_q() { | ||
if (QuarkusClassLoader.isClassPresentAtRuntime("dev.langchain4j.model.embedding.BgeSmallEnQuantizedEmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("bge-small-en-q", | ||
"dev.langchain4j.model.embedding.BgeSmallEnQuantizedEmbeddingModel", | ||
"bge-small-en-q.onnx", "bert-vocabulary-en.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// https://huggingface.co/BAAI/bge-small-en-v1.5 | ||
@BuildStep | ||
InProcessEmbeddingBuildItem bge_small_en() { | ||
if (QuarkusClassLoader.isClassPresentAtRuntime("dev.langchain4j.model.embedding.BgeSmallEnEmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("bge-small-en", "dev.langchain4j.model.embedding.BgeSmallEnEmbeddingModel", | ||
"bge-small-en.onnx", "bert-vocabulary-en.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@BuildStep | ||
InProcessEmbeddingBuildItem bge_small_zh_q() { | ||
if (QuarkusClassLoader.isClassPresentAtRuntime("dev.langchain4j.model.embedding.BgeSmallZhQuantizedEmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("bge-small-zh-q", | ||
"dev.langchain4j.model.embedding.BgeSmallZhQuantizedEmbeddingModel", | ||
"bge-small-zh-q.onnx", "bge-small-zh-vocabulary.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// https://huggingface.co/BAAI/bge-small-zh | ||
@BuildStep | ||
InProcessEmbeddingBuildItem bge_small_zh() { | ||
if (QuarkusClassLoader.isClassPresentAtRuntime("dev.langchain4j.model.embedding.BgeSmallZhEmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("bge-small-zh", "dev.langchain4j.model.embedding.BgeSmallZhEmbeddingModel", | ||
"bge-small-zh.onnx", "bge-small-zh-vocabulary.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// https://huggingface.co/intfloat/e5-small-v2 | ||
@BuildStep | ||
InProcessEmbeddingBuildItem e5_small_v2_q() { | ||
if (QuarkusClassLoader.isClassPresentAtRuntime("dev.langchain4j.model.embedding.E5SmallV2QuantizedEmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("e5-small-v2-q", | ||
"dev.langchain4j.model.embedding.E5SmallV2QuantizedEmbeddingModel", | ||
"e5-small-v2-q.onnx", "bert-vocabulary-en.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// https://huggingface.co/intfloat/e5-small-v2 | ||
@BuildStep | ||
InProcessEmbeddingBuildItem e5_small_v2() { | ||
if (QuarkusClassLoader.isClassPresentAtRuntime("dev.langchain4j.model.embedding.E5SmallV2EmbeddingModel")) { | ||
return new InProcessEmbeddingBuildItem("e5-small-v2", "dev.langchain4j.model.embedding.E5SmallV2EmbeddingModel", | ||
"e5-small-v2.onnx", "bert-vocabulary-en.txt"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// Expose a bean for each in process embedding model | ||
@BuildStep | ||
@Record(ExecutionTime.RUNTIME_INIT) | ||
void exposeInProcessEmbeddingBeans(InProcessEmbeddingRecorder recorder, | ||
List<InProcessEmbeddingBuildItem> embeddings, | ||
BuildProducer<SyntheticBeanBuildItem> beanProducer) { | ||
|
||
for (InProcessEmbeddingBuildItem embedding : embeddings) { | ||
beanProducer.produce(SyntheticBeanBuildItem | ||
.configure(DotName.createSimple(embedding.className())) | ||
.types(EmbeddingModel.class) | ||
.defaultBean() | ||
.setRuntimeInit() | ||
.scope(ApplicationScoped.class) | ||
.supplier(recorder.instantiate(embedding.className())) | ||
.done()); | ||
} | ||
|
||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...rc/main/java/io/quarkiverse/langchain4j/deployment/items/InProcessEmbeddingBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.quarkiverse.langchain4j.deployment.items; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
public final class InProcessEmbeddingBuildItem extends MultiBuildItem implements ProviderHolder { | ||
|
||
private final String modelName; | ||
private final String onnxModelPath; | ||
private final String vocabularyPath; | ||
|
||
private final String className; | ||
|
||
public InProcessEmbeddingBuildItem(String modelName, String className, String onnxModelPath, String vocabularyPath) { | ||
this.modelName = modelName; | ||
this.className = className; | ||
this.onnxModelPath = onnxModelPath; | ||
this.vocabularyPath = vocabularyPath; | ||
} | ||
|
||
public String modelName() { | ||
return modelName; | ||
} | ||
|
||
public String onnxModelPath() { | ||
return onnxModelPath; | ||
} | ||
|
||
public String vocabularyPath() { | ||
return vocabularyPath; | ||
} | ||
|
||
public String className() { | ||
return className; | ||
} | ||
|
||
@Override | ||
public String getProvider() { | ||
return className; | ||
} | ||
} |
Oops, something went wrong.