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


##########
solr/modules/llm/src/java/org/apache/solr/llm/search/TextEmbedderQParserPlugin.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.search;
+
+import java.io.IOException;
+import org.apache.lucene.index.VectorEncoding;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.ResourceLoader;
+import org.apache.lucene.util.ResourceLoaderAware;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.SolrResourceLoader;
+import org.apache.solr.llm.embedding.SolrEmbeddingModel;
+import org.apache.solr.llm.store.rest.ManagedEmbeddingModelStore;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.rest.ManagedResource;
+import org.apache.solr.rest.ManagedResourceObserver;
+import org.apache.solr.schema.DenseVectorField;
+import org.apache.solr.schema.SchemaField;
+import org.apache.solr.search.QParser;
+import org.apache.solr.search.QParserPlugin;
+import org.apache.solr.search.SyntaxError;
+import org.apache.solr.search.neural.KnnQParser;
+
+/**
+ * A neural query parser that embed the query and then run K-nearest neighbors 
search on Dense
+ * Vector fields. See Wiki page
+ * 
https://solr.apache.org/guide/solr/latest/query-guide/dense-vector-search.html
+ */
+public class TextEmbedderQParserPlugin extends QParserPlugin
+    implements ResourceLoaderAware, ManagedResourceObserver {
+  public static final String EMBEDDING_MODEL_PARAM = "model";
+  private ManagedEmbeddingModelStore modelStore = null;
+
+  @Override
+  public QParser createParser(
+      String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest 
req) {
+    return new TextEmbedderQParser(qstr, localParams, params, req);
+  }
+
+  @Override
+  public void inform(ResourceLoader loader) throws IOException {
+    final SolrResourceLoader solrResourceLoader = (SolrResourceLoader) loader;
+    
ManagedEmbeddingModelStore.registerManagedEmbeddingModelStore(solrResourceLoader,
 this);
+  }
+
+  @Override
+  public void onManagedResourceInitialized(NamedList<?> args, ManagedResource 
res)
+      throws SolrException {
+    if (res instanceof ManagedEmbeddingModelStore) {
+      modelStore = (ManagedEmbeddingModelStore) res;
+    }
+    if (modelStore != null) {
+      // now we can safely load the models
+      modelStore.loadStoredModels();
+    }
+  }
+
+  public class TextEmbedderQParser extends KnnQParser {
+
+    public TextEmbedderQParser(
+        String qstr, SolrParams localParams, SolrParams params, 
SolrQueryRequest req) {
+      super(qstr, localParams, params, req);
+    }
+
+    @Override
+    public Query parse() throws SyntaxError {
+      checkParam(qstr, "Query string is empty, nothing to embed");
+      final String embeddingModelName = localParams.get(EMBEDDING_MODEL_PARAM);
+      checkParam(embeddingModelName, "The 'model' parameter is missing");
+      SolrEmbeddingModel embedder = modelStore.getModel(embeddingModelName);
+
+      if (embedder != null) {
+        final SchemaField schemaField = 
req.getCore().getLatestSchema().getField(getFieldName());
+        final DenseVectorField denseVectorType = 
getCheckedFieldType(schemaField);
+        int fieldDimensions = denseVectorType.getDimension();
+        VectorEncoding vectorEncoding = denseVectorType.getVectorEncoding();
+        final int topK = localParams.getInt(TOP_K, DEFAULT_TOP_K);
+
+        switch (vectorEncoding) {
+          case FLOAT32:
+            {
+              float[] vectorToSearch = embedder.vectorise(qstr);
+              checkVectorDimension(vectorToSearch.length, fieldDimensions);
+              return denseVectorType.getKnnVectorQuery(
+                  schemaField.getName(), vectorToSearch, topK, 
getFilterQuery());

Review Comment:
   Could we call the constructor directly here?
   ```suggestion
                 return new KnnFloatVectorQuery(
                     schemaField.getName(), vectorToSearch, topK, 
getFilterQuery());
   ```



-- 
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