forked from usemoss/moss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_and_query_sample.py
More file actions
66 lines (49 loc) · 1.94 KB
/
load_and_query_sample.py
File metadata and controls
66 lines (49 loc) · 1.94 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Simple Moss SDK Load Index and Query Sample
This sample shows how to load an existing FAQ index and perform search queries.
Includes sample returns-related questions for demonstration.
Required Environment Variables:
- MOSS_PROJECT_ID: Your Moss project ID
- MOSS_PROJECT_KEY: Your Moss project key
- MOSS_INDEX_NAME: Name of existing FAQ index to query
"""
import asyncio
import os
from dotenv import load_dotenv
from inferedge_moss import MossClient, QueryOptions
# Load environment variables
load_dotenv()
async def load_and_query_sample():
"""Simple sample showing how to load an existing index and perform queries."""
print("=" * 40)
print("Moss SDK - Load Index & Query Sample")
print("=" * 40)
# Load configuration from environment variables
project_id = os.getenv("MOSS_PROJECT_ID")
project_key = os.getenv("MOSS_PROJECT_KEY")
index_name = os.getenv("MOSS_INDEX_NAME")
print(f"Using index: {index_name}")
# Initialize Moss client
client = MossClient(project_id, project_key)
try:
# Load the index for querying
print("\nLoading index...")
await client.load_index(index_name)
print("Index loaded successfully")
print("=" * 40)
print("\nPerforming sample search...\n")
query = "refund processing time and policy"
results = await client.query(index_name, query, QueryOptions(top_k=6))
print(f"Found {len(results.docs)} results in {results.time_taken_ms}ms\n")
for j, result in enumerate(results.docs, 1):
print(f"[{result.id}] Score: {result.score:.3f}")
print(f" {result.text}\n")
print("\nSample completed successfully!")
except Exception as error:
print(f"Error: {error}")
print("Check your credentials and index name in .env file")
# Export main function
__all__ = ["load_and_query_sample"]
# Run the sample
if __name__ == "__main__":
asyncio.run(load_and_query_sample())