-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_full_text_quickstart.py
More file actions
50 lines (41 loc) · 1.66 KB
/
Copy pathrag_full_text_quickstart.py
File metadata and controls
50 lines (41 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""MongoDB full-text RAG explicit provisioning and direct-search quickstart."""
from __future__ import annotations
import asyncio
import os
from agent_framework_mongodb import (
EqualFilter,
MongoDBRAGContextProvider,
MongoDBRAGProvider,
MongoDBRAGProviderOptions,
MongoDBSearchMode,
)
def required_environment(name: str) -> str:
value = os.getenv(name)
if not value:
raise RuntimeError(f"Set {name} before running the full-text RAG quickstart.")
return value
async def main() -> None:
direct = MongoDBRAGProvider(
MongoDBRAGProviderOptions(
mode=MongoDBSearchMode.FULL_TEXT,
search_index_name=required_environment("MONGODB_RAG_SEARCH_INDEX"),
text_fields=("content",),
# Custom analyzer definitions are intentionally outside this sample facade.
search_analyzer="lucene.standard",
filter=EqualFilter(
"tenant_id",
required_environment("MONGODB_RAG_TENANT"),
),
),
connection_string=required_environment("MONGODB_URI"),
database_name=required_environment("MONGODB_DATABASE"),
collection_name=required_environment("MONGODB_RAG_COLLECTION"),
)
rag = MongoDBRAGContextProvider(direct)
async with rag:
# Run this only under a provisioner identity; normal searches never mutate indexes.
await direct.ensure_search_index(wait_until_ready=True)
for result in await rag.search("How does this system isolate tenants?"):
print(f"{result.score:.4f} {result.source_name or result.id}: {result.text}")
if __name__ == "__main__":
asyncio.run(main())