mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-22 11:41:02 +02:00
Knowledge core CLI
This commit is contained in:
parent
807c19fd22
commit
d4e49ef07c
7 changed files with 330 additions and 0 deletions
39
test-api/test-knowledge-delete
Executable file
39
test-api/test-knowledge-delete
Executable file
|
|
@ -0,0 +1,39 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import base64
|
||||||
|
import time
|
||||||
|
|
||||||
|
url = "http://localhost:8088/api/v1/"
|
||||||
|
|
||||||
|
############################################################################
|
||||||
|
|
||||||
|
input = {
|
||||||
|
"operation": "delete-kg-core",
|
||||||
|
"id": "https://trustgraph.ai/doc/intelligence-and-state",
|
||||||
|
"user": "trustgraph",
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = requests.post(
|
||||||
|
f"{url}knowledge",
|
||||||
|
json=input,
|
||||||
|
)
|
||||||
|
|
||||||
|
print(resp.text)
|
||||||
|
resp = resp.json()
|
||||||
|
|
||||||
|
print(resp)
|
||||||
|
|
||||||
|
if "error" in resp:
|
||||||
|
print(f"Error: {resp['error']}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# print(resp["response"])
|
||||||
|
print(resp)
|
||||||
|
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
############################################################################
|
||||||
|
|
||||||
|
|
@ -7,6 +7,7 @@ import time
|
||||||
from . library import Library
|
from . library import Library
|
||||||
from . flow import Flow
|
from . flow import Flow
|
||||||
from . config import Config
|
from . config import Config
|
||||||
|
from . knowledge import Knowledge
|
||||||
from . exceptions import *
|
from . exceptions import *
|
||||||
from . types import *
|
from . types import *
|
||||||
|
|
||||||
|
|
@ -39,6 +40,9 @@ class Api:
|
||||||
def config(self):
|
def config(self):
|
||||||
return Config(api=self)
|
return Config(api=self)
|
||||||
|
|
||||||
|
def knowledge(self):
|
||||||
|
return Knowledge(api=self)
|
||||||
|
|
||||||
def request(self, path, request):
|
def request(self, path, request):
|
||||||
|
|
||||||
url = f"{self.url}{path}"
|
url = f"{self.url}{path}"
|
||||||
|
|
|
||||||
41
trustgraph-base/trustgraph/api/knowledge.py
Normal file
41
trustgraph-base/trustgraph/api/knowledge.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
|
||||||
|
import json
|
||||||
|
import base64
|
||||||
|
|
||||||
|
from .. knowledge import hash, Uri, Literal
|
||||||
|
from . types import Triple
|
||||||
|
|
||||||
|
def to_value(x):
|
||||||
|
if x["e"]: return Uri(x["v"])
|
||||||
|
return Literal(x["v"])
|
||||||
|
|
||||||
|
class Knowledge:
|
||||||
|
|
||||||
|
def __init__(self, api):
|
||||||
|
self.api = api
|
||||||
|
|
||||||
|
def request(self, request):
|
||||||
|
|
||||||
|
return self.api.request(f"knowledge", request)
|
||||||
|
|
||||||
|
def list_kg_cores(self, user="trustgraph"):
|
||||||
|
|
||||||
|
# The input consists of system and prompt strings
|
||||||
|
input = {
|
||||||
|
"operation": "list-kg-cores",
|
||||||
|
"user": user,
|
||||||
|
}
|
||||||
|
|
||||||
|
return self.request(request = input)["ids"]
|
||||||
|
|
||||||
|
def delete_kg_core(self, id, user="trustgraph"):
|
||||||
|
|
||||||
|
# The input consists of system and prompt strings
|
||||||
|
input = {
|
||||||
|
"operation": "delete-kg-core",
|
||||||
|
"user": user,
|
||||||
|
"id": id,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.request(request = input)
|
||||||
|
|
||||||
61
trustgraph-cli/scripts/tg-delete-kg-core
Executable file
61
trustgraph-cli/scripts/tg-delete-kg-core
Executable file
|
|
@ -0,0 +1,61 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
Deletes a flow class
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import tabulate
|
||||||
|
from trustgraph.api import Api
|
||||||
|
import json
|
||||||
|
|
||||||
|
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||||
|
|
||||||
|
def delete_kg_core(url, user, id):
|
||||||
|
|
||||||
|
api = Api(url).knowledge()
|
||||||
|
|
||||||
|
class_names = api.delete_kg_core(user = user, id = id)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog='tg-delete-flow-class',
|
||||||
|
description=__doc__,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-u', '--api-url',
|
||||||
|
default=default_url,
|
||||||
|
help=f'API URL (default: {default_url})',
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-U', '--user',
|
||||||
|
default="trustgraph",
|
||||||
|
help='API URL (default: trustgraph)',
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--id', '--identifier',
|
||||||
|
required=True,
|
||||||
|
help=f'Knowledge core ID',
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
delete_kg_core(
|
||||||
|
url=args.api_url,
|
||||||
|
user=args.user,
|
||||||
|
id=args.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print("Exception:", e, flush=True)
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
123
trustgraph-cli/scripts/tg-fetch-kg-core
Executable file
123
trustgraph-cli/scripts/tg-fetch-kg-core
Executable file
|
|
@ -0,0 +1,123 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
Uses the agent service to answer a question
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import textwrap
|
||||||
|
import uuid
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
from websockets.asyncio.client import connect
|
||||||
|
import msgpack
|
||||||
|
|
||||||
|
default_url = os.getenv("TRUSTGRAPH_URL", 'ws://localhost:8088/')
|
||||||
|
default_user = 'trustgraph'
|
||||||
|
|
||||||
|
async def fetch(url, user, id, output):
|
||||||
|
|
||||||
|
if not url.endswith("/"):
|
||||||
|
url += "/"
|
||||||
|
|
||||||
|
url = url + "api/v1/socket"
|
||||||
|
|
||||||
|
mid = str(uuid.uuid4())
|
||||||
|
|
||||||
|
async with connect(url) as ws:
|
||||||
|
|
||||||
|
req = json.dumps({
|
||||||
|
"id": mid,
|
||||||
|
"service": "knowledge",
|
||||||
|
"request": {
|
||||||
|
"operation": "fetch-kg-core",
|
||||||
|
"user": user,
|
||||||
|
"id": id,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await ws.send(req)
|
||||||
|
|
||||||
|
ge = 0
|
||||||
|
t = 0
|
||||||
|
|
||||||
|
with open(output, "wb") as f:
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
msg = await ws.recv()
|
||||||
|
|
||||||
|
obj = json.loads(msg)
|
||||||
|
|
||||||
|
if "error" in obj:
|
||||||
|
raise RuntimeError(obj["error"])
|
||||||
|
|
||||||
|
if "response" not in obj:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if "eos" in obj["response"]:
|
||||||
|
if obj["response"]["eos"]: break
|
||||||
|
|
||||||
|
if "triples" in obj["response"]:
|
||||||
|
t += 1
|
||||||
|
msg = obj["response"]["triples"]
|
||||||
|
f.write(msgpack.packb(msg, use_bin_type=True))
|
||||||
|
|
||||||
|
if "graph-embeddings" in obj["response"]:
|
||||||
|
ge += 1
|
||||||
|
msg = obj["response"]["graph-embeddings"]
|
||||||
|
f.write(msgpack.packb(msg, use_bin_type=True))
|
||||||
|
|
||||||
|
print(f"Wrote: {t} triple, {ge} GE messages.")
|
||||||
|
await ws.close()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog='tg-invoke-agent',
|
||||||
|
description=__doc__,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-u', '--url',
|
||||||
|
default=default_url,
|
||||||
|
help=f'API URL (default: {default_url})',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-U', '--user',
|
||||||
|
default=default_user,
|
||||||
|
help=f'User ID (default: {default_user})'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--id', '--identifier',
|
||||||
|
required=True,
|
||||||
|
help=f'Knowledge core ID',
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-o', '--output',
|
||||||
|
required=True,
|
||||||
|
help=f'Output file'
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
asyncio.run(
|
||||||
|
fetch(
|
||||||
|
url = args.url,
|
||||||
|
user = args.user,
|
||||||
|
id = args.id,
|
||||||
|
output = args.output,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print("Exception:", e, flush=True)
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
59
trustgraph-cli/scripts/tg-show-kg-cores
Executable file
59
trustgraph-cli/scripts/tg-show-kg-cores
Executable file
|
|
@ -0,0 +1,59 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
Shows knowledge cores
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import tabulate
|
||||||
|
from trustgraph.api import Api, ConfigKey
|
||||||
|
import json
|
||||||
|
|
||||||
|
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||||
|
|
||||||
|
def show_cores(url, user):
|
||||||
|
|
||||||
|
api = Api(url).knowledge()
|
||||||
|
|
||||||
|
ids = api.list_kg_cores()
|
||||||
|
|
||||||
|
if len(ids) == 0:
|
||||||
|
print("No knowledge cores.")
|
||||||
|
|
||||||
|
for id in ids:
|
||||||
|
print(id)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog='tg-show-flows',
|
||||||
|
description=__doc__,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-u', '--api-url',
|
||||||
|
default=default_url,
|
||||||
|
help=f'API URL (default: {default_url})',
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-U', '--user',
|
||||||
|
default="trustgraph",
|
||||||
|
help='API URL (default: trustgraph)',
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
show_cores(
|
||||||
|
url=args.api_url, user=args.user
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print("Exception:", e, flush=True)
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
@ -46,7 +46,9 @@ setuptools.setup(
|
||||||
scripts=[
|
scripts=[
|
||||||
"scripts/tg-add-library-document",
|
"scripts/tg-add-library-document",
|
||||||
"scripts/tg-delete-flow-class",
|
"scripts/tg-delete-flow-class",
|
||||||
|
"scripts/tg-delete-kg-core",
|
||||||
"scripts/tg-dump-msgpack",
|
"scripts/tg-dump-msgpack",
|
||||||
|
"scripts/tg-fetch-kg-core",
|
||||||
"scripts/tg-get-flow-class",
|
"scripts/tg-get-flow-class",
|
||||||
"scripts/tg-graph-to-turtle",
|
"scripts/tg-graph-to-turtle",
|
||||||
"scripts/tg-init-trustgraph",
|
"scripts/tg-init-trustgraph",
|
||||||
|
|
@ -72,6 +74,7 @@ setuptools.setup(
|
||||||
"scripts/tg-show-flow-state",
|
"scripts/tg-show-flow-state",
|
||||||
"scripts/tg-show-flows",
|
"scripts/tg-show-flows",
|
||||||
"scripts/tg-show-graph",
|
"scripts/tg-show-graph",
|
||||||
|
"scripts/tg-show-kg-cores",
|
||||||
"scripts/tg-show-library-documents",
|
"scripts/tg-show-library-documents",
|
||||||
"scripts/tg-show-library-processing",
|
"scripts/tg-show-library-processing",
|
||||||
"scripts/tg-show-processor-state",
|
"scripts/tg-show-processor-state",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue