alessandrobenedetti commented on code in PR #2809:
URL: https://github.com/apache/solr/pull/2809#discussion_r1863372272


##########
solr/modules/llm/src/java/org/apache/solr/llm/embedding/SolrEmbeddingModel.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.solr.llm.embedding;
+
+import dev.langchain4j.data.embedding.Embedding;
+import dev.langchain4j.model.embedding.EmbeddingModel;
+import java.lang.reflect.Method;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.Objects;
+import org.apache.lucene.util.Accountable;
+import org.apache.lucene.util.RamUsageEstimator;
+import org.apache.solr.core.SolrResourceLoader;
+import org.apache.solr.llm.store.EmbeddingModelException;
+import org.apache.solr.llm.store.rest.ManagedEmbeddingModelStore;
+
+/**
+ * This object wraps a {@link dev.langchain4j.model.embedding.EmbeddingModel} 
to encode text to
+ * vector. It's meant to be used as a managed resource with the {@link 
ManagedEmbeddingModelStore}
+ */
+public class SolrEmbeddingModel implements Accountable {
+  private static final long BASE_RAM_BYTES =
+      RamUsageEstimator.shallowSizeOfInstance(SolrEmbeddingModel.class);
+  private static final String TIMEOUT_PARAM = "timeout";
+  private static final String MAX_SEGMENTS_PER_BATCH_PARAM = 
"maxSegmentsPerBatch";
+  private static final String MAX_RETRIES_PARAM = "maxRetries";
+
+  private final String name;
+  private final Map<String, Object> params;
+  private final EmbeddingModel textToVector;
+  private final Integer hashCode;
+
+  public static SolrEmbeddingModel getInstance(
+      SolrResourceLoader solrResourceLoader,
+      String className,
+      String name,
+      Map<String, Object> params)
+      throws EmbeddingModelException {
+    try {
+      /*
+       * The idea here is to build a {@link 
dev.langchain4j.model.embedding.EmbeddingModel} using inversion
+       * of control.
+       * Each model has its own list of parameters we don't know beforehand, 
but each {@link dev.langchain4j.model.embedding.EmbeddingModel} class
+       * has its own builder that uses setters with the same name of the 
parameter in input.
+       * */
+      EmbeddingModel textToVector;
+      Class<?> modelClass = solrResourceLoader.findClass(className, 
EmbeddingModel.class);
+      var builder = modelClass.getMethod("builder").invoke(null);
+      if (params != null) {
+        /**
+         * Some {@link dev.langchain4j.model.embedding.EmbeddingModel} classes 
have params of
+         * specific types that must be constructed, for primitive types we can 
resort to the
+         * default. N.B. when adding support to new models, pay attention to 
all the parameters they
+         * support, some of them may require to be handled in here as separate 
switch cases
+         */
+        for (String paramName : params.keySet()) {
+          switch (paramName) {
+            case TIMEOUT_PARAM:
+              Duration timeOut = Duration.ofSeconds((Long) 
params.get(paramName));
+              builder.getClass().getMethod(paramName, 
Duration.class).invoke(builder, timeOut);
+              break;
+            case MAX_SEGMENTS_PER_BATCH_PARAM:
+              builder
+                  .getClass()
+                  .getMethod(paramName, Integer.class)
+                  .invoke(builder, ((Long) params.get(paramName)).intValue());
+              break;
+            case MAX_RETRIES_PARAM:
+              builder
+                  .getClass()
+                  .getMethod(paramName, Integer.class)
+                  .invoke(builder, ((Long) params.get(paramName)).intValue());
+              break;
+            default:
+              ArrayList<Method> paramNameMatches = new ArrayList<>();

Review Comment:
   agreed and added!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to