Skip to content

Commit

Permalink
Merge branch 'kag_agent' into kag_agent_baseline_impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhanGHanG9991 authored Oct 9, 2024
2 parents b003c6a + 88c8bcb commit 5bd3892
Show file tree
Hide file tree
Showing 115 changed files with 2,653 additions and 984 deletions.
12 changes: 6 additions & 6 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied.

@baifuyu @leywar @yixianlzz
@andylau-55 @leywar @caszkgui @matthewhyx

/cloudext fuyu.bfy@antgroup.com [email protected]
/python [email protected] [email protected] [email protected] [email protected]
/reasoner [email protected] [email protected] [email protected] [email protected] [email protected] fuyu.bfy@antgroup.com
/builder fuyu.bfy@antgroup.com [email protected]
/server fuyu.bfy@antgroup.com [email protected] [email protected]
/cloudext andy.yj@antgroup.com leywar.liang@antgroup.com [email protected] matthew.hyx@antgroup.com
/python [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
/reasoner [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] matthew.hyx@antgroup.com
/builder andy.yj@antgroup.com leywar.liang@antgroup.com [email protected] matthew.hyx@antgroup.com
/server andy.yj@antgroup.com [email protected] leywar.liang@antgroup.com caszkgui@gmail.com

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.antgroup.openspg.builder.model.record.BaseRecord;
import com.antgroup.openspg.builder.model.record.BaseSPGRecord;
import com.antgroup.openspg.core.schema.model.semantic.DynamicTaxonomySemantic;
import com.antgroup.openspg.core.schema.model.semantic.LogicalCausationSemantic;
import com.antgroup.openspg.core.schema.model.semantic.TripleSemantic;
import com.antgroup.openspg.core.schema.model.type.ConceptList;
import com.antgroup.openspg.reasoner.catalog.impl.OpenSPGCatalog;
import com.antgroup.openspg.reasoner.common.graph.vertex.IVertexId;
Expand Down Expand Up @@ -106,7 +106,7 @@ private List<BaseSPGRecord> reasoning(BaseAdvancedRecord record, ConceptList con
}

// then run causal reasoning logic
for (LogicalCausationSemantic leadTo : conceptList.getLogicalCausation()) {
for (TripleSemantic leadTo : conceptList.getLogicalCausation()) {
spgRecords = causalConceptReasoner.reason(spgRecords, leadTo);
}
return spgRecords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public static List<BaseSPGRecord> toSpgRecords(
Map<String, String> properties = toProps(vertex.getValue());
BaseSPGType spgType = catalog.getSPGType(SPGTypeIdentifier.parse(vertexId.getType()));

if (spgType == null) {
return;
}
BaseAdvancedRecord advancedRecord =
VertexRecordConvertor.toAdvancedRecord(spgType, vertexId.getBizId(), properties);
results.add(advancedRecord);
Expand All @@ -88,6 +91,9 @@ public static List<BaseSPGRecord> toSpgRecords(
Relation relationType = catalog.getRelation(RelationIdentifier.parse(edge.getType()));
Map<String, String> properties = toProps(edge.getValue());

if (relationType == null) {
return;
}
RelationRecord relationRecord =
EdgeRecordConvertor.toRelationRecord(
relationType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.antgroup.openspg.builder.model.record.property.SPGPropertyRecord;
import com.antgroup.openspg.builder.model.record.property.SPGPropertyValue;
import com.antgroup.openspg.core.schema.model.semantic.DynamicTaxonomySemantic;
import com.antgroup.openspg.core.schema.model.semantic.LogicalCausationSemantic;
import com.antgroup.openspg.core.schema.model.semantic.SystemPredicateEnum;
import com.antgroup.openspg.core.schema.model.semantic.TripleSemantic;
import com.antgroup.openspg.core.schema.model.type.ConceptList;
import com.antgroup.openspg.reasoner.common.graph.vertex.IVertexId;
import com.antgroup.openspg.reasoner.graphstate.GraphState;
Expand All @@ -37,25 +37,22 @@
import org.apache.commons.collections4.CollectionUtils;
import scala.Tuple2;

public class CausalConceptReasoner implements ConceptReasoner<LogicalCausationSemantic> {
public class CausalConceptReasoner implements ConceptReasoner<TripleSemantic> {

@Setter private InductiveConceptReasoner inductiveConceptReasoner;
@Setter private BuilderCatalog builderCatalog;
@Setter private Catalog catalog;
@Setter private GraphState<IVertexId> graphState;

@Override
public List<BaseSPGRecord> reason(
List<BaseSPGRecord> records, LogicalCausationSemantic conceptSemantic) {
public List<BaseSPGRecord> reason(List<BaseSPGRecord> records, TripleSemantic conceptSemantic) {
List<BaseSPGRecord> results = new ArrayList<>(records);
propagate(records, conceptSemantic, results);
return results;
}

private void propagate(
List<BaseSPGRecord> spgRecords,
LogicalCausationSemantic conceptSemantic,
List<BaseSPGRecord> results) {
List<BaseSPGRecord> spgRecords, TripleSemantic conceptSemantic, List<BaseSPGRecord> results) {
List<BaseAdvancedRecord> toPropagated = new ArrayList<>();
for (BaseSPGRecord spgRecord : spgRecords) {
if (!(spgRecord instanceof BaseAdvancedRecord)) {
Expand Down Expand Up @@ -102,15 +99,15 @@ private void propagate(
nextSpgRecords = inductiveConceptReasoner.reason(nextSpgRecords, belongTo);
}

for (LogicalCausationSemantic nextLeadTo :
for (TripleSemantic nextLeadTo :
conceptList.getLogicalCausation(conceptSemantic.getObjectIdentifier())) {
propagate(nextSpgRecords, nextLeadTo, results);
}
}
}
}

private List<BaseSPGRecord> leadTo(BaseAdvancedRecord record, LogicalCausationSemantic leadTo) {
private List<BaseSPGRecord> leadTo(BaseAdvancedRecord record, TripleSemantic leadTo) {
LocalReasonerTask reasonerTask = new LocalReasonerTask();

reasonerTask.setCatalog(catalog);
Expand Down
4 changes: 2 additions & 2 deletions dev/release/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.7"
services:
server:
restart: always
image: openspg/openspg-server:latest
image: spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-server:latest
container_name: release-openspg-server
ports:
- "8887:8887"
Expand All @@ -19,7 +19,7 @@ services:

mysql:
restart: always
image: openspg/openspg-mysql:latest
image: spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-mysql:latest
container_name: release-openspg-mysql
environment:
TZ: Asia/Shanghai
Expand Down
4 changes: 3 additions & 1 deletion dev/release/mysql/buildx-release-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# or implied.

docker buildx build -f Dockerfile --platform linux/arm64/v8,linux/amd64 --push \
-t openspg/openspg-mysql:0.0.3-beta1 \
-t spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-mysql:0.0.3 \
-t spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-mysql:latest \
-t openspg/openspg-mysql:0.0.3 \
-t openspg/openspg-mysql:latest \
.
2 changes: 1 addition & 1 deletion dev/release/python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ENV LANG C.UTF-8
ARG TARGETPLATFORM
ARG APT_INSTALL="apt-get install --no-install-recommends -y"

ARG PIP_PKGS="openspg-knext openspg-nn4k pemja==0.4.0"
ARG PIP_PKGS="openspg-knext==0.0.3 openspg-nn4k==0.0.3 pemja==0.4.0"

RUN apt-get clean && apt-get update && \
# Install openjdk 8.
Expand Down
4 changes: 3 additions & 1 deletion dev/release/python/build-release-python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

# for amd64
docker build -f Dockerfile --platform linux/amd64 --push \
-t openspg/openspg-python:0.0.3-beta1 \
-t spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-python:0.0.3 \
-t spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-python:latest \
-t openspg/openspg-python:0.0.3 \
-t openspg/openspg-python:latest \
.
4 changes: 3 additions & 1 deletion dev/release/server/buildx-release-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# or implied.

docker buildx build -f Dockerfile --platform linux/arm64/v8,linux/amd64 --push \
-t openspg/openspg-server:0.0.3-beta1 \
-t spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-server:0.0.3 \
-t spg-registry.cn-hangzhou.cr.aliyuncs.com/spg/openspg-server:latest \
-t openspg/openspg-server:0.0.3 \
-t openspg/openspg-server:latest \
.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
<artifactId>reasoner-local-runner</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.antgroup.openspg.reasoner</groupId>
<artifactId>reasoner-thinker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.antgroup.openspg.reasoner</groupId>
<artifactId>reasoner-cloudext-warehouse</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion python/knext/KNEXT_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3-beta2
0.0.3
2 changes: 1 addition & 1 deletion python/knext/knext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


__package_name__ = "openspg-knext"
__version__ = "0.0.3-beta2"
__version__ = "0.0.3"

from knext.common.env import init_env

Expand Down
19 changes: 15 additions & 4 deletions python/knext/knext/builder/operator/builtin/auto_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@

import json
import re
import uuid
from abc import ABC
from typing import List, Dict, Tuple


from knext.builder.operator.op import PromptOp
from knext.builder.operator.spg_record import SPGRecord
from knext.schema.client import SchemaClient
from knext.schema.model.base import BaseSpgType
from knext.schema.model.schema_helper import SPGTypeName, PropertyName, RelationName
from knext.builder.operator.op import PromptOp
from knext.builder.operator.spg_record import SPGRecord
import uuid


class AutoPrompt(PromptOp, ABC):
Expand Down Expand Up @@ -119,6 +118,12 @@ def __init__(

self._init_render_variables()
self._render()
self.params = {
"spg_type_name": spg_type_name,
"property_names": property_names,
"relation_names": relation_names,
"with_description": with_description,
}

def build_prompt(self, variables: Dict[str, str]) -> str:
return self.template.replace("${input}", variables.get("input"))
Expand Down Expand Up @@ -292,6 +297,12 @@ def __init__(

self._init_render_variables()
self._render()
self.params = {
"event_type_name": event_type_name,
"property_names": property_names,
"relation_names": relation_names,
"with_description": with_description,
}

def build_prompt(self, variables: Dict[str, str]) -> str:
return self.template.replace("${input}", variables.get("input"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Requirements:
1. When answering questions, rely on the information provided in the context below.
2. Answer the questions as directly as possible, without including extraneous information.
3. Do not include information that is only relevant to one aspect.
4. In addition to the context, you can also rely on some common knowledge when answering questions.
5. If the context does not provide relevant information, respond with "No relevant information found," without fabricating an answer.

Example 1:
llm_input: What is the relationship between Jin Yong and Xu Zhimo?
context:
Everyone knows that Jin Yong and Xu Zhimo are cousins. Jin Yong's mother and Xu Zhimo's father are cousins, and Xu Zhimo is about 27 years older than Jin Yong, so Jin Yong has to call Xu Zhimo his cousin. Jin Yong's original name is Cha Liangyong, and both Jin Yong's and Xu Zhimo's families are wealthy families in Jiangsu and Zhejiang.
llm_output: Cousins

Example 2:
llm_input: What is the surname of Xie Nan's father?
context:
No information found related to Xie Nan's father
llm_output: Xie


Based on the above requirements and examples, answer the following question:
llm_input: $question
context: $context
llm_output:
11 changes: 11 additions & 0 deletions python/knext/knext/command/knext_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from knext.command.sub_command.project import create_project
from knext.command.sub_command.project import list_project
from knext.command.sub_command.reasoner import execute_reasoner_job
from knext.command.sub_command.thinker import execute_thinker_job
from knext.command.sub_command.schema import commit_schema
from knext.command.sub_command.schema import list_schema
from knext.command.sub_command.schema import reg_concept_rule
Expand Down Expand Up @@ -79,5 +80,15 @@ def reasoner() -> None:

reasoner.command("execute")(execute_reasoner_job)


@_main.group()
def thinker() -> None:
"""Thinker client."""
pass


thinker.command("execute")(execute_thinker_job)


if __name__ == "__main__":
_main()
28 changes: 28 additions & 0 deletions python/knext/knext/command/sub_command/thinker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Copyright 2023 OpenSPG Authors
#
# Licensed 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.

import click

from knext.thinker.client import ThinkerClient


@click.option("--subject", help="The subject of reasoning goal, eg: id,type or type")
@click.option("--predicate", help="The predicate of reasoning goal, eg: type")
@click.option("--object", help="The object of reasoning goal, eg: id,type or type")
@click.option("--mode", help="Reasoning mode, eg: spo or node")
@click.option("--params", help="Reasoning context")
def execute_thinker_job(subject="", predicate="", object="", mode="spo", params=""):
"""
Submit asynchronous reasoner jobs to server by providing DSL file or string.
"""
client = ThinkerClient()
client.execute(subject, predicate, object, mode, params)
Loading

0 comments on commit 5bd3892

Please sign in to comment.