amsdal_ml 0.1.3__py3-none-any.whl → 0.1.4__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.
amsdal_ml/__about__.py CHANGED
@@ -1 +1 @@
1
- __version__ = '0.1.3'
1
+ __version__ = '0.1.4'
@@ -27,7 +27,16 @@ class RetrieverArgs(BaseModel):
27
27
  exclude_tags: Optional[list[str]] = None
28
28
 
29
29
 
30
- _retriever = OpenAIRetriever()
30
+ class _RetrieverSingleton:
31
+ """Singleton holder for lazy retriever initialization."""
32
+ _instance: Optional[OpenAIRetriever] = None
33
+
34
+ @classmethod
35
+ def get(cls) -> OpenAIRetriever:
36
+ """Lazy initialization of retriever to ensure env vars are loaded."""
37
+ if cls._instance is None:
38
+ cls._instance = OpenAIRetriever()
39
+ return cls._instance
31
40
 
32
41
 
33
42
  async def retriever_search(
@@ -39,7 +48,8 @@ async def retriever_search(
39
48
  logging.info(
40
49
  f"retriever_search called with query={query}, k={k}, include_tags={include_tags}, exclude_tags={exclude_tags}"
41
50
  )
42
- chunks = await _retriever.asimilarity_search(
51
+ retriever = _RetrieverSingleton.get()
52
+ chunks = await retriever.asimilarity_search(
43
53
  query=query,
44
54
  k=k,
45
55
  include_tags=include_tags,
@@ -94,7 +94,7 @@ class StdioClient(ToolClient):
94
94
 
95
95
  if not self._persist:
96
96
  async with AsyncExitStack() as stack:
97
- params = StdioServerParameters(command=self._command, args=self._args)
97
+ params = StdioServerParameters(command=self._command, args=self._args, env=os.environ.copy())
98
98
  rx, tx = await stack.enter_async_context(stdio_client(params))
99
99
  s = await stack.enter_async_context(ClientSession(rx, tx))
100
100
  await s.initialize()
@@ -123,7 +123,7 @@ class StdioClient(ToolClient):
123
123
  async def call(self, tool_name: str, args: dict[str, Any], *, timeout: float | None = None) -> Any:
124
124
  if not self._persist:
125
125
  async with AsyncExitStack() as stack:
126
- params = StdioServerParameters(command=self._command, args=self._args)
126
+ params = StdioServerParameters(command=self._command, args=self._args, env=os.environ.copy())
127
127
  rx, tx = await stack.enter_async_context(stdio_client(params))
128
128
  s = await stack.enter_async_context(ClientSession(rx, tx))
129
129
  await s.initialize()
@@ -0,0 +1,235 @@
1
+ Metadata-Version: 2.4
2
+ Name: amsdal_ml
3
+ Version: 0.1.4
4
+ Summary: amsdal_ml plugin for AMSDAL Framework
5
+ Requires-Python: >=3.11
6
+ Requires-Dist: aiohttp==3.12.15
7
+ Requires-Dist: amsdal-cli>=0.5.7
8
+ Requires-Dist: amsdal-data>=0.5.9
9
+ Requires-Dist: amsdal-models>=0.5.9
10
+ Requires-Dist: amsdal-utils>=0.5.4
11
+ Requires-Dist: amsdal>=0.5.6
12
+ Requires-Dist: mcp>=0.1
13
+ Requires-Dist: openai==1.100.2
14
+ Requires-Dist: pydantic-settings==2.10.1
15
+ Requires-Dist: pydantic==2.11.7
16
+ Description-Content-Type: text/markdown
17
+
18
+ # AMSDAL ML
19
+
20
+ [![CI](https://github.com/amsdal/amsdal_ml/actions/workflows/ci.yml/badge.svg)](https://github.com/amsdal/amsdal_ml/actions/workflows/ci.yml)
21
+ [![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
22
+
23
+ Machine learning plugin for the AMSDAL Framework, providing embeddings, vector search, semantic retrieval, and AI agents with support for OpenAI models.
24
+
25
+ ## Features
26
+
27
+ - **Vector Embeddings**: Generate and store embeddings for any AMSDAL model with automatic chunking
28
+ - **Semantic Search**: Query your data using natural language with tag-based filtering
29
+ - **AI Agents**: Build Q&A systems with streaming support and citation tracking
30
+ - **Async-First**: Optimized for high-performance async operations
31
+ - **MCP Integration**: Expose and consume tools via Model Context Protocol (stdio/HTTP)
32
+ - **File Attachments**: Process and embed documents with built-in loaders
33
+ - **Extensible**: Abstract base classes for custom models, retrievers, and ingesters
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ pip install amsdal-ml
39
+ ```
40
+
41
+ ### Requirements
42
+
43
+ - Python 3.11 or higher
44
+ - AMSDAL Framework 0.5.6+
45
+ - OpenAI API key (for default implementations)
46
+
47
+ ## Quick Start
48
+
49
+ ### 1. Configuration
50
+
51
+ Create a `.env` file in your project root:
52
+
53
+ ```env
54
+ OPENAI_API_KEY=sk-your-api-key-here
55
+ async_mode=true
56
+ ml_model_class=amsdal_ml.ml_models.openai_model.OpenAIModel
57
+ ml_retriever_class=amsdal_ml.ml_retrievers.openai_retriever.OpenAIRetriever
58
+ ml_ingesting_class=amsdal_ml.ml_ingesting.openai_ingesting.OpenAIIngesting
59
+ ```
60
+
61
+ Create a `config.yml` for AMSDAL connections:
62
+
63
+ ```yaml
64
+ application_name: my-ml-app
65
+ async_mode: true
66
+ connections:
67
+ - name: sqlite_state
68
+ backend: sqlite-state-async
69
+ credentials:
70
+ - db_path: ./warehouse/state.sqlite3
71
+ - check_same_thread: false
72
+ - name: lock
73
+ backend: amsdal_data.lock.implementations.thread_lock.ThreadLock
74
+ resources_config:
75
+ repository:
76
+ default: sqlite_state
77
+ lock: lock
78
+ ```
79
+
80
+ ### 2. Generate Embeddings
81
+
82
+ ```python
83
+ from amsdal_ml.ml_ingesting.openai_ingesting import OpenAIIngesting
84
+ from amsdal_ml.ml_config import ml_config
85
+
86
+ # Initialize ingesting
87
+ ingester = OpenAIIngesting(
88
+ model=MyModel,
89
+ embedding_field='embedding',
90
+ )
91
+
92
+ # Generate embeddings for an instance
93
+ instance = MyModel(content='Your text here')
94
+ embeddings = await ingester.agenerate_embeddings(instance)
95
+ await ingester.asave(embeddings, instance)
96
+ ```
97
+
98
+ ### 3. Semantic Search
99
+
100
+ ```python
101
+ from amsdal_ml.ml_retrievers.openai_retriever import OpenAIRetriever
102
+
103
+ retriever = OpenAIRetriever()
104
+
105
+ # Search for relevant content
106
+ results = await retriever.asimilarity_search(
107
+ query='What is machine learning?',
108
+ k=5,
109
+ include_tags=['documentation']
110
+ )
111
+
112
+ for chunk in results:
113
+ print(f'{chunk.object_class}:{chunk.object_id} - {chunk.raw_text}')
114
+ ```
115
+
116
+ ### 4. Build an AI Agent
117
+
118
+ ```python
119
+ from amsdal_ml.agents.default_qa_agent import DefaultQAAgent
120
+
121
+ agent = DefaultQAAgent()
122
+
123
+ # Ask questions
124
+ output = await agent.arun('Explain vector embeddings')
125
+ print(output.answer)
126
+ print(f'Used tools: {output.used_tools}')
127
+
128
+ # Stream responses
129
+ async for chunk in agent.astream('What is semantic search?'):
130
+ print(chunk, end='', flush=True)
131
+ ```
132
+
133
+ ## Architecture
134
+
135
+ ### Core Components
136
+
137
+ - **`MLModel`**: Abstract interface for LLM inference (invoke, stream, with attachments)
138
+ - **`MLIngesting`**: Generate text and embeddings from data objects with chunking
139
+ - **`MLRetriever`**: Semantic similarity search with tag-based filtering
140
+ - **`Agent`**: Q&A and task-oriented agents with streaming and citations
141
+ - **`EmbeddingModel`**: Database model storing 1536-dimensional vectors linked to source objects
142
+ - **MCP Server/Client**: Expose retrievers as tools or consume external MCP services
143
+
144
+ ### Configuration
145
+
146
+ All settings are managed via `MLConfig` in `.env`:
147
+
148
+ ```env
149
+ # Model Configuration
150
+ llm_model_name=gpt-4o
151
+ llm_temperature=0.0
152
+ embed_model_name=text-embedding-3-small
153
+
154
+ # Chunking Parameters
155
+ embed_max_depth=2
156
+ embed_max_chunks=10
157
+ embed_max_tokens_per_chunk=800
158
+
159
+ # Retrieval Settings
160
+ retriever_default_k=8
161
+ ```
162
+
163
+ ## Development
164
+
165
+ ### Setup
166
+
167
+ ```bash
168
+ # Install dependencies
169
+ pip install --upgrade uv hatch==1.14.2
170
+ hatch env create
171
+ hatch run sync
172
+ ```
173
+
174
+ ### Testing
175
+
176
+ ```bash
177
+ # Run all tests with coverage
178
+ hatch run cov
179
+
180
+ # Run specific tests
181
+ hatch run test tests/test_openai_model.py
182
+
183
+ # Watch mode
184
+ pytest tests/ -v
185
+ ```
186
+
187
+ ### Code Quality
188
+
189
+ ```bash
190
+ # Run all checks (style + typing)
191
+ hatch run all
192
+
193
+ # Format code
194
+ hatch run fmt
195
+
196
+ # Type checking
197
+ hatch run typing
198
+ ```
199
+
200
+ ### AMSDAL CLI
201
+
202
+ ```bash
203
+ # Generate a new model
204
+ amsdal generate model MyModel --format py
205
+
206
+ # Generate property
207
+ amsdal generate property --model MyModel embedding_field
208
+
209
+ # Generate transaction
210
+ amsdal generate transaction ProcessEmbeddings
211
+
212
+ # Generate hook
213
+ amsdal generate hook --model MyModel on_create
214
+ ```
215
+
216
+ ## MCP Server
217
+
218
+ Run the retriever as an MCP server for integration with Claude Desktop or other MCP clients:
219
+
220
+ ```bash
221
+ python -m amsdal_ml.mcp_server.server_retriever_stdio \
222
+ --amsdal-config "$(echo '{"async_mode": true, ...}' | base64)"
223
+ ```
224
+
225
+ The server exposes a `search` tool for semantic search in your knowledge base.
226
+
227
+ ## License
228
+
229
+ See `amsdal_ml/Third-Party Materials - AMSDAL Dependencies - License Notices.md` for dependency licenses.
230
+
231
+ ## Links
232
+
233
+ - [AMSDAL Framework](https://github.com/amsdal/amsdal)
234
+ - [Documentation](https://docs.amsdal.com)
235
+ - [Issue Tracker](https://github.com/amsdal/amsdal_ml/issues)
@@ -1,5 +1,5 @@
1
1
  amsdal_ml/Third-Party Materials - AMSDAL Dependencies - License Notices.md,sha256=0A3jjw91I-NaTHXCw8IQiKUdFJgVavhgHaFX4Nag4jk,81658
2
- amsdal_ml/__about__.py,sha256=uZsygMXMKRw-7qhWojAjnpm8GFPXU92xW6XA8O5GwFY,22
2
+ amsdal_ml/__about__.py,sha256=aBEbDvx4LMg8A1TJJR6dEHu8rQODVin528hLS_EDvuA,22
3
3
  amsdal_ml/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  amsdal_ml/app.py,sha256=K_lUz37_FUQmPwzDakDhVXA_D2SL9z1JtcuHfuBvlKE,184
5
5
  amsdal_ml/ml_config.py,sha256=UONMVG2RbKVbaYF-GdlhRQbm8flp52GxBzfqdzuLZ5w,1788
@@ -7,7 +7,7 @@ amsdal_ml/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  amsdal_ml/agents/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  amsdal_ml/agents/agent.py,sha256=L00RTIkopdGnJp_NctEtWRXAgWy1DAn7AEg0rMoXdJw,1514
9
9
  amsdal_ml/agents/default_qa_agent.py,sha256=MjrA5XYOKe5PwfSnkoeGmEnC8mP9asRHGHPHd1oPH6g,14017
10
- amsdal_ml/agents/retriever_tool.py,sha256=SqIGqYi_xMcm_MgLcwdMW2ALzYphkk02KBevH8n-Op0,1850
10
+ amsdal_ml/agents/retriever_tool.py,sha256=ND4lm1I4ncWWSQBQlYYP1EpNBfwTbdy89FaTvMzLARU,2239
11
11
  amsdal_ml/agents/promts/__init__.py,sha256=3AKR3lrV067CO03XYFDmAKJv0x2oqSTcVeDIqNwoAu0,1571
12
12
  amsdal_ml/agents/promts/react_chat.prompt,sha256=EArdA102sSBOWchhub1QoaKws_DVpMqDflc5htBkFKI,1762
13
13
  amsdal_ml/fileio/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -16,7 +16,7 @@ amsdal_ml/fileio/openai_loader.py,sha256=4YyxK5pBAnRm55et5yMu6X5OdK2kWTqpuPF1E2-
16
16
  amsdal_ml/mcp_client/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
17
  amsdal_ml/mcp_client/base.py,sha256=cNuHHCSSND4BkIyhQZ-1mJLPp630wTCtiCQSHnK2zmA,441
18
18
  amsdal_ml/mcp_client/http_client.py,sha256=2fFCaubp_cFeyCqN9CP7bVLTp4UIIEhJPq9BVHBf6-w,1719
19
- amsdal_ml/mcp_client/stdio_client.py,sha256=F5Y2yGFNDCE7Nfg_6QMCkp81Vb4uS2UQ1XnD_FTGM8U,5777
19
+ amsdal_ml/mcp_client/stdio_client.py,sha256=i-xI7ROMOV-HKRYVUw-PZR0kYEdQbZfO0E9eteiBaPQ,5823
20
20
  amsdal_ml/mcp_server/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
21
  amsdal_ml/mcp_server/server_retriever_stdio.py,sha256=iUQNwWEDZE0OIS9vyTyJp_EOn4kvl9Gn0Okzcq-ymo0,1762
22
22
  amsdal_ml/migrations/0000_initial.py,sha256=UeEtqHrCgmTQyGnyV2jC7yt9Ce7Xm0-MyhOQU3rRrs8,1633
@@ -34,6 +34,6 @@ amsdal_ml/ml_retrievers/openai_retriever.py,sha256=oY71oPJdFuECWaJL_ByKZFE7HRJ-S
34
34
  amsdal_ml/ml_retrievers/retriever.py,sha256=zHvaAOAD6H4IaIaYA_wy2e8tMmGtOnyNsDS2B4dQjBU,1005
35
35
  amsdal_ml/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
36
36
  amsdal_ml/models/embedding_model.py,sha256=mdMSYHrkSIKHi5mg4HqMaTTiPGqLYtRdRkbEK_trFWw,774
37
- amsdal_ml-0.1.3.dist-info/METADATA,sha256=BETzPNugrsUz-RL3yG-2I-4I_WBSLkJ5oRJYxyZxEDY,1676
38
- amsdal_ml-0.1.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
39
- amsdal_ml-0.1.3.dist-info/RECORD,,
37
+ amsdal_ml-0.1.4.dist-info/METADATA,sha256=F4dsbTo3XU-fJ3v1FX8BjimB0lB_B54Nl6gUF0tp2hk,5890
38
+ amsdal_ml-0.1.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
39
+ amsdal_ml-0.1.4.dist-info/RECORD,,
@@ -1,69 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: amsdal_ml
3
- Version: 0.1.3
4
- Summary: amsdal_ml plugin for AMSDAL Framework
5
- Requires-Python: >=3.11
6
- Requires-Dist: aiohttp==3.12.15
7
- Requires-Dist: amsdal-cli>=0.5.7
8
- Requires-Dist: amsdal-data>=0.5.9
9
- Requires-Dist: amsdal-models>=0.5.9
10
- Requires-Dist: amsdal-utils>=0.5.4
11
- Requires-Dist: amsdal>=0.5.6
12
- Requires-Dist: mcp>=0.1
13
- Requires-Dist: openai==1.100.2
14
- Requires-Dist: pydantic-settings==2.10.1
15
- Requires-Dist: pydantic==2.11.7
16
- Description-Content-Type: text/markdown
17
-
18
- # amsdal-ml
19
-
20
- This plugin extends the AMSDAL Framework with machine learning utilities,
21
- including custom models for embeddings, properties for ML metadata, and
22
- hooks for working with vector search and AI-driven features.
23
-
24
- ## Plugin Structure
25
-
26
- - `src/models/` - Contains model definitions in Python format
27
- - `src/transactions/` - Contains transaction definitions
28
- - `pyproject.toml` - Plugin configuration file
29
- - `config.yml` - Configuration for connections
30
-
31
- ## Installing this Plugin
32
-
33
- To use this plugin in an AMSDAL application:
34
-
35
- 1. Copy the plugin directory to your AMSDAL application
36
- 2. Import the models and transactions as needed
37
- 3. Register the plugin in your application configuration
38
-
39
- ## Development
40
-
41
- This plugin uses sync mode.
42
-
43
- ### Adding Models
44
-
45
- ```bash
46
- amsdal generate model ModelName --format py
47
- ```
48
-
49
- ### Adding Properties
50
-
51
- ```bash
52
- amsdal generate property --model ModelName property_name
53
- ```
54
-
55
- ### Adding Transactions
56
-
57
- ```bash
58
- amsdal generate transaction TransactionName
59
- ```
60
-
61
- ### Adding Hooks
62
-
63
- ```bash
64
- amsdal generate hook --model ModelName on_create
65
- ```
66
-
67
- ## Testing
68
-
69
- Test your plugin by integrating it with an AMSDAL application and running the application's test suite.