trustgraph-cli 0.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: trustgraph-cli
3
+ Version: 0.0.0
4
+ Summary: TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.
5
+ Home-page: https://github.com/trustgraph-ai/trustgraph
6
+ Author: trustgraph.ai
7
+ Author-email: security@trustgraph.ai
8
+ License: UNKNOWN
9
+ Download-URL: https://github.com/trustgraph-ai/trustgraph/archive/refs/tags/v0.0.0.tar.gz
10
+ Platform: UNKNOWN
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
13
+ Classifier: Operating System :: OS Independent
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+
17
+ See https://trustgraph.ai/
18
+
19
+
@@ -0,0 +1 @@
1
+ See https://trustgraph.ai/
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """
4
+ This utility reads a knowledge core in msgpack format and outputs its
5
+ contents in JSON form to standard output. This is useful only as a
6
+ diagnostic utility.
7
+ """
8
+
9
+ import msgpack
10
+ import sys
11
+ import argparse
12
+ import json
13
+
14
+ def dump(input_file, action):
15
+
16
+ with open(input_file, 'rb') as f:
17
+
18
+ unpacker = msgpack.Unpacker(f, raw=False)
19
+
20
+ for unpacked in unpacker:
21
+ print(json.dumps(unpacked))
22
+
23
+ def summary(input_file, action):
24
+
25
+ vector_dim = None
26
+
27
+ triples = set()
28
+
29
+ max_records = 1000000
30
+
31
+ with open(input_file, 'rb') as f:
32
+
33
+ unpacker = msgpack.Unpacker(f, raw=False)
34
+
35
+ rec_count = 0
36
+
37
+ for msg in unpacker:
38
+
39
+ if msg[0] == "ge":
40
+ vector_dim = len(msg[1]["v"][0])
41
+
42
+ if msg[0] == "t":
43
+
44
+ for elt in msg[1]["m"]["m"]:
45
+ triples.add((
46
+ elt["s"]["v"],
47
+ elt["p"]["v"],
48
+ elt["o"]["v"],
49
+ ))
50
+
51
+ if rec_count > max_records: break
52
+ rec_count += 1
53
+
54
+ print("Vector dimension:", vector_dim)
55
+
56
+ for t in triples:
57
+ if t[1] == "http://www.w3.org/2000/01/rdf-schema#label":
58
+ print("-", t[2])
59
+
60
+ def main():
61
+
62
+ parser = argparse.ArgumentParser(
63
+ prog='tg-dump-msgpack',
64
+ description=__doc__,
65
+ )
66
+
67
+ parser.add_argument(
68
+ '-i', '--input-file',
69
+ required=True,
70
+ help=f'Input file'
71
+ )
72
+
73
+ parser.add_argument(
74
+ '-s', '--summary', action="store_const", const="summary",
75
+ dest="action",
76
+ help=f'Show a summary'
77
+ )
78
+
79
+ parser.add_argument(
80
+ '-r', '--records', action="store_const", const="records",
81
+ dest="action",
82
+ help=f'Dump individual records'
83
+ )
84
+
85
+ args = parser.parse_args()
86
+
87
+ if args.action == "summary":
88
+ summary(**vars(args))
89
+ else:
90
+ dump(**vars(args))
91
+
92
+ main()
93
+
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """
4
+ Connects to the graph query service and dumps all graph edges.
5
+ """
6
+
7
+ import argparse
8
+ import os
9
+ from trustgraph.api import Api
10
+
11
+ default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
12
+ default_user = 'trustgraph'
13
+ default_collection = 'default'
14
+
15
+ def show_graph(url, user, collection):
16
+
17
+ api = Api(url)
18
+
19
+ rows = api.triples_query(
20
+ # user=user, collection=collection,
21
+ s=None, p=None, o=None, limit=10_000,
22
+ )
23
+
24
+ for row in rows:
25
+ print(row.s, row.p, row.o)
26
+
27
+ def main():
28
+
29
+ parser = argparse.ArgumentParser(
30
+ prog='tg-graph-show',
31
+ description=__doc__,
32
+ )
33
+
34
+ parser.add_argument(
35
+ '-u', '--api-url',
36
+ default=default_url,
37
+ help=f'API URL (default: {default_url})',
38
+ )
39
+
40
+ parser.add_argument(
41
+ '-U', '--user',
42
+ default=default_user,
43
+ help=f'User ID (default: {default_user})'
44
+ )
45
+
46
+ parser.add_argument(
47
+ '-C', '--collection',
48
+ default=default_collection,
49
+ help=f'Collection ID (default: {default_collection})'
50
+ )
51
+
52
+ args = parser.parse_args()
53
+
54
+ try:
55
+
56
+ show_graph(
57
+ url=args.api_url,
58
+ user=args.user,
59
+ collection=args.collection,
60
+ )
61
+
62
+ except Exception as e:
63
+
64
+ print("Exception:", e, flush=True)
65
+
66
+ main()
67
+
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """
4
+ Connects to the graph query service and dumps all graph edges in Turtle
5
+ format.
6
+ """
7
+
8
+ import rdflib
9
+ import io
10
+ import sys
11
+ import argparse
12
+ import os
13
+
14
+ from trustgraph.api import Api, Uri
15
+
16
+ default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
17
+ default_user = 'trustgraph'
18
+ default_collection = 'default'
19
+
20
+ def show_graph(url, user, collection):
21
+
22
+ api = Api(url)
23
+
24
+ rows = api.triples_query(
25
+ s=None, p=None, o=None,
26
+ limit=10_000)
27
+ # user=user, collection=collection,
28
+
29
+ g = rdflib.Graph()
30
+
31
+ for row in rows:
32
+
33
+ sv = rdflib.term.URIRef(row.s)
34
+ pv = rdflib.term.URIRef(row.p)
35
+
36
+ if isinstance(row.o, Uri):
37
+
38
+ # Skip malformed URLs with spaces in
39
+ if " " in row.o:
40
+ continue
41
+
42
+ ov = rdflib.term.URIRef(row.o)
43
+
44
+ else:
45
+
46
+ ov = rdflib.term.Literal(row.o)
47
+
48
+ g.add((sv, pv, ov))
49
+
50
+ g.serialize(destination="output.ttl", format="turtle")
51
+
52
+ buf = io.BytesIO()
53
+
54
+ g.serialize(destination=buf, format="turtle")
55
+
56
+ sys.stdout.write(buf.getvalue().decode("utf-8"))
57
+
58
+
59
+ def main():
60
+
61
+ parser = argparse.ArgumentParser(
62
+ prog='tg-graph-to-turtle',
63
+ description=__doc__,
64
+ )
65
+
66
+ parser.add_argument(
67
+ '-u', '--api-url',
68
+ default=default_url,
69
+ help=f'API URL (default: {default_url})',
70
+ )
71
+
72
+ parser.add_argument(
73
+ '-U', '--user',
74
+ default=default_user,
75
+ help=f'User ID (default: {default_user})'
76
+ )
77
+
78
+ parser.add_argument(
79
+ '-C', '--collection',
80
+ default=default_collection,
81
+ help=f'Collection ID (default: {default_collection})'
82
+ )
83
+
84
+ args = parser.parse_args()
85
+
86
+ try:
87
+
88
+ show_graph(
89
+ url=args.api_url,
90
+ user=args.user,
91
+ collection=args.collection
92
+ )
93
+
94
+ except Exception as e:
95
+
96
+ print("Exception:", e, flush=True)
97
+
98
+ main()
99
+
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """
4
+ Initialises Pulsar with Trustgraph tenant / namespaces & policy.
5
+ """
6
+
7
+ import requests
8
+ import time
9
+ import argparse
10
+
11
+ default_pulsar_admin_url = "http://pulsar:8080"
12
+
13
+ def get_clusters(url):
14
+
15
+ print("Get clusters...", flush=True)
16
+
17
+ resp = requests.get(f"{url}/admin/v2/clusters")
18
+
19
+ if resp.status_code != 200: raise RuntimeError("Could not fetch clusters")
20
+
21
+ return resp.json()
22
+
23
+ def ensure_tenant(url, tenant, clusters):
24
+
25
+ resp = requests.get(f"{url}/admin/v2/tenants/{tenant}")
26
+
27
+ if resp.status_code == 200:
28
+ print(f"Tenant {tenant} already exists.", flush=True)
29
+ return
30
+
31
+ resp = requests.put(
32
+ f"{url}/admin/v2/tenants/{tenant}",
33
+ json={
34
+ "adminRoles": [],
35
+ "allowedClusters": clusters,
36
+ }
37
+ )
38
+
39
+ if resp.status_code != 204:
40
+ print(resp.text, flush=True)
41
+ raise RuntimeError("Tenant creation failed.")
42
+
43
+ print(f"Tenant {tenant} created.", flush=True)
44
+
45
+ def ensure_namespace(url, tenant, namespace, config):
46
+
47
+ resp = requests.get(f"{url}/admin/v2/namespaces/{tenant}/{namespace}")
48
+
49
+ if resp.status_code == 200:
50
+ print(f"Namespace {tenant}/{namespace} already exists.", flush=True)
51
+ return
52
+
53
+ resp = requests.put(
54
+ f"{url}/admin/v2/namespaces/{tenant}/{namespace}",
55
+ json=config,
56
+ )
57
+
58
+ if resp.status_code != 204:
59
+ print(resp.status_code, flush=True)
60
+ print(resp.text, flush=True)
61
+ raise RuntimeError(f"Namespace {tenant}/{namespace} creation failed.")
62
+
63
+ print(f"Namespace {tenant}/{namespace} created.", flush=True)
64
+
65
+ def init(url, tenant="tg"):
66
+
67
+ clusters = get_clusters(url)
68
+
69
+ ensure_tenant(url, tenant, clusters)
70
+
71
+ ensure_namespace(url, tenant, "flow", {})
72
+
73
+ ensure_namespace(url, tenant, "request", {})
74
+
75
+ ensure_namespace(url, tenant, "response", {
76
+ "retention_policies": {
77
+ "retentionSizeInMB": -1,
78
+ "retentionTimeInMinutes": 3,
79
+ }
80
+ })
81
+
82
+ def main():
83
+
84
+ parser = argparse.ArgumentParser(
85
+ prog='tg-init-pulsar',
86
+ description=__doc__,
87
+ )
88
+
89
+ parser.add_argument(
90
+ '-p', '--pulsar-admin-url',
91
+ default=default_pulsar_admin_url,
92
+ help=f'Pulsar admin URL (default: {default_pulsar_admin_url})',
93
+ )
94
+
95
+ args = parser.parse_args()
96
+
97
+ while True:
98
+
99
+ try:
100
+
101
+ print(flush=True)
102
+ print(
103
+ f"Initialising with Pulsar {args.pulsar_admin_url}...",
104
+ flush=True
105
+ )
106
+ init(args.pulsar_admin_url, "tg")
107
+ print("Initialisation complete.", flush=True)
108
+ break
109
+
110
+ except Exception as e:
111
+
112
+ print("Exception:", e, flush=True)
113
+
114
+ print("Sleeping...", flush=True)
115
+ time.sleep(2)
116
+ print("Will retry...", flush=True)
117
+
118
+ main()
119
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env bash
2
+
3
+ CSRF_TOKEN=$(curl http://localhost:7750/pulsar-manager/csrf-token)
4
+
5
+ curl \
6
+ -H "X-XSRF-TOKEN: $CSRF_TOKEN" \
7
+ -H "Cookie: XSRF-TOKEN=$CSRF_TOKEN;" \
8
+ -H 'Content-Type: application/json' \
9
+ -X PUT \
10
+ http://localhost:7750/pulsar-manager/users/superuser \
11
+ -d '{"name": "admin", "password": "apachepulsar", "description": "test", "email": "username@test.org"}'
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """
4
+ Uses the GraphRAG service to answer a question
5
+ """
6
+
7
+ import argparse
8
+ import os
9
+ import textwrap
10
+ import uuid
11
+ import asyncio
12
+ import json
13
+ from websockets.asyncio.client import connect
14
+
15
+ default_url = os.getenv("TRUSTGRAPH_URL", 'ws://localhost:8088/')
16
+ default_user = 'trustgraph'
17
+ default_collection = 'default'
18
+
19
+ def wrap(text, width=75):
20
+ if text is None: text = "n/a"
21
+ out = textwrap.wrap(
22
+ text, width=width
23
+ )
24
+ return "\n".join(out)
25
+
26
+ def output(text, prefix="> ", width=78):
27
+ out = textwrap.indent(
28
+ text, prefix=prefix
29
+ )
30
+ print(out)
31
+
32
+ async def question(
33
+ url, question, user, collection,
34
+ plan=None, state=None, verbose=False
35
+ ):
36
+
37
+ if not url.endswith("/"):
38
+ url += "/"
39
+
40
+ url = url + "api/v1/socket"
41
+
42
+ if verbose:
43
+ output(wrap(question), "\U00002753 ")
44
+ print()
45
+
46
+ def think(x):
47
+ if verbose:
48
+ output(wrap(x), "\U0001f914 ")
49
+ print()
50
+
51
+ def observe(x):
52
+ if verbose:
53
+ output(wrap(x), "\U0001f4a1 ")
54
+ print()
55
+
56
+ mid = str(uuid.uuid4())
57
+
58
+ async with connect(url) as ws:
59
+
60
+ req = json.dumps({
61
+ "id": mid,
62
+ "service": "agent",
63
+ "request": {
64
+ "question": question,
65
+ }
66
+
67
+ })
68
+
69
+ await ws.send(req)
70
+
71
+ while True:
72
+
73
+ msg = await ws.recv()
74
+
75
+ obj = json.loads(msg)
76
+
77
+ if obj["id"] != mid:
78
+ print("Ignore message")
79
+ continue
80
+
81
+ if "thought" in obj["response"]:
82
+ think(obj["response"]["thought"])
83
+
84
+ if "observation" in obj["response"]:
85
+ observe(obj["response"]["observation"])
86
+
87
+ if "answer" in obj["response"]:
88
+ print(obj["response"]["answer"])
89
+
90
+ if obj["complete"]: break
91
+
92
+ await ws.close()
93
+
94
+ def main():
95
+
96
+ parser = argparse.ArgumentParser(
97
+ prog='tg-invoke-agent',
98
+ description=__doc__,
99
+ )
100
+
101
+ parser.add_argument(
102
+ '-u', '--url',
103
+ default=default_url,
104
+ help=f'API URL (default: {default_url})',
105
+ )
106
+
107
+ parser.add_argument(
108
+ '-q', '--question',
109
+ required=True,
110
+ help=f'Question to answer',
111
+ )
112
+
113
+ parser.add_argument(
114
+ '-U', '--user',
115
+ default=default_user,
116
+ help=f'User ID (default: {default_user})'
117
+ )
118
+
119
+ parser.add_argument(
120
+ '-C', '--collection',
121
+ default=default_collection,
122
+ help=f'Collection ID (default: {default_collection})'
123
+ )
124
+
125
+ parser.add_argument(
126
+ '-l', '--plan',
127
+ help=f'Agent plan (default: unspecified)'
128
+ )
129
+
130
+ parser.add_argument(
131
+ '-s', '--state',
132
+ help=f'Agent initial state (default: unspecified)'
133
+ )
134
+
135
+ parser.add_argument(
136
+ '-v', '--verbose',
137
+ action="store_true",
138
+ help=f'Output thinking/observations'
139
+ )
140
+
141
+ args = parser.parse_args()
142
+
143
+ try:
144
+
145
+ asyncio.run(
146
+ question(
147
+ url=args.url,
148
+ question=args.question,
149
+ user=args.user,
150
+ collection=args.collection,
151
+ plan=args.plan,
152
+ state=args.state,
153
+ verbose=args.verbose,
154
+ )
155
+ )
156
+
157
+ except Exception as e:
158
+
159
+ print("Exception:", e, flush=True)
160
+
161
+ main()
162
+
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """
4
+ Uses the GraphRAG service to answer a question
5
+ """
6
+
7
+ import argparse
8
+ import os
9
+ from trustgraph.api import Api
10
+
11
+ default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
12
+ default_user = 'trustgraph'
13
+ default_collection = 'default'
14
+
15
+ def question(url, question, user, collection):
16
+
17
+ rag = Api(url)
18
+
19
+ # user=user, collection=collection,
20
+ resp = rag.document_rag(question=question)
21
+
22
+ print(resp)
23
+
24
+ def main():
25
+
26
+ parser = argparse.ArgumentParser(
27
+ prog='tg-invoke-document-rag',
28
+ description=__doc__,
29
+ )
30
+
31
+ parser.add_argument(
32
+ '-u', '--url',
33
+ default=default_url,
34
+ help=f'API URL (default: {default_url})',
35
+ )
36
+
37
+ parser.add_argument(
38
+ '-q', '--question',
39
+ required=True,
40
+ help=f'Question to answer',
41
+ )
42
+
43
+ parser.add_argument(
44
+ '-U', '--user',
45
+ default=default_user,
46
+ help=f'User ID (default: {default_user})'
47
+ )
48
+
49
+ parser.add_argument(
50
+ '-C', '--collection',
51
+ default=default_collection,
52
+ help=f'Collection ID (default: {default_collection})'
53
+ )
54
+
55
+ args = parser.parse_args()
56
+
57
+ try:
58
+
59
+ question(
60
+ url=args.url,
61
+ question=args.question,
62
+ user=args.user,
63
+ collection=args.collection,
64
+ )
65
+
66
+ except Exception as e:
67
+
68
+ print("Exception:", e, flush=True)
69
+
70
+ main()
71
+