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,48 @@
1
+ from typing import Generic, Type, TypeVar
2
+
3
+ from langchain_core.tools import BaseTool, StructuredTool
4
+ from naas_abi_core.services.triple_store.TripleStoreService import TripleStoreService
5
+ from naas_abi_core.utils.SPARQL import SPARQLUtils
6
+ from pydantic import BaseModel
7
+
8
+ T = TypeVar("T", bound=BaseModel)
9
+
10
+
11
+ class GenericWorkflow(Generic[T]):
12
+ def __init__(
13
+ self,
14
+ name: str,
15
+ description: str,
16
+ sparql_template: str,
17
+ arguments_model: Type[T],
18
+ triple_store_service: TripleStoreService,
19
+ ):
20
+ self.name = name
21
+ self.description = description
22
+ self.sparql_template = sparql_template
23
+ self.arguments_model = arguments_model
24
+ self.triple_store_service = triple_store_service
25
+
26
+ def run(self, parameters: T):
27
+ try:
28
+ # Template the sparql template with the parameters using jinja2
29
+ from jinja2 import Template
30
+
31
+ template = Template(self.sparql_template)
32
+ sparql_query = template.render(parameters.model_dump())
33
+ # print(sparql_query)
34
+ results = self.triple_store_service.query(sparql_query)
35
+
36
+ return SPARQLUtils(self.triple_store_service).results_to_list(results)
37
+ except Exception as e:
38
+ return [{"error": str(e)}]
39
+
40
+ def as_tools(self) -> list[BaseTool]:
41
+ return [
42
+ StructuredTool(
43
+ name=self.name,
44
+ description=self.description,
45
+ func=lambda **kwargs: self.run(self.arguments_model(**kwargs)),
46
+ args_schema=self.arguments_model,
47
+ )
48
+ ]
@@ -0,0 +1,192 @@
1
+ import asyncio
2
+
3
+ from naas_abi_core import logger
4
+ from naas_abi_core.services.triple_store.TripleStoreService import TripleStoreService
5
+ from pydantic import Field, create_model
6
+ from rdflib import RDF, Graph, URIRef
7
+
8
+ from .GenericWorkflow import GenericWorkflow
9
+
10
+
11
+ async def async_asyncio_thread_jobs(jobs):
12
+ tasks = []
13
+ for job in jobs:
14
+ task = asyncio.create_task(asyncio.to_thread(*job))
15
+ tasks.append(task)
16
+ return await asyncio.gather(*tasks)
17
+
18
+
19
+ def asyncio_thread_job(jobs):
20
+ return asyncio.run(async_asyncio_thread_jobs(jobs))
21
+
22
+
23
+ class TemplatableSparqlQueryLoader:
24
+ triple_store_service: TripleStoreService
25
+
26
+ def __init__(self, triple_store_service: TripleStoreService):
27
+ self.triple_store_service = triple_store_service
28
+
29
+ def templatable_queries(self):
30
+ results = self.triple_store_service.query("""
31
+ PREFIX intentMapping: <http://ontology.naas.ai/intentMapping/>
32
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
33
+ SELECT ?query ?label ?description ?sparqlTemplate ?hasArgument
34
+ WHERE {
35
+ ?query a intentMapping:TemplatableSparqlQuery ;
36
+ intentMapping:intentDescription ?description ;
37
+ intentMapping:sparqlTemplate ?sparqlTemplate ;
38
+ intentMapping:hasArgument ?hasArgument ;
39
+ rdfs:label ?label .
40
+ }
41
+ """)
42
+
43
+ queries = {}
44
+
45
+ for result in results:
46
+ query, label, description, sparqlTemplate, hasArgument = result
47
+ queries[query] = {
48
+ "label": label,
49
+ "description": description,
50
+ "sparqlTemplate": sparqlTemplate,
51
+ "hasArgument": [hasArgument]
52
+ if (query not in queries or queries[query].get("hasArgument") is None)
53
+ else queries[query].get("hasArgument") + [hasArgument],
54
+ }
55
+
56
+ arguments = {}
57
+
58
+ argument_graph = Graph()
59
+ argument_graph.bind(
60
+ "intentMapping", URIRef("http://ontology.naas.ai/intentMapping/")
61
+ )
62
+ results = self.triple_store_service.query("""
63
+ PREFIX intentMapping: <http://ontology.naas.ai/intentMapping/>
64
+
65
+ SELECT ?argument ?name ?description ?validationPattern ?validationFormat
66
+ WHERE {
67
+ ?argument a intentMapping:QueryArgument ;
68
+ intentMapping:argumentName ?name ;
69
+ intentMapping:argumentDescription ?description ;
70
+ intentMapping:validationPattern ?validationPattern ;
71
+ intentMapping:validationFormat ?validationFormat .
72
+ }
73
+ """)
74
+
75
+ for argument, name, description, validationPattern, validationFormat in results:
76
+ argument_graph.add(
77
+ (
78
+ argument,
79
+ RDF.type,
80
+ URIRef("http://ontology.naas.ai/intentMapping/QueryArgument"),
81
+ )
82
+ )
83
+ argument_graph.add(
84
+ (
85
+ argument,
86
+ URIRef("http://ontology.naas.ai/intentMapping/argumentName"),
87
+ name,
88
+ )
89
+ )
90
+ argument_graph.add(
91
+ (
92
+ argument,
93
+ URIRef("http://ontology.naas.ai/intentMapping/argumentDescription"),
94
+ description,
95
+ )
96
+ )
97
+ argument_graph.add(
98
+ (
99
+ argument,
100
+ URIRef("http://ontology.naas.ai/intentMapping/validationPattern"),
101
+ validationPattern,
102
+ )
103
+ )
104
+ argument_graph.add(
105
+ (
106
+ argument,
107
+ URIRef("http://ontology.naas.ai/intentMapping/validationFormat"),
108
+ validationFormat,
109
+ )
110
+ )
111
+
112
+ arguments = {}
113
+ for templatableQuery in queries:
114
+ for argument in queries[templatableQuery].get("hasArgument"):
115
+ q = (
116
+ """
117
+ PREFIX intentMapping: <http://ontology.naas.ai/intentMapping/>
118
+
119
+ SELECT ?argument ?name ?description ?validationPattern ?validationFormat
120
+ WHERE {
121
+ BIND(<"""
122
+ + str(argument)
123
+ + """> AS ?argument)
124
+ ?argument a intentMapping:QueryArgument ;
125
+ intentMapping:argumentName ?name ;
126
+ intentMapping:argumentDescription ?description ;
127
+ intentMapping:validationPattern ?validationPattern ;
128
+ intentMapping:validationFormat ?validationFormat .
129
+ }
130
+ """
131
+ )
132
+ # logger.debug(f"Query: {q}")
133
+ results = argument_graph.query(q)
134
+
135
+ for result in results:
136
+ argument, name, description, validationPattern, validationFormat = (
137
+ result
138
+ )
139
+
140
+ arguments[argument] = {
141
+ "name": name,
142
+ "description": description,
143
+ "validationPattern": validationPattern,
144
+ "validationFormat": validationFormat,
145
+ }
146
+
147
+ return queries, arguments
148
+
149
+ def load_workflows(self):
150
+ workflows = []
151
+
152
+ queries, arguments = self.templatable_queries()
153
+
154
+ # workflows = asyncio.run(__load_queries(queries, arguments))
155
+
156
+ # Now for each query, we need to create a Pydantic BaseModel based on the arguments
157
+ for _query in queries:
158
+ try:
159
+ query = queries[_query]
160
+
161
+ # Arguments Model with validation patterns
162
+ arguments_model = create_model(
163
+ f"{str(query['label']).capitalize()}Arguments",
164
+ **{
165
+ str(arguments[argument]["name"]): (
166
+ str,
167
+ Field(
168
+ ...,
169
+ description=str(arguments[argument]["description"]),
170
+ pattern=str(arguments[argument]["validationPattern"]),
171
+ # You could also add additional metadata from validationFormat if needed
172
+ example=str(arguments[argument]["validationFormat"]),
173
+ ),
174
+ )
175
+ for argument in query.get("hasArgument")
176
+ },
177
+ )
178
+
179
+ p = GenericWorkflow[arguments_model](
180
+ str(query["label"]),
181
+ str(query["description"]),
182
+ str(query["sparqlTemplate"]),
183
+ arguments_model,
184
+ self.triple_store_service,
185
+ )
186
+ workflows.append(p)
187
+ except Exception as e:
188
+ logger.warning(
189
+ f"Error loading workflow for query {query['label']}\nMessage: {e}"
190
+ )
191
+
192
+ return workflows
@@ -0,0 +1,6 @@
1
+ from naas_abi_core.pipeline.pipeline import Graph as Graph
2
+ from naas_abi_core.pipeline.pipeline import Pipeline as Pipeline
3
+ from naas_abi_core.pipeline.pipeline import (
4
+ PipelineConfiguration as PipelineConfiguration,
5
+ )
6
+ from naas_abi_core.pipeline.pipeline import PipelineParameters as PipelineParameters
@@ -0,0 +1,70 @@
1
+ from abc import abstractmethod
2
+ from dataclasses import dataclass
3
+ from typing import Any
4
+
5
+ from pydantic import BaseModel
6
+ from rdflib import Graph
7
+
8
+ from naas_abi_core.services.triple_store.TripleStorePorts import OntologyEvent
9
+ from naas_abi_core.utils.Expose import Expose
10
+
11
+
12
+ @dataclass
13
+ class PipelineConfiguration:
14
+ """Base configuration class for pipelines.
15
+
16
+ This class serves as the base configuration for all pipeline classes.
17
+ Concrete pipeline implementations should extend this class and add their
18
+ specific configuration attributes.
19
+ """
20
+
21
+ pass
22
+
23
+
24
+ class PipelineParameters(BaseModel):
25
+ """Base parameters class for pipeline execution.
26
+
27
+ This class serves as the base parameters for all pipeline executions.
28
+ Concrete pipeline implementations should extend this class and add their
29
+ specific runtime parameters.
30
+ """
31
+
32
+ pass
33
+
34
+
35
+ class Pipeline(Expose):
36
+ __configuration: PipelineConfiguration
37
+
38
+ def __init__(self, configuration: PipelineConfiguration):
39
+ self.__configuration = configuration
40
+
41
+ # TODO: Make this an abstract method
42
+ # @abstractmethod
43
+ def trigger(
44
+ self, event: OntologyEvent, ontology_name: str, triple: tuple[Any, Any, Any]
45
+ ) -> Graph:
46
+ """Trigger the pipeline with the given event and triple.
47
+
48
+ This method should be implemented by concrete pipeline classes to process data
49
+ according to their specific logic and configuration.
50
+ """
51
+ raise NotImplementedError()
52
+
53
+ @abstractmethod
54
+ def run(self, parameters: PipelineParameters) -> Graph:
55
+ """Execute the pipeline with the given parameters.
56
+
57
+ This method should be implemented by concrete pipeline classes to process data
58
+ according to their specific logic and configuration.
59
+
60
+ Args:
61
+ parameters (PipelineParameters): Runtime parameters for pipeline execution
62
+ that control how the pipeline processes data
63
+
64
+ Returns:
65
+ Graph: An RDF graph containing the processed data and relationships
66
+
67
+ Raises:
68
+ NotImplementedError: If the concrete class does not implement this method
69
+ """
70
+ raise NotImplementedError()
File without changes