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,44 @@
1
+ from typing import Callable, Any
2
+
3
+ class LazyLoader:
4
+ loaded : bool
5
+ loader : Callable
6
+ value : Any
7
+
8
+ def __init__(self, loader: Callable):
9
+ self.loaded = False
10
+ self.loader = loader
11
+ self.value = None
12
+
13
+ def is_loaded(self):
14
+ return self.loaded
15
+
16
+ def __getattr__(self, name):
17
+ if not self.loaded:
18
+ self.value = self.loader()
19
+ self.loaded = True
20
+ return getattr(self.value, name)
21
+
22
+ def __iter__(self):
23
+ if not self.loaded:
24
+ self.value = self.loader()
25
+ self.loaded = True
26
+ return iter(self.value)
27
+
28
+ def __len__(self):
29
+ if not self.loaded:
30
+ self.value = self.loader()
31
+ self.loaded = True
32
+ return len(self.value)
33
+
34
+ def __getitem__(self, key):
35
+ if not self.loaded:
36
+ self.value = self.loader()
37
+ self.loaded = True
38
+ return self.value[key]
39
+
40
+ def __repr__(self):
41
+ if not self.loaded:
42
+ self.value = self.loader()
43
+ self.loaded = True
44
+ return repr(self.value)
@@ -0,0 +1,12 @@
1
+ import os
2
+ import sys
3
+
4
+ from loguru import logger
5
+
6
+
7
+ def reconfigure(level: str = "DEBUG"):
8
+ logger.remove()
9
+ logger.add(sys.stderr, level=level)
10
+
11
+
12
+ reconfigure(os.environ.get("LOG_LEVEL", "WARNING"))
@@ -0,0 +1,141 @@
1
+ from rdflib import Graph, RDF, RDFS, URIRef, Literal
2
+ import rdflib
3
+ import re
4
+ import uuid
5
+ from typing import List, Dict, Tuple
6
+
7
+
8
+ class OntologyReasoner:
9
+ def is_iri_uuid(self, iri: URIRef) -> bool:
10
+ # Split by / and get last element then split by # and get last element
11
+ last_element = iri.split("/")[-1].split("#")[-1]
12
+
13
+ # Check if last element is a UUID
14
+ return (
15
+ re.match(
16
+ r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
17
+ last_element,
18
+ )
19
+ is not None
20
+ )
21
+
22
+ def dedup_subject(self, class_label: tuple, graph: Graph) -> Graph:
23
+ cls, label = class_label
24
+ # Search all IRI in graph using SPARQL where p = rdf:type and o = cls and p = rdfs:label and o = label
25
+ query = """
26
+ SELECT ?s WHERE {
27
+ ?s a <%s> .
28
+ ?s rdfs:label "%s" .
29
+ FILTER NOT EXISTS { ?other rdfs:subClassOf ?s }
30
+ FILTER NOT EXISTS { ?s a owl:Class }
31
+ FILTER NOT EXISTS { ?s a rdfs:Class }
32
+ }
33
+ """ % (cls, label)
34
+ # print(query)
35
+ results: rdflib.query.Result = graph.query(query)
36
+
37
+ new_graph = Graph()
38
+
39
+ def filter_func(x: rdflib.query.ResultRow) -> bool:
40
+ return (
41
+ self.is_iri_uuid(x[0])
42
+ if isinstance(x, rdflib.query.ResultRow) and isinstance(x[0], URIRef)
43
+ else False
44
+ )
45
+
46
+ main_node: List[rdflib.query.ResultRow] = list(
47
+ filter(
48
+ filter_func,
49
+ [
50
+ result
51
+ for result in results
52
+ if isinstance(result, rdflib.query.ResultRow)
53
+ ],
54
+ )
55
+ )
56
+
57
+ main_node_uri: str = ""
58
+ if len(main_node) == 0:
59
+ # Create new IRI UUID
60
+ main_node_uri = f"http://ontology.naas.ai/abi/{uuid.uuid4()}"
61
+
62
+ # Add new node to graph
63
+ new_graph.add((URIRef(main_node_uri), RDF.type, URIRef(cls)))
64
+ new_graph.add((URIRef(main_node_uri), RDFS.label, Literal(label)))
65
+ else:
66
+ main_node_uri = str(main_node[0][0])
67
+
68
+ excluded_iris = []
69
+
70
+ def not_filter_func(x: rdflib.query.ResultRow) -> bool:
71
+ return not filter_func(x)
72
+
73
+ # For each result, we need to get all triples and add them to the new node then remove existing triples
74
+ for result in list(
75
+ filter(
76
+ not_filter_func,
77
+ [
78
+ result
79
+ for result in results
80
+ if isinstance(result, rdflib.query.ResultRow)
81
+ ],
82
+ )
83
+ ):
84
+ existing_iri = result[0]
85
+ excluded_iris.append(existing_iri)
86
+ for s, p, o in graph:
87
+ # Add existing triples to the new node.
88
+ if s == existing_iri and p != RDF.type:
89
+ new_graph.add((URIRef(main_node_uri), p, o))
90
+
91
+ # Update existing references to the new node.
92
+ elif o == existing_iri:
93
+ new_graph.add((s, p, URIRef(main_node_uri)))
94
+
95
+ for s, p, o in graph:
96
+ if s not in excluded_iris and o not in excluded_iris:
97
+ new_graph.add((s, p, o))
98
+
99
+ return new_graph
100
+
101
+ def dedup_ttl(self, ttl: str) -> Tuple[str, Graph]:
102
+ graph = Graph()
103
+ graph.parse(data=ttl, format="turtle")
104
+
105
+ types: Dict[URIRef, URIRef] = {}
106
+ labels: Dict[URIRef, str] = {}
107
+ types_label: Dict[Tuple[URIRef, str], List[URIRef]] = {}
108
+
109
+ pn = graph.namespaces()
110
+
111
+ # Look for triples that have same label and same type
112
+ for s, p, o in graph:
113
+ assert (
114
+ isinstance(s, URIRef)
115
+ and isinstance(p, URIRef)
116
+ and isinstance(o, URIRef)
117
+ )
118
+
119
+ if p == RDF.type:
120
+ types[s] = o
121
+ if p == RDFS.label:
122
+ labels[s] = str(o)
123
+
124
+ for st in types:
125
+ t = types[st]
126
+ if st in labels:
127
+ key = (t, labels[st])
128
+ if key not in types_label:
129
+ types_label[key] = []
130
+ types_label[key].append(st)
131
+
132
+ for key in types_label:
133
+ if len(types_label[key]) > 1:
134
+ graph = self.dedup_subject(key, graph)
135
+
136
+ for prefix, namespace in pn:
137
+ graph.bind(prefix, namespace)
138
+
139
+ return graph.serialize(
140
+ format="turtle", namespace_manager=graph.namespace_manager
141
+ ), graph