-
Notifications
You must be signed in to change notification settings - Fork 155
feat(ai): support graph consolidate algorithm #729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,640
−17
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
7d8832f
init dcp code
Leomrlin 1b10d23
Merge remote-tracking branch 'origin/master' into dev_init_dcp
Leomrlin 73b97d1
add lucene search
Leomrlin 0da962e
add prompt formatter
Leomrlin 18b359f
add test case
Leomrlin 3bd80f0
handle ldbc id conflict
Leomrlin 4c1aa15
support llm
Leomrlin e0e983a
support embedding index store
Leomrlin b945221
add embedding op
Leomrlin 3253a0e
refine test case
Leomrlin 1b2fe59
delete test data
Leomrlin 5ee48a1
add MockChatRobot
Leomrlin a127c4b
fix checkstyle
Leomrlin ce2fde1
Merge remote-tracking branch 'origin/master' into dev_init_dcp
Leomrlin 8ccc524
fix pom
Leomrlin 453dfd9
fix finishReason
Leomrlin a80cc46
fix ci tests
Leomrlin bb12777
fix ci tests
Leomrlin 3975294
fix comments
Leomrlin adffdd9
fix codestyle
Leomrlin 65835ce
support mutable graph
Leomrlin 9f48bf1
Merge remote-tracking branch 'origin/master' into dev_init_dcp
Leomrlin 94baf1b
Add ConsolidateFunction
Leomrlin 2ed341b
Add geaflow memory server
Leomrlin 2db5068
Add GeaFlowMemoryClientCLI
Leomrlin e69bed2
fix comment
Leomrlin 8fa8742
refine code
Leomrlin c504e94
Merge branch 'master' into dev_self_built_graph
Leomrlin 840cde8
replay commit
Leomrlin 19602af
add consolidate server
Leomrlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
232 changes: 232 additions & 0 deletions
232
geaflow-ai/src/main/java/org/apache/geaflow/ai/GeaFlowMemoryServer.java
This file contains hidden or 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,232 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.geaflow.ai.common.util.SeDeUtil; | ||
| import org.apache.geaflow.ai.graph.*; | ||
| import org.apache.geaflow.ai.graph.io.*; | ||
| import org.apache.geaflow.ai.index.EntityAttributeIndexStore; | ||
| import org.apache.geaflow.ai.index.vector.KeywordVector; | ||
| import org.apache.geaflow.ai.search.VectorSearch; | ||
| import org.apache.geaflow.ai.service.ServerMemoryCache; | ||
| import org.apache.geaflow.ai.verbalization.Context; | ||
| import org.apache.geaflow.ai.verbalization.SubgraphSemanticPromptFunction; | ||
| import org.noear.solon.Solon; | ||
| import org.noear.solon.annotation.*; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @Controller | ||
| public class GeaFlowMemoryServer { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(GeaFlowMemoryServer.class); | ||
|
|
||
| private static final String SERVER_NAME = "geaflow-memory-server"; | ||
| private static final int DEFAULT_PORT = 8080; | ||
|
|
||
| private static final ServerMemoryCache CACHE = new ServerMemoryCache(); | ||
|
|
||
| public static void main(String[] args) { | ||
| System.setProperty("solon.app.name", SERVER_NAME); | ||
| Solon.start(GeaFlowMemoryServer.class, args, app -> { | ||
| app.cfg().loadAdd("application.yml"); | ||
| int port = app.cfg().getInt("server.port", DEFAULT_PORT); | ||
| LOGGER.info("Starting {} on port {}", SERVER_NAME, port); | ||
| app.get("/", ctx -> { | ||
| ctx.output("GeaFlow AI Server is running..."); | ||
| }); | ||
| app.get("/health", ctx -> { | ||
| ctx.output("{\"status\":\"UP\",\"service\":\"" + SERVER_NAME + "\"}"); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| @Get | ||
| @Mapping("/api/test") | ||
| public String test() { | ||
| return "GeaFlow Memory Server is working!"; | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/graph/create") | ||
| public String createGraph(@Body String input) { | ||
| GraphSchema graphSchema = SeDeUtil.deserializeGraphSchema(input); | ||
| String graphName = graphSchema.getName(); | ||
| if (graphName == null || CACHE.getGraphByName(graphName) != null) { | ||
| throw new RuntimeException("Cannot create graph name: " + graphName); | ||
| } | ||
| Map<String, EntityGroup> entities = new HashMap<>(); | ||
| for (VertexSchema vertexSchema : graphSchema.getVertexSchemaList()) { | ||
| entities.put(vertexSchema.getName(), new VertexGroup(vertexSchema, new ArrayList<>())); | ||
| } | ||
| for (EdgeSchema edgeSchema : graphSchema.getEdgeSchemaList()) { | ||
| entities.put(edgeSchema.getName(), new EdgeGroup(edgeSchema, new ArrayList<>())); | ||
| } | ||
| MemoryGraph graph = new MemoryGraph(graphSchema, entities); | ||
| CACHE.putGraph(graph); | ||
| LocalMemoryGraphAccessor graphAccessor = new LocalMemoryGraphAccessor(graph); | ||
| LOGGER.info("Success to init empty graph."); | ||
|
|
||
| EntityAttributeIndexStore indexStore = new EntityAttributeIndexStore(); | ||
| indexStore.initStore(new SubgraphSemanticPromptFunction(graphAccessor)); | ||
| LOGGER.info("Success to init EntityAttributeIndexStore."); | ||
|
|
||
| GraphMemoryServer server = new GraphMemoryServer(); | ||
| server.addGraphAccessor(graphAccessor); | ||
| server.addIndexStore(indexStore); | ||
| LOGGER.info("Success to init GraphMemoryServer."); | ||
| CACHE.putServer(server); | ||
|
|
||
| LOGGER.info("Success to init graph. SCHEMA: {}", graphSchema); | ||
| return "createGraph has been called, graphName: " + graphName; | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/graph/addEntitySchema") | ||
| public String addSchema(@Param("graphName") String graphName, | ||
| @Body String input) { | ||
| Graph graph = CACHE.getGraphByName(graphName); | ||
| if (graph == null) { | ||
| throw new RuntimeException("Graph not exist."); | ||
| } | ||
| if (!(graph instanceof MemoryGraph)) { | ||
| throw new RuntimeException("Graph cannot modify."); | ||
| } | ||
| MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph); | ||
| Schema schema = SeDeUtil.deserializeEntitySchema(input); | ||
| String schemaName = schema.getName(); | ||
| if (schema instanceof VertexSchema) { | ||
| memoryMutableGraph.addVertexSchema((VertexSchema) schema); | ||
| } else if (schema instanceof EdgeSchema) { | ||
| memoryMutableGraph.addEdgeSchema((EdgeSchema) schema); | ||
| } else { | ||
| throw new RuntimeException("Cannt add schema: " + input); | ||
| } | ||
| return "addSchema has been called, schemaName: " + schemaName; | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/graph/getGraphSchema") | ||
| public String getSchema(@Param("graphName") String graphName) { | ||
| Graph graph = CACHE.getGraphByName(graphName); | ||
| if (graph == null) { | ||
| throw new RuntimeException("Graph not exist."); | ||
| } | ||
| if (!(graph instanceof MemoryGraph)) { | ||
| throw new RuntimeException("Graph cannot modify."); | ||
| } | ||
| return SeDeUtil.serializeGraphSchema(graph.getGraphSchema()); | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/graph/insertEntity") | ||
| public String addEntity(@Param("graphName") String graphName, | ||
| @Body String input) { | ||
| Graph graph = CACHE.getGraphByName(graphName); | ||
| if (graph == null) { | ||
| throw new RuntimeException("Graph not exist."); | ||
| } | ||
| if (!(graph instanceof MemoryGraph)) { | ||
| throw new RuntimeException("Graph cannot modify."); | ||
| } | ||
| MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph); | ||
| List<GraphEntity> graphEntities = SeDeUtil.deserializeEntities(input); | ||
|
|
||
| for (GraphEntity entity : graphEntities) { | ||
| if (entity instanceof GraphVertex) { | ||
| memoryMutableGraph.addVertex(((GraphVertex) entity).getVertex()); | ||
| } else { | ||
| memoryMutableGraph.addEdge(((GraphEdge) entity).getEdge()); | ||
| } | ||
| } | ||
| CACHE.getConsolidateServer().executeConsolidateTask( | ||
| CACHE.getServerByName(graphName).getGraphAccessors().get(0), memoryMutableGraph); | ||
| return "Success to add entities, num: " + graphEntities.size(); | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/graph/delEntity") | ||
| public String deleteEntity(@Param("graphName") String graphName, | ||
| @Body String input) { | ||
| Graph graph = CACHE.getGraphByName(graphName); | ||
| if (graph == null) { | ||
| throw new RuntimeException("Graph not exist."); | ||
| } | ||
| if (!(graph instanceof MemoryGraph)) { | ||
| throw new RuntimeException("Graph cannot modify."); | ||
| } | ||
| MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph); | ||
| List<GraphEntity> graphEntities = SeDeUtil.deserializeEntities(input); | ||
| for (GraphEntity entity : graphEntities) { | ||
| if (entity instanceof GraphVertex) { | ||
| memoryMutableGraph.removeVertex(entity.getLabel(), | ||
| ((GraphVertex) entity).getVertex().getId()); | ||
| } else { | ||
| memoryMutableGraph.removeEdge(((GraphEdge) entity).getEdge()); | ||
| } | ||
| } | ||
| return "Success to remove entities, num: " + graphEntities.size(); | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/query/context") | ||
| public String createContext(@Param("graphName") String graphName) { | ||
| GraphMemoryServer server = CACHE.getServerByName(graphName); | ||
| if (server == null) { | ||
| throw new RuntimeException("Server not exist."); | ||
| } | ||
| String sessionId = server.createSession(); | ||
| CACHE.putSession(server, sessionId); | ||
| return sessionId; | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/query/exec") | ||
| public String execQuery(@Param("sessionId") String sessionId, | ||
| @Body String query) { | ||
| String graphName = CACHE.getGraphNameBySession(sessionId); | ||
| if (graphName == null) { | ||
| throw new RuntimeException("Graph not exist."); | ||
| } | ||
| GraphMemoryServer server = CACHE.getServerByName(graphName); | ||
| VectorSearch search = new VectorSearch(null, sessionId); | ||
| search.addVector(new KeywordVector(query)); | ||
| server.search(search); | ||
| Context context = server.verbalize(sessionId, | ||
| new SubgraphSemanticPromptFunction(server.getGraphAccessors().get(0))); | ||
| return context.toString(); | ||
| } | ||
|
|
||
| @Post | ||
| @Mapping("/query/result") | ||
| public String getResult(@Param("sessionId") String sessionId) { | ||
| String graphName = CACHE.getGraphNameBySession(sessionId); | ||
| if (graphName == null) { | ||
| throw new RuntimeException("Graph not exist."); | ||
| } | ||
| GraphMemoryServer server = CACHE.getServerByName(graphName); | ||
| List<GraphEntity> result = server.getSessionEntities(sessionId); | ||
| return result.toString(); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.