forked from Merck/Halyard
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Hale
committed
Sep 27, 2024
1 parent
45409ef
commit 289e72d
Showing
8 changed files
with
126 additions
and
13 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
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
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
53 changes: 53 additions & 0 deletions
53
sail/src/main/java/com/msd/gin/halyard/sail/model/embedding/function/VectorEmbedding.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,53 @@ | ||
package com.msd.gin.halyard.sail.model.embedding.function; | ||
|
||
import com.msd.gin.halyard.model.ArrayLiteral; | ||
import com.msd.gin.halyard.model.vocabulary.HALYARD; | ||
import com.msd.gin.halyard.query.algebra.evaluation.ExtendedTripleSource; | ||
|
||
import org.eclipse.rdf4j.model.Literal; | ||
import org.eclipse.rdf4j.model.Value; | ||
import org.eclipse.rdf4j.model.ValueFactory; | ||
import org.eclipse.rdf4j.query.QueryEvaluationException; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.TripleSource; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.function.Function; | ||
import org.kohsuke.MetaInfServices; | ||
|
||
import dev.langchain4j.data.embedding.Embedding; | ||
import dev.langchain4j.model.embedding.EmbeddingModel; | ||
import dev.langchain4j.model.output.Response; | ||
|
||
@MetaInfServices(Function.class) | ||
public class VectorEmbedding implements Function { | ||
|
||
@Override | ||
public String getURI() { | ||
return HALYARD.VECTOR_EMBEDDING_FUNCTION.stringValue(); | ||
} | ||
|
||
@Override | ||
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Value evaluate(TripleSource ts, Value... args) throws ValueExprEvaluationException { | ||
if (args.length != 1) { | ||
throw new QueryEvaluationException("Missing arguments"); | ||
} | ||
|
||
if (!args[0].isLiteral()) { | ||
throw new QueryEvaluationException(String.format("Non-literal value: %s", args[0])); | ||
} | ||
Literal l = (Literal) args[0]; | ||
|
||
ExtendedTripleSource extTs = (ExtendedTripleSource) ts; | ||
Response<Embedding> resp = extTs.getQueryHelper(EmbeddingModel.class).embed(l.getLabel()); | ||
float[] vec = resp.content().vector(); | ||
Float[] arr = new Float[vec.length]; | ||
for (int i = 0; i < vec.length; i++) { | ||
arr[i] = Float.valueOf(vec[i]); | ||
} | ||
return new ArrayLiteral((Object[]) arr); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../java/com/msd/gin/halyard/sail/model/embedding/EmbeddingModelQueryHelperProviderTest.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,20 @@ | ||
package com.msd.gin.halyard.sail.model.embedding; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import dev.langchain4j.model.localai.LocalAiEmbeddingModel; | ||
|
||
public class EmbeddingModelQueryHelperProviderTest { | ||
@Test | ||
public void testLocalAI() throws Exception { | ||
EmbeddingModelQueryHelperProvider provider = new EmbeddingModelQueryHelperProvider(); | ||
Map<String, String> config = new HashMap<>(); | ||
config.put("model.class", LocalAiEmbeddingModel.class.getName()); | ||
config.put("baseUrl", "http://localhost"); | ||
config.put("modelName", "llama3"); | ||
provider.createQueryHelper(config); | ||
} | ||
} |