naas-abi-core 1.4.1__py3-none-any.whl

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.
Files changed (124) hide show
  1. assets/favicon.ico +0 -0
  2. assets/logo.png +0 -0
  3. naas_abi_core/__init__.py +1 -0
  4. naas_abi_core/apps/api/api.py +245 -0
  5. naas_abi_core/apps/api/api_test.py +281 -0
  6. naas_abi_core/apps/api/openapi_doc.py +144 -0
  7. naas_abi_core/apps/mcp/Dockerfile.mcp +35 -0
  8. naas_abi_core/apps/mcp/mcp_server.py +243 -0
  9. naas_abi_core/apps/mcp/mcp_server_test.py +163 -0
  10. naas_abi_core/apps/terminal_agent/main.py +555 -0
  11. naas_abi_core/apps/terminal_agent/terminal_style.py +175 -0
  12. naas_abi_core/engine/Engine.py +87 -0
  13. naas_abi_core/engine/EngineProxy.py +109 -0
  14. naas_abi_core/engine/Engine_test.py +6 -0
  15. naas_abi_core/engine/IEngine.py +91 -0
  16. naas_abi_core/engine/conftest.py +45 -0
  17. naas_abi_core/engine/engine_configuration/EngineConfiguration.py +216 -0
  18. naas_abi_core/engine/engine_configuration/EngineConfiguration_Deploy.py +7 -0
  19. naas_abi_core/engine/engine_configuration/EngineConfiguration_GenericLoader.py +49 -0
  20. naas_abi_core/engine/engine_configuration/EngineConfiguration_ObjectStorageService.py +159 -0
  21. naas_abi_core/engine/engine_configuration/EngineConfiguration_ObjectStorageService_test.py +26 -0
  22. naas_abi_core/engine/engine_configuration/EngineConfiguration_SecretService.py +138 -0
  23. naas_abi_core/engine/engine_configuration/EngineConfiguration_SecretService_test.py +74 -0
  24. naas_abi_core/engine/engine_configuration/EngineConfiguration_TripleStoreService.py +224 -0
  25. naas_abi_core/engine/engine_configuration/EngineConfiguration_TripleStoreService_test.py +109 -0
  26. naas_abi_core/engine/engine_configuration/EngineConfiguration_VectorStoreService.py +76 -0
  27. naas_abi_core/engine/engine_configuration/EngineConfiguration_VectorStoreService_test.py +33 -0
  28. naas_abi_core/engine/engine_configuration/EngineConfiguration_test.py +9 -0
  29. naas_abi_core/engine/engine_configuration/utils/PydanticModelValidator.py +15 -0
  30. naas_abi_core/engine/engine_loaders/EngineModuleLoader.py +302 -0
  31. naas_abi_core/engine/engine_loaders/EngineOntologyLoader.py +16 -0
  32. naas_abi_core/engine/engine_loaders/EngineServiceLoader.py +47 -0
  33. naas_abi_core/integration/__init__.py +7 -0
  34. naas_abi_core/integration/integration.py +28 -0
  35. naas_abi_core/models/Model.py +198 -0
  36. naas_abi_core/models/OpenRouter.py +18 -0
  37. naas_abi_core/models/OpenRouter_test.py +36 -0
  38. naas_abi_core/module/Module.py +252 -0
  39. naas_abi_core/module/ModuleAgentLoader.py +50 -0
  40. naas_abi_core/module/ModuleUtils.py +20 -0
  41. naas_abi_core/modules/templatablesparqlquery/README.md +196 -0
  42. naas_abi_core/modules/templatablesparqlquery/__init__.py +39 -0
  43. naas_abi_core/modules/templatablesparqlquery/ontologies/TemplatableSparqlQueryOntology.ttl +116 -0
  44. naas_abi_core/modules/templatablesparqlquery/workflows/GenericWorkflow.py +48 -0
  45. naas_abi_core/modules/templatablesparqlquery/workflows/TemplatableSparqlQueryLoader.py +192 -0
  46. naas_abi_core/pipeline/__init__.py +6 -0
  47. naas_abi_core/pipeline/pipeline.py +70 -0
  48. naas_abi_core/services/__init__.py +0 -0
  49. naas_abi_core/services/agent/Agent.py +1619 -0
  50. naas_abi_core/services/agent/AgentMemory_test.py +28 -0
  51. naas_abi_core/services/agent/Agent_test.py +214 -0
  52. naas_abi_core/services/agent/IntentAgent.py +1179 -0
  53. naas_abi_core/services/agent/IntentAgent_test.py +139 -0
  54. naas_abi_core/services/agent/beta/Embeddings.py +181 -0
  55. naas_abi_core/services/agent/beta/IntentMapper.py +120 -0
  56. naas_abi_core/services/agent/beta/LocalModel.py +88 -0
  57. naas_abi_core/services/agent/beta/VectorStore.py +89 -0
  58. naas_abi_core/services/agent/test_agent_memory.py +278 -0
  59. naas_abi_core/services/agent/test_postgres_integration.py +145 -0
  60. naas_abi_core/services/cache/CacheFactory.py +31 -0
  61. naas_abi_core/services/cache/CachePort.py +63 -0
  62. naas_abi_core/services/cache/CacheService.py +246 -0
  63. naas_abi_core/services/cache/CacheService_test.py +85 -0
  64. naas_abi_core/services/cache/adapters/secondary/CacheFSAdapter.py +39 -0
  65. naas_abi_core/services/object_storage/ObjectStorageFactory.py +57 -0
  66. naas_abi_core/services/object_storage/ObjectStoragePort.py +47 -0
  67. naas_abi_core/services/object_storage/ObjectStorageService.py +41 -0
  68. naas_abi_core/services/object_storage/adapters/secondary/ObjectStorageSecondaryAdapterFS.py +52 -0
  69. naas_abi_core/services/object_storage/adapters/secondary/ObjectStorageSecondaryAdapterNaas.py +131 -0
  70. naas_abi_core/services/object_storage/adapters/secondary/ObjectStorageSecondaryAdapterS3.py +171 -0
  71. naas_abi_core/services/ontology/OntologyPorts.py +36 -0
  72. naas_abi_core/services/ontology/OntologyService.py +17 -0
  73. naas_abi_core/services/ontology/adaptors/secondary/OntologyService_SecondaryAdaptor_NERPort.py +37 -0
  74. naas_abi_core/services/secret/Secret.py +138 -0
  75. naas_abi_core/services/secret/SecretPorts.py +45 -0
  76. naas_abi_core/services/secret/Secret_test.py +65 -0
  77. naas_abi_core/services/secret/adaptors/secondary/Base64Secret.py +57 -0
  78. naas_abi_core/services/secret/adaptors/secondary/Base64Secret_test.py +39 -0
  79. naas_abi_core/services/secret/adaptors/secondary/NaasSecret.py +88 -0
  80. naas_abi_core/services/secret/adaptors/secondary/NaasSecret_test.py +25 -0
  81. naas_abi_core/services/secret/adaptors/secondary/dotenv_secret_secondaryadaptor.py +29 -0
  82. naas_abi_core/services/triple_store/TripleStoreFactory.py +116 -0
  83. naas_abi_core/services/triple_store/TripleStorePorts.py +223 -0
  84. naas_abi_core/services/triple_store/TripleStoreService.py +419 -0
  85. naas_abi_core/services/triple_store/adaptors/secondary/AWSNeptune.py +1300 -0
  86. naas_abi_core/services/triple_store/adaptors/secondary/AWSNeptune_test.py +284 -0
  87. naas_abi_core/services/triple_store/adaptors/secondary/Oxigraph.py +597 -0
  88. naas_abi_core/services/triple_store/adaptors/secondary/Oxigraph_test.py +1474 -0
  89. naas_abi_core/services/triple_store/adaptors/secondary/TripleStoreService__SecondaryAdaptor__Filesystem.py +223 -0
  90. naas_abi_core/services/triple_store/adaptors/secondary/TripleStoreService__SecondaryAdaptor__ObjectStorage.py +234 -0
  91. naas_abi_core/services/triple_store/adaptors/secondary/base/TripleStoreService__SecondaryAdaptor__FileBase.py +18 -0
  92. naas_abi_core/services/vector_store/IVectorStorePort.py +101 -0
  93. naas_abi_core/services/vector_store/IVectorStorePort_test.py +189 -0
  94. naas_abi_core/services/vector_store/VectorStoreFactory.py +47 -0
  95. naas_abi_core/services/vector_store/VectorStoreService.py +171 -0
  96. naas_abi_core/services/vector_store/VectorStoreService_test.py +185 -0
  97. naas_abi_core/services/vector_store/__init__.py +13 -0
  98. naas_abi_core/services/vector_store/adapters/QdrantAdapter.py +251 -0
  99. naas_abi_core/services/vector_store/adapters/QdrantAdapter_test.py +57 -0
  100. naas_abi_core/tests/test_services_imports.py +69 -0
  101. naas_abi_core/utils/Expose.py +55 -0
  102. naas_abi_core/utils/Graph.py +182 -0
  103. naas_abi_core/utils/JSON.py +49 -0
  104. naas_abi_core/utils/LazyLoader.py +44 -0
  105. naas_abi_core/utils/Logger.py +12 -0
  106. naas_abi_core/utils/OntologyReasoner.py +141 -0
  107. naas_abi_core/utils/OntologyYaml.py +681 -0
  108. naas_abi_core/utils/SPARQL.py +256 -0
  109. naas_abi_core/utils/Storage.py +33 -0
  110. naas_abi_core/utils/StorageUtils.py +398 -0
  111. naas_abi_core/utils/String.py +52 -0
  112. naas_abi_core/utils/Workers.py +114 -0
  113. naas_abi_core/utils/__init__.py +0 -0
  114. naas_abi_core/utils/onto2py/README.md +0 -0
  115. naas_abi_core/utils/onto2py/__init__.py +10 -0
  116. naas_abi_core/utils/onto2py/__main__.py +29 -0
  117. naas_abi_core/utils/onto2py/onto2py.py +611 -0
  118. naas_abi_core/utils/onto2py/tests/ttl2py_test.py +271 -0
  119. naas_abi_core/workflow/__init__.py +5 -0
  120. naas_abi_core/workflow/workflow.py +48 -0
  121. naas_abi_core-1.4.1.dist-info/METADATA +630 -0
  122. naas_abi_core-1.4.1.dist-info/RECORD +124 -0
  123. naas_abi_core-1.4.1.dist-info/WHEEL +4 -0
  124. naas_abi_core-1.4.1.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,284 @@
