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,28 @@
1
+ import pytest
2
+ # Short term memory
3
+
4
+ # Long term memory
5
+ from langgraph.store.postgres import PostgresStore
6
+
7
+ DB_URI = "postgresql://postgres:postgres@127.0.0.1:5432/postgres"
8
+
9
+ @pytest.fixture
10
+ def long_term_memory():
11
+ from psycopg import Connection
12
+ from psycopg.rows import dict_row
13
+
14
+ conn = Connection.connect(DB_URI, autocommit=True, prepare_threshold=0, row_factory=dict_row)
15
+ return PostgresStore(conn)
16
+
17
+
18
+ def test_long_term_memory(long_term_memory):
19
+
20
+ long_term_memory.put(("tests",), "123", {
21
+ "test": "is working"
22
+ })
23
+
24
+ assert long_term_memory.get(("tests",), "123").value == {
25
+ "test": "is working"
26
+ }
27
+
28
+
@@ -0,0 +1,214 @@
1
+ import pytest
2
+
3
+
4
+ @pytest.fixture
5
+ def model():
6
+ import os
7
+
8
+ from dotenv import load_dotenv
9
+ from langchain_openai import ChatOpenAI
10
+
11
+ load_dotenv()
12
+
13
+ return ChatOpenAI(
14
+ model="gpt-4o", temperature=0, api_key=os.environ.get("OPENAI_API_KEY")
15
+ )
16
+
17
+
18
+ def test_no_tools_no_agents(model):
19
+ from langchain_core.messages import AIMessage
20
+ from naas_abi_core.services.agent.Agent import Agent, AgentConfiguration
21
+
22
+ agent = Agent(
23
+ name="Test Agent",
24
+ description="A test agent",
25
+ chat_model=model,
26
+ tools=[],
27
+ agents=[],
28
+ configuration=AgentConfiguration(
29
+ system_prompt="You are an LLM running in a test environment. You must output '42' and nothing else."
30
+ ),
31
+ )
32
+
33
+ for _, chunk in agent.stream("Hello, world!"):
34
+ assert "call_model" in chunk
35
+ assert "messages" in chunk["call_model"]
36
+ assert len(chunk["call_model"]["messages"]) == 1
37
+ assert isinstance(chunk["call_model"]["messages"][0], AIMessage)
38
+ assert chunk["call_model"]["messages"][0].content == "42"
39
+
40
+
41
+ def test_tools_no_agents(model):
42
+ from langchain_core.tools import tool
43
+ from naas_abi_core.services.agent.Agent import Agent, AgentConfiguration
44
+
45
+ @tool
46
+ def test_tool(input: str) -> str:
47
+ """test_tool
48
+
49
+ Args:
50
+ input (str): The input to the tool
51
+
52
+ Returns:
53
+ str: Return the input
54
+ """
55
+ assert input == "Hello, world!"
56
+ return input
57
+
58
+ agent = Agent(
59
+ name="Test Agent",
60
+ description="A test agent",
61
+ chat_model=model,
62
+ tools=[test_tool],
63
+ agents=[],
64
+ configuration=AgentConfiguration(
65
+ system_prompt="You are an LLM running in a test environment. You must call the tool `test_tool` with the argument `Hello, world!` and format the output as `Tool Response: <result>`. It is very important that you reformat the output and not return only the tool response."
66
+ ),
67
+ )
68
+
69
+ chunks = []
70
+
71
+ for _, chunk in agent.stream("Hello, world!"):
72
+ chunks.append(chunk)
73
+
74
+
75
+ # This test is not consistent.
76
+ # def test_agents_no_tools(model):
77
+ # from naas_abi_core.services.agent.Agent import Agent, AgentConfiguration, AgentSharedState
78
+ # from queue import Queue
79
+ # import json
80
+ # import pydash as pd
81
+
82
+ # queue = Queue()
83
+
84
+ # agent = Agent(
85
+ # name="Test Agent",
86
+ # description="A test agent",
87
+ # chat_model=model,
88
+ # tools=[
89
+ # Agent(
90
+ # name="Greeter",
91
+ # description="A greeter agent",
92
+ # chat_model=model,
93
+ # tools=[],
94
+ # agents=[],
95
+ # configuration=AgentConfiguration(
96
+ # system_prompt="You are a greeter agent. You must greet the user with the name they provide in the form 'Hello, <name>!' and nothing else."
97
+ # ),
98
+ # event_queue=queue,
99
+ # )
100
+ # ],
101
+ # agents=[],
102
+ # configuration=AgentConfiguration(
103
+ # system_prompt="The user will send you his name. You must call the greeter agent and output it's result and nothing else. You must call it only once."
104
+ # ),
105
+ # event_queue=queue,
106
+ # )
107
+
108
+ # chunks = []
109
+
110
+ # for _, chunk in agent.stream("ABI"):
111
+ # chunks.append(chunk)
112
+ # if 'call_model' in chunk:
113
+ # print(f'\n\t Call Model: {type(chunk)}')
114
+ # print(pd.get(chunk, "call_model.messages[-1].content"))
115
+ # print('\n')
116
+ # elif 'call_tools' in chunk:
117
+ # print(f'\n\t Call Tools: {type(chunk)}')
118
+ # print(pd.get(chunk, "call_tools.messages[-1].content"))
119
+ # print('\n')
120
+ # else:
121
+ # print(f'\n\t Unknown: {type(chunk)}')
122
+ # print(chunk)
123
+ # print('\n')
124
+ # assert len(chunks) <= 5
125
+
126
+ # assert len(chunks) == 5
127
+ # assert list(chunks[-1].values())[0]['messages'][-1].content == "Hello, ABI!"
128
+
129
+
130
+ def test_agent_duplication(model):
131
+ from queue import Queue
132
+
133
+ from naas_abi_core.services.agent.Agent import Agent, AgentConfiguration
134
+
135
+ queue = Queue()
136
+
137
+ sub_agent = Agent(
138
+ name="Sub agent",
139
+ description="Sub agent",
140
+ chat_model=model,
141
+ tools=[],
142
+ configuration=AgentConfiguration(system_prompt=""),
143
+ event_queue=queue,
144
+ )
145
+
146
+ first_agent = Agent(
147
+ name="Test Agent",
148
+ description="A test agent",
149
+ chat_model=model,
150
+ tools=[sub_agent],
151
+ agents=[],
152
+ configuration=AgentConfiguration(system_prompt=""),
153
+ event_queue=queue,
154
+ )
155
+
156
+ duplicated_agent = first_agent.duplicate()
157
+
158
+ assert id(duplicated_agent) != id(first_agent)
159
+ assert id(duplicated_agent.agents[0]) != id(first_agent.agents[0])
160
+
161
+
162
+ # def test_agent_stream_invoke(model):
163
+ # from naas_abi_core.services.agent.Agent import Agent, AgentConfiguration
164
+
165
+ # agent = Agent(
166
+ # name="Greeting Agent",
167
+ # description="A Greeting agent",
168
+ # chat_model=model,
169
+ # tools=[],
170
+ # agents=[],
171
+ # configuration=AgentConfiguration(system_prompt="You are a Greeting agent"),
172
+ # )
173
+
174
+ # events = []
175
+ # for event in agent.stream_invoke("My name is ABI"):
176
+ # events.append(event)
177
+
178
+ # assert len(events) == 2
179
+ # assert events[0]["event"] == "message"
180
+ # assert events[1]["event"] == "done"
181
+
182
+
183
+ def test_agent_one_tool_agent_response(model):
184
+ from langchain_core.tools import tool
185
+ from naas_abi_core.services.agent.Agent import Agent, AgentConfiguration
186
+
187
+ @tool
188
+ def add_one(input: int) -> int:
189
+ """Add one to the input"""
190
+ return input + 1
191
+
192
+ agent = Agent(
193
+ name="Test Agent",
194
+ description="A test agent",
195
+ chat_model=model,
196
+ tools=[add_one],
197
+ configuration=AgentConfiguration(
198
+ system_prompt="""You are a test agent. You must add one to the input and return the result.
199
+ Your output must be like:
200
+
201
+
202
+ <user_input> + one = result
203
+ """
204
+ ),
205
+ )
206
+
207
+ chunks = []
208
+ for _, chunk in agent.stream("42"):
209
+ chunks.append(chunk)
210
+ print(chunk)
211
+
212
+ assert len(chunks) == 3
213
+ assert "call_model" in chunks[-1]
214
+ assert chunks[-1]["call_model"]["messages"][0].content == "42 + one = 43"