Leomrlin commented on code in PR #716:
URL: https://github.com/apache/geaflow/pull/716#discussion_r2664121386


##########
geaflow-ai/src/main/java/org/apache/geaflow/ai/index/EmbeddingIndexStore.java:
##########
@@ -0,0 +1,251 @@
+/*
+ * 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.geaflow.ai.index;
+
+import com.google.gson.Gson;
+import java.io.*;
+import java.nio.charset.Charset;
+import java.util.*;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.geaflow.ai.common.model.ChatRobot;
+import org.apache.geaflow.ai.common.model.ModelInfo;
+import org.apache.geaflow.ai.common.model.ModelUtils;
+import org.apache.geaflow.ai.graph.GraphAccessor;
+import org.apache.geaflow.ai.graph.GraphEdge;
+import org.apache.geaflow.ai.graph.GraphEntity;
+import org.apache.geaflow.ai.graph.GraphVertex;
+import org.apache.geaflow.ai.index.vector.EmbeddingVector;
+import org.apache.geaflow.ai.index.vector.IVector;
+import org.apache.geaflow.ai.verbalization.VerbalizationFunction;
+
+public class EmbeddingIndexStore implements IndexStore {
+
+    private GraphAccessor graphAccessor;
+    private VerbalizationFunction verbFunc;
+    private String indexFilePath;
+    private ModelInfo modelInfo;
+    private Map<GraphEntity, List<ChatRobot.EmbeddingResult>> indexStoreMap;
+
+    public void initStore(GraphAccessor graphAccessor, VerbalizationFunction 
func,
+                          String indexFilePath, ModelInfo modelInfo) {
+        this.graphAccessor = graphAccessor;
+        this.verbFunc = func;
+        this.indexFilePath = indexFilePath;
+        this.modelInfo = modelInfo;
+        this.indexStoreMap = new HashMap<>();
+
+        //Read index items from indexFilePath
+        Map<String, GraphEntity> key2EntityMap = new HashMap<>();
+        for (Iterator<GraphVertex> itV = this.graphAccessor.scanVertex(); 
itV.hasNext(); ) {
+            GraphVertex vertex = itV.next();
+            key2EntityMap.put(ModelUtils.getGraphEntityKey(vertex), vertex);
+            for (Iterator<GraphEdge> itE = 
this.graphAccessor.scanEdge(vertex); itE.hasNext(); ) {
+                GraphEdge edge = itE.next();
+                key2EntityMap.put(ModelUtils.getGraphEntityKey(edge), edge);
+            }
+        }
+        System.out.println("Success to scan entities. total entities num: " + 
key2EntityMap.size());
+
+        try {
+            File indexFile = new File(this.indexFilePath);
+
+            if (!indexFile.exists()) {
+                File parentDir = indexFile.getParentFile();
+                if (parentDir != null && !parentDir.exists()) {
+                    parentDir.mkdirs();
+                }
+                indexFile.createNewFile();
+                System.out.println("Success to create new index store file. 
Path: " + this.indexFilePath);
+            }
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+
+
+        long count = 0;
+        try (BufferedReader reader = new BufferedReader(
+                new InputStreamReader(
+                        new FileInputStream(this.indexFilePath),
+                        Charset.defaultCharset()))) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                line = line.trim();
+                if (line.isEmpty()) {
+                    continue;
+                }
+                try {
+                    ChatRobot.EmbeddingResult embedding =
+                            new Gson().fromJson(line, 
ChatRobot.EmbeddingResult.class);
+                    String key = embedding.input;
+                    GraphEntity entity = key2EntityMap.get(key);
+                    if (entity != null) {
+                        this.indexStoreMap.computeIfAbsent(entity, k -> new 
ArrayList<>()).add(embedding);
+                    }
+                    count++;
+                } catch (Throwable e) {
+                    System.out.println("Cannot parse embedding item: " + line);
+                }
+            }
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+
+        System.out.println("Success to read index store file. items num: " + 
count);
+        System.out.println("Success to rebuild index with file. index num: " + 
this.indexStoreMap.size());
+
+
+        //Scan entities in the graph, make new index items
+        ChatRobot chatRobot = new ChatRobot();
+        chatRobot.setModelInfo(modelInfo);
+
+        final int BATCH_SIZE = 32;

Review Comment:
   Currently, `model.Constants` and `GraphMemoryConfigKeys` have been added, 
and all necessary literal values have been moved and declared within them.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to