agent-framework-mongodb 1.0.0a260918__tar.gz
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.
- agent_framework_mongodb-1.0.0a260918/LICENSE +21 -0
- agent_framework_mongodb-1.0.0a260918/PKG-INFO +136 -0
- agent_framework_mongodb-1.0.0a260918/README.md +111 -0
- agent_framework_mongodb-1.0.0a260918/agent_framework_mongodb/__init__.py +16 -0
- agent_framework_mongodb-1.0.0a260918/agent_framework_mongodb/_vector_store.py +1217 -0
- agent_framework_mongodb-1.0.0a260918/agent_framework_mongodb/py.typed +0 -0
- agent_framework_mongodb-1.0.0a260918/pyproject.toml +67 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agent-framework-mongodb
|
|
3
|
+
Version: 1.0.0a260918
|
|
4
|
+
Summary: MongoDB vector stores for Microsoft Agent Framework.
|
|
5
|
+
Author-email: Microsoft <af-support@microsoft.com>
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: agent-framework-core>=1.19.0,<2
|
|
20
|
+
Requires-Dist: pymongo>=4.13.2,<5
|
|
21
|
+
Project-URL: homepage, https://aka.ms/agent-framework
|
|
22
|
+
Project-URL: issues, https://github.com/microsoft/agent-framework/issues
|
|
23
|
+
Project-URL: source, https://github.com/microsoft/agent-framework/tree/main/python
|
|
24
|
+
|
|
25
|
+
# MongoDB vector stores
|
|
26
|
+
|
|
27
|
+
An alpha MongoDB integration for [Microsoft Agent Framework](https://learn.microsoft.com/agent-framework/).
|
|
28
|
+
`MongoDBCollection` provides async BSON CRUD and dense-vector search,
|
|
29
|
+
`MongoDBStore` shares an official async PyMongo client across collections, and
|
|
30
|
+
`MongoDBSettings` defines connection configuration.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install agent-framework-mongodb --pre
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Requires Python 3.10+, PyMongo 4.13.2+, and a MongoDB deployment with
|
|
39
|
+
[MongoDB Vector Search](https://www.mongodb.com/docs/vector-search/). Search-index
|
|
40
|
+
creation requires the corresponding database permissions.
|
|
41
|
+
|
|
42
|
+
## Connection
|
|
43
|
+
|
|
44
|
+
Set `MONGODB_URI` and `MONGODB_DATABASE_NAME`; `MONGODB_APP_NAME` is optional.
|
|
45
|
+
Constructors resolve explicit arguments first, then an explicitly selected
|
|
46
|
+
`.env` file, then process environment variables. URIs accept `str` or AF
|
|
47
|
+
`SecretString` and are unwrapped only for PyMongo.
|
|
48
|
+
|
|
49
|
+
You can instead inject an `AsyncMongoClient` and pass `database_name`. An
|
|
50
|
+
injected client bypasses URI, app-name, and `.env` loading and always remains
|
|
51
|
+
caller-owned. Connector-created clients are closed by `close()` or async context
|
|
52
|
+
exit. Collections created by a store borrow its already-resolved client.
|
|
53
|
+
|
|
54
|
+
## Example
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import asyncio
|
|
58
|
+
from dataclasses import dataclass
|
|
59
|
+
from typing import Annotated
|
|
60
|
+
|
|
61
|
+
from agent_framework import Filter, VectorStoreField, vectorstoremodel
|
|
62
|
+
from agent_framework_mongodb import MongoDBStore
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@vectorstoremodel(collection_name="articles")
|
|
66
|
+
@dataclass
|
|
67
|
+
class Article:
|
|
68
|
+
id: Annotated[str, VectorStoreField("key")]
|
|
69
|
+
topic: Annotated[str, VectorStoreField("data", is_indexed=True)]
|
|
70
|
+
text: Annotated[str, VectorStoreField("data")]
|
|
71
|
+
embedding: Annotated[list[float] | None, VectorStoreField("vector", dimensions=3)] = None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async def main() -> None:
|
|
75
|
+
async with MongoDBStore() as store:
|
|
76
|
+
collection = store.get_collection(Article)
|
|
77
|
+
await collection.ensure_collection_exists()
|
|
78
|
+
await collection.upsert(
|
|
79
|
+
[Article("1", "database", "MongoDB stores BSON documents", [1, 0, 0])],
|
|
80
|
+
generate_vectors=False,
|
|
81
|
+
)
|
|
82
|
+
results = await collection.search(
|
|
83
|
+
vector=[1, 0, 0],
|
|
84
|
+
filter=Filter("topic", "eq", "database"),
|
|
85
|
+
)
|
|
86
|
+
async for result in results:
|
|
87
|
+
print(result["record"].text, result["score"])
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
asyncio.run(main())
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Capabilities and limits
|
|
94
|
+
|
|
95
|
+
- String, signed 64-bit integer, and BSON `ObjectId` `_id` values retain native
|
|
96
|
+
identity. Only `ObjectId` keys can be generated. Typed `ObjectId` models must
|
|
97
|
+
register an encoder and decoder that preserve `ObjectId`; IDs are never stringified
|
|
98
|
+
or copied to hidden fields in storage. Agent-facing CRUD tool JSON represents
|
|
99
|
+
`ObjectId` keys as validated 24-character hexadecimal strings and converts them
|
|
100
|
+
back to native values at the collection boundary.
|
|
101
|
+
- Multiple top-level dense-vector fields are supported through one vector-search
|
|
102
|
+
index per field. Use `provider_annotations={"mongodb.index_name": "..."}` to
|
|
103
|
+
select an existing index name. Dimensions must be 1-8192. Metrics are cosine,
|
|
104
|
+
dot product, and Euclidean; returned `vectorSearchScore` values are native
|
|
105
|
+
normalized relevance scores where larger is better.
|
|
106
|
+
- `ensure_collection_exists()` creates missing indexes, waits for readiness, and
|
|
107
|
+
validates existing semantic definitions. It never updates or migrates an
|
|
108
|
+
incompatible collection or index. Newly written documents become searchable
|
|
109
|
+
asynchronously, even after an index is queryable.
|
|
110
|
+
- Retrieval filters preserve whole-value equality, missing/null, direct array
|
|
111
|
+
membership, boolean/number, and literal text semantics with server-side
|
|
112
|
+
expressions. Collection filters require fields declared as `list` and reject
|
|
113
|
+
mapping and non-list collection operands whose identity BSON cannot preserve.
|
|
114
|
+
Vector prefilters are intentionally narrower: indexed scalar equality, range,
|
|
115
|
+
membership, AND, and OR only. Nested paths, NOT, null/missing, list membership,
|
|
116
|
+
and literal/analyzed text are rejected for vector search.
|
|
117
|
+
- Keyword-hybrid search, sparse/binary vectors, provider-side embedding
|
|
118
|
+
generation, and automatic schema migration are not supported.
|
|
119
|
+
- ANN defaults `numCandidates` to the MongoDB recommendation of 20 times
|
|
120
|
+
`skip + top`, capped at MongoDB's maximum of 10,000. Explicit values must be
|
|
121
|
+
1-10,000 and at least `skip + top`; larger result windows require exact search.
|
|
122
|
+
The vector stage selects its result window before score thresholding, skip,
|
|
123
|
+
and limit, so selective thresholds can underfill a page. Deep offsets increase
|
|
124
|
+
server cost; result prefixes are never materialized by the connector.
|
|
125
|
+
- Every record is fully validated against BSON's signed 64-bit integer and 16 MiB
|
|
126
|
+
document limits before any batch write. PyMongo then chunks bulk writes using
|
|
127
|
+
negotiated server limits. A server error can leave a successful prefix
|
|
128
|
+
(`ordered=True`) or subset (`ordered=False`) persisted.
|
|
129
|
+
|
|
130
|
+
## Documentation
|
|
131
|
+
|
|
132
|
+
- [Microsoft Agent Framework documentation](https://learn.microsoft.com/agent-framework/)
|
|
133
|
+
- [MongoDB Vector Search documentation](https://www.mongodb.com/docs/vector-search/)
|
|
134
|
+
- [PyMongo async documentation](https://pymongo.readthedocs.io/en/stable/api/pymongo/asynchronous/)
|
|
135
|
+
- [MongoDB limits and thresholds](https://www.mongodb.com/docs/manual/reference/limits/)
|
|
136
|
+
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# MongoDB vector stores
|
|
2
|
+
|
|
3
|
+
An alpha MongoDB integration for [Microsoft Agent Framework](https://learn.microsoft.com/agent-framework/).
|
|
4
|
+
`MongoDBCollection` provides async BSON CRUD and dense-vector search,
|
|
5
|
+
`MongoDBStore` shares an official async PyMongo client across collections, and
|
|
6
|
+
`MongoDBSettings` defines connection configuration.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install agent-framework-mongodb --pre
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Requires Python 3.10+, PyMongo 4.13.2+, and a MongoDB deployment with
|
|
15
|
+
[MongoDB Vector Search](https://www.mongodb.com/docs/vector-search/). Search-index
|
|
16
|
+
creation requires the corresponding database permissions.
|
|
17
|
+
|
|
18
|
+
## Connection
|
|
19
|
+
|
|
20
|
+
Set `MONGODB_URI` and `MONGODB_DATABASE_NAME`; `MONGODB_APP_NAME` is optional.
|
|
21
|
+
Constructors resolve explicit arguments first, then an explicitly selected
|
|
22
|
+
`.env` file, then process environment variables. URIs accept `str` or AF
|
|
23
|
+
`SecretString` and are unwrapped only for PyMongo.
|
|
24
|
+
|
|
25
|
+
You can instead inject an `AsyncMongoClient` and pass `database_name`. An
|
|
26
|
+
injected client bypasses URI, app-name, and `.env` loading and always remains
|
|
27
|
+
caller-owned. Connector-created clients are closed by `close()` or async context
|
|
28
|
+
exit. Collections created by a store borrow its already-resolved client.
|
|
29
|
+
|
|
30
|
+
## Example
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import asyncio
|
|
34
|
+
from dataclasses import dataclass
|
|
35
|
+
from typing import Annotated
|
|
36
|
+
|
|
37
|
+
from agent_framework import Filter, VectorStoreField, vectorstoremodel
|
|
38
|
+
from agent_framework_mongodb import MongoDBStore
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@vectorstoremodel(collection_name="articles")
|
|
42
|
+
@dataclass
|
|
43
|
+
class Article:
|
|
44
|
+
id: Annotated[str, VectorStoreField("key")]
|
|
45
|
+
topic: Annotated[str, VectorStoreField("data", is_indexed=True)]
|
|
46
|
+
text: Annotated[str, VectorStoreField("data")]
|
|
47
|
+
embedding: Annotated[list[float] | None, VectorStoreField("vector", dimensions=3)] = None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
async def main() -> None:
|
|
51
|
+
async with MongoDBStore() as store:
|
|
52
|
+
collection = store.get_collection(Article)
|
|
53
|
+
await collection.ensure_collection_exists()
|
|
54
|
+
await collection.upsert(
|
|
55
|
+
[Article("1", "database", "MongoDB stores BSON documents", [1, 0, 0])],
|
|
56
|
+
generate_vectors=False,
|
|
57
|
+
)
|
|
58
|
+
results = await collection.search(
|
|
59
|
+
vector=[1, 0, 0],
|
|
60
|
+
filter=Filter("topic", "eq", "database"),
|
|
61
|
+
)
|
|
62
|
+
async for result in results:
|
|
63
|
+
print(result["record"].text, result["score"])
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
asyncio.run(main())
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Capabilities and limits
|
|
70
|
+
|
|
71
|
+
- String, signed 64-bit integer, and BSON `ObjectId` `_id` values retain native
|
|
72
|
+
identity. Only `ObjectId` keys can be generated. Typed `ObjectId` models must
|
|
73
|
+
register an encoder and decoder that preserve `ObjectId`; IDs are never stringified
|
|
74
|
+
or copied to hidden fields in storage. Agent-facing CRUD tool JSON represents
|
|
75
|
+
`ObjectId` keys as validated 24-character hexadecimal strings and converts them
|
|
76
|
+
back to native values at the collection boundary.
|
|
77
|
+
- Multiple top-level dense-vector fields are supported through one vector-search
|
|
78
|
+
index per field. Use `provider_annotations={"mongodb.index_name": "..."}` to
|
|
79
|
+
select an existing index name. Dimensions must be 1-8192. Metrics are cosine,
|
|
80
|
+
dot product, and Euclidean; returned `vectorSearchScore` values are native
|
|
81
|
+
normalized relevance scores where larger is better.
|
|
82
|
+
- `ensure_collection_exists()` creates missing indexes, waits for readiness, and
|
|
83
|
+
validates existing semantic definitions. It never updates or migrates an
|
|
84
|
+
incompatible collection or index. Newly written documents become searchable
|
|
85
|
+
asynchronously, even after an index is queryable.
|
|
86
|
+
- Retrieval filters preserve whole-value equality, missing/null, direct array
|
|
87
|
+
membership, boolean/number, and literal text semantics with server-side
|
|
88
|
+
expressions. Collection filters require fields declared as `list` and reject
|
|
89
|
+
mapping and non-list collection operands whose identity BSON cannot preserve.
|
|
90
|
+
Vector prefilters are intentionally narrower: indexed scalar equality, range,
|
|
91
|
+
membership, AND, and OR only. Nested paths, NOT, null/missing, list membership,
|
|
92
|
+
and literal/analyzed text are rejected for vector search.
|
|
93
|
+
- Keyword-hybrid search, sparse/binary vectors, provider-side embedding
|
|
94
|
+
generation, and automatic schema migration are not supported.
|
|
95
|
+
- ANN defaults `numCandidates` to the MongoDB recommendation of 20 times
|
|
96
|
+
`skip + top`, capped at MongoDB's maximum of 10,000. Explicit values must be
|
|
97
|
+
1-10,000 and at least `skip + top`; larger result windows require exact search.
|
|
98
|
+
The vector stage selects its result window before score thresholding, skip,
|
|
99
|
+
and limit, so selective thresholds can underfill a page. Deep offsets increase
|
|
100
|
+
server cost; result prefixes are never materialized by the connector.
|
|
101
|
+
- Every record is fully validated against BSON's signed 64-bit integer and 16 MiB
|
|
102
|
+
document limits before any batch write. PyMongo then chunks bulk writes using
|
|
103
|
+
negotiated server limits. A server error can leave a successful prefix
|
|
104
|
+
(`ordered=True`) or subset (`ordered=False`) persisted.
|
|
105
|
+
|
|
106
|
+
## Documentation
|
|
107
|
+
|
|
108
|
+
- [Microsoft Agent Framework documentation](https://learn.microsoft.com/agent-framework/)
|
|
109
|
+
- [MongoDB Vector Search documentation](https://www.mongodb.com/docs/vector-search/)
|
|
110
|
+
- [PyMongo async documentation](https://pymongo.readthedocs.io/en/stable/api/pymongo/asynchronous/)
|
|
111
|
+
- [MongoDB limits and thresholds](https://www.mongodb.com/docs/manual/reference/limits/)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
"""Async MongoDB vector collections and stores."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import importlib.metadata
|
|
8
|
+
|
|
9
|
+
from ._vector_store import MongoDBCollection, MongoDBSettings, MongoDBStore
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
__version__ = importlib.metadata.version(__name__)
|
|
13
|
+
except importlib.metadata.PackageNotFoundError:
|
|
14
|
+
__version__ = "0.0.0"
|
|
15
|
+
|
|
16
|
+
__all__ = ["MongoDBCollection", "MongoDBSettings", "MongoDBStore", "__version__"]
|