1
+ from typing import Any
2
+
3
+ import pytest
4
+ from dotenv import load_dotenv
5
+ from naas_abi_core.services.triple_store.adaptors.secondary.AWSNeptune import (
6
+ AWSNeptune,
7
+ AWSNeptuneSSHTunnel,
8
+ )
9
+
10
+ load_dotenv()
11
+
12
+
13
+ @pytest.fixture
14
+ def aws_neptune():
15
+ import os
16
+
17
+ return AWSNeptuneSSHTunnel(
18
+ aws_region_name=os.environ.get("AWS_REGION"),
19
+ aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"),
20
+ aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY"),
21
+ db_instance_identifier=os.environ.get("AWS_NEPTUNE_DB_INSTANCE_IDENTIFIER"),
22
+ bastion_host=os.environ.get("AWS_BASTION_HOST"),
23
+ bastion_port=int(os.environ.get("AWS_BASTION_PORT")),
24
+ bastion_user=os.environ.get("AWS_BASTION_USER"),
25
+ bastion_private_key=os.environ.get("AWS_BASTION_PRIVATE_KEY"),
26
+ )
27
+
28
+
29
+ # def test_get(aws_neptune):
30
+ # graph = aws_neptune.get()
31
+ # assert graph is not None
32
+ # assert graph.serialize(format='turtle') is not None
33
+ # assert graph.query("select ?s ?p ?o where {?s ?p ?o}") is not None
34
+
35
+
36
+ def test_graph_to_insert_query(aws_neptune):
37
+ import rdflib
38
+ from naas_abi_core.services.triple_store.adaptors.secondary.AWSNeptune import (
39
+ NEPTUNE_DEFAULT_GRAPH_NAME,
40
+ QueryType,
41
+ )
42
+
43
+ # from rdflib.namespace import _NAMESPACE_PREFIXES_RDFLIB, _NAMESPACE_PREFIXES_CORE
44
+
45
+ graph = rdflib.Graph()
46
+
47
+ graph.bind("test", "https://test.com/")
48
+ graph.add(
49
+ (
50
+ rdflib.URIRef("https://test.com/s"),
51
+ rdflib.URIRef("https://test.com/p"),
52
+ rdflib.URIRef("https://test.com/o"),
53
+ )
54
+ )
55
+
56
+ insert_statement = aws_neptune.graph_to_query(
57
+ graph, QueryType.INSERT_DATA, graph_name=NEPTUNE_DEFAULT_GRAPH_NAME
58
+ )
59
+
60
+ # PREFIXES = "\n".join(
61
+ # [
62
+ # f"""PREFIX {prefix}: <{namespace}>"""
63
+ # for prefix, namespace in list(_NAMESPACE_PREFIXES_RDFLIB.items())
64
+ # + list(_NAMESPACE_PREFIXES_CORE.items())
65
+ # ]
66
+ # )
67
+
68
+ expected_insert_statement = f"""PREFIX test: <https://test.com/>
69
+
70
+ INSERT DATA {{ GRAPH <{str(NEPTUNE_DEFAULT_GRAPH_NAME)}> {{
71
+ <https://test.com/s> <https://test.com/p> <https://test.com/o> .
72
+ }}}}"""
73
+
74
+ assert insert_statement == expected_insert_statement, (
75
+ insert_statement,
76
+ expected_insert_statement,
77
+ )
78
+
79
+
80
+ def test_graph_management(aws_neptune: AWSNeptune):
81
+ import uuid
82
+
83
+ import rdflib
84
+
85
+ left_graph_uuid = uuid.uuid4()
86
+ right_graph_uuid = uuid.uuid4()
87
+ left_graph_name = rdflib.URIRef(f"https://test.com/left/{left_graph_uuid}")
88
+ right_graph_name = rdflib.URIRef(f"https://test.com/right/{right_graph_uuid}")
89
+
90
+ left_graph = rdflib.Graph()
91
+ right_graph = rdflib.Graph()
92
+
93
+ left_graph.add(
94
+ (
95
+ rdflib.URIRef(f"https://test.com/left/{left_graph_uuid}s"),
96
+ rdflib.URIRef(f"https://test.com/left/{left_graph_uuid}p"),
97
+ rdflib.URIRef(f"https://test.com/left/{left_graph_uuid}o"),
98
+ )
99
+ )
100
+ right_graph.add(
101
+ (
102
+ rdflib.URIRef(f"https://test.com/right/{right_graph_uuid}s"),
103
+ rdflib.URIRef(f"https://test.com/right/{right_graph_uuid}p"),
104
+ rdflib.URIRef(f"https://test.com/right/{right_graph_uuid}o"),
105
+ )
106
+ )
107
+
108
+ aws_neptune.insert(right_graph)
109
+
110
+ aws_neptune.create_graph(left_graph_name)
111
+ aws_neptune.create_graph(right_graph_name)
112
+
113
+ aws_neptune.insert(left_graph, left_graph_name)
114
+ aws_neptune.insert(right_graph, right_graph_name)
115
+
116
+ assert (
117
+ len(
118
+ list(
119
+ aws_neptune.query(
120
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(left_graph_name)}> {{ ?s ?p ?o }} }}"""
121
+ )
122
+ )
123
+ )
124
+ == 1
125
+ )
126
+ assert (
127
+ len(
128
+ list(
129
+ aws_neptune.query(
130
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(right_graph_name)}> {{ ?s ?p ?o }} }}"""
131
+ )
132
+ )
133
+ )
134
+ == 1
135
+ )
136
+
137
+ graph_list = aws_neptune.query("SELECT ?g WHERE { GRAPH ?g {} }")
138
+
139
+ def _filter(row: Any) -> bool:
140
+ assert isinstance(row, rdflib.query.ResultRow)
141
+ return str(row[0]) in [str(left_graph_name), str(right_graph_name)]
142
+
143
+ assert len(list(filter(_filter, list(graph_list)))) == 2
144
+
145
+ aws_neptune.add_graph_to_graph(left_graph_name, right_graph_name)
146
+
147
+ assert (
148
+ len(
149
+ list(
150
+ aws_neptune.query(
151
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(left_graph_name)}> {{ ?s ?p ?o }} }}"""
152
+ )
153
+ )
154
+ )
155
+ == 1
156
+ )
157
+ assert (
158
+ len(
159
+ list(
160
+ aws_neptune.query(
161
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(right_graph_name)}> {{ ?s ?p ?o }} }}"""
162
+ )
163
+ )
164
+ )
165
+ == 2
166
+ )
167
+
168
+ aws_neptune.copy_graph(left_graph_name, right_graph_name)
169
+
170
+ assert (
171
+ len(
172
+ list(
173
+ aws_neptune.query(
174
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(left_graph_name)}> {{ ?s ?p ?o }} }}"""
175
+ )
176
+ )
177
+ )
178
+ == 1
179
+ )
180
+ assert (
181
+ len(
182
+ list(
183
+ aws_neptune.query(
184
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(right_graph_name)}> {{ ?s ?p ?o }} }}"""
185
+ )
186
+ )
187
+ )
188
+ == 1
189
+ )
190
+
191
+ aws_neptune.remove(left_graph, left_graph_name)
192
+
193
+ assert (
194
+ len(
195
+ list(
196
+ aws_neptune.query(
197
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(left_graph_name)}> {{ ?s ?p ?o }} }}"""
198
+ )
199
+ )
200
+ )
201
+ == 0
202
+ )
203
+
204
+ aws_neptune.clear_graph(left_graph_name)
205
+ aws_neptune.clear_graph(right_graph_name)
206
+
207
+ assert (
208
+ len(
209
+ list(
210
+ aws_neptune.query(
211
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(left_graph_name)}> {{ ?s ?p ?o }} }}"""
212
+ )
213
+ )
214
+ )
215
+ == 0
216
+ )
217
+ assert (
218
+ len(
219
+ list(
220
+ aws_neptune.query(
221
+ f"""SELECT ?s ?p ?o WHERE {{ GRAPH <{str(right_graph_name)}> {{ ?s ?p ?o }} }}"""
222
+ )
223
+ )
224
+ )
225
+ == 0
226
+ )
227
+
228
+ aws_neptune.drop_graph(left_graph_name)
229
+ aws_neptune.drop_graph(right_graph_name)
230
+
231
+ graph_list = aws_neptune.query("SELECT ?g WHERE { GRAPH ?g {} }")
232
+
233
+ assert len(list(filter(_filter, list(graph_list)))) == 0
234
+
235
+
236
+ def test_AWSNeptune(aws_neptune):
237
+ import uuid
238
+
239
+ import rdflib
240
+
241
+ graph = rdflib.Graph()
242
+
243
+ prefix = f"https://test.com/{uuid.uuid4()}"
244
+
245
+ graph.bind("test", prefix)
246
+ graph.add(
247
+ (
248
+ rdflib.URIRef(f"{prefix}s"),
249
+ rdflib.URIRef(f"{prefix}p"),
250
+ rdflib.URIRef(f"{prefix}o"),
251
+ )
252
+ )
253
+
254
+ # Testing insert
255
+
256
+ aws_neptune.insert(graph)
257
+
258
+ query = f"""PREFIX test: <{prefix}>
259
+
260
+ SELECT ?s ?p ?o WHERE {{
261
+ <{prefix}s> ?p ?o
262
+ }}"""
263
+
264
+ # Testing query
265
+
266
+ query_result = aws_neptune.query(query)
267
+
268
+ assert len(list(query_result)) == 1, query_result.serialize(format="json")
269
+
270
+ for row in query_result:
271
+ s, p, o = row
272
+ assert s is None, row
273
+ assert str(p) == f"{prefix}p", row
274
+ assert str(o) == f"{prefix}o", row
275
+
276
+ # Testing get_subject_graph
277
+
278
+ subject_graph = aws_neptune.get_subject_graph(rdflib.URIRef(f"{prefix}s"))
279
+ assert len(list(subject_graph)) == 1, subject_graph.serialize(format="json")
280
+
281
+ for s, p, o in subject_graph:
282
+ assert str(s) == f"{prefix}s", subject_graph
283
+ assert str(p) == f"{prefix}p", subject_graph
284
+ assert str(o) == f"{prefix}o", subject_graph