|
| 1 | +"""Create and populate a demo Moss index for the ten-moss integration. |
| 2 | +
|
| 3 | +Usage: |
| 4 | + export MOSS_PROJECT_ID=... MOSS_PROJECT_KEY=... MOSS_INDEX_NAME=... |
| 5 | + python examples/create_index.py |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import os |
| 10 | + |
| 11 | +from dotenv import load_dotenv |
| 12 | +from loguru import logger |
| 13 | +from moss import DocumentInfo, MossClient |
| 14 | + |
| 15 | + |
| 16 | +def build_documents() -> list[DocumentInfo]: |
| 17 | + """Return a small support knowledge base for the demo index.""" |
| 18 | + return [ |
| 19 | + DocumentInfo( |
| 20 | + id="doc-1", |
| 21 | + text="Refunds are processed within 3-5 business days once approved.", |
| 22 | + metadata={"category": "billing"}, |
| 23 | + ), |
| 24 | + DocumentInfo( |
| 25 | + id="doc-2", |
| 26 | + text="You can track your order from the dashboard under Order History.", |
| 27 | + metadata={"category": "orders"}, |
| 28 | + ), |
| 29 | + DocumentInfo( |
| 30 | + id="doc-3", |
| 31 | + text="We offer 24/7 live chat support from the Help menu.", |
| 32 | + metadata={"category": "support"}, |
| 33 | + ), |
| 34 | + DocumentInfo( |
| 35 | + id="doc-4", |
| 36 | + text="Standard shipping takes 3-5 business days; express takes 1-2.", |
| 37 | + metadata={"category": "shipping"}, |
| 38 | + ), |
| 39 | + DocumentInfo( |
| 40 | + id="doc-5", |
| 41 | + text="Reset your password using the Forgot Password link on the login page.", |
| 42 | + metadata={"category": "account"}, |
| 43 | + ), |
| 44 | + DocumentInfo( |
| 45 | + id="doc-6", |
| 46 | + text="We accept Visa, Mastercard, American Express, PayPal, and Apple Pay.", |
| 47 | + metadata={"category": "billing"}, |
| 48 | + ), |
| 49 | + DocumentInfo( |
| 50 | + id="doc-7", |
| 51 | + text="Orders can be cancelled within 1 hour of placement.", |
| 52 | + metadata={"category": "orders"}, |
| 53 | + ), |
| 54 | + DocumentInfo( |
| 55 | + id="doc-8", |
| 56 | + text="International shipping is available to most countries; rates vary.", |
| 57 | + metadata={"category": "shipping"}, |
| 58 | + ), |
| 59 | + DocumentInfo( |
| 60 | + id="doc-9", |
| 61 | + text="Gift wrapping is available at checkout for a small fee.", |
| 62 | + metadata={"category": "services"}, |
| 63 | + ), |
| 64 | + DocumentInfo( |
| 65 | + id="doc-10", |
| 66 | + text="We price-match authorized retailers within 14 days of purchase.", |
| 67 | + metadata={"category": "billing"}, |
| 68 | + ), |
| 69 | + ] |
| 70 | + |
| 71 | + |
| 72 | +async def main() -> None: |
| 73 | + """Create the index named by MOSS_INDEX_NAME from build_documents().""" |
| 74 | + load_dotenv() |
| 75 | + client = MossClient(os.environ["MOSS_PROJECT_ID"], os.environ["MOSS_PROJECT_KEY"]) |
| 76 | + index_name = os.environ["MOSS_INDEX_NAME"] |
| 77 | + logger.info("Creating index {}", index_name) |
| 78 | + await client.create_index(name=index_name, docs=build_documents(), model_id="moss-minilm") |
| 79 | + logger.success("Index {} created", index_name) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + asyncio.run(main()) |
0 commit comments