forked from usemoss/moss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_and_query_sample.ts
More file actions
85 lines (69 loc) · 2.61 KB
/
load_and_query_sample.ts
File metadata and controls
85 lines (69 loc) · 2.61 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* 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 { MossClient } from "@inferedge/moss";
import { config } from 'dotenv';
// Load environment variables
config();
/**
* Simple sample showing how to load an existing index and perform queries.
*/
async function loadAndQuerySample(): Promise<void> {
console.log('Moss SDK - Load Index & Query Sample');
// Load configuration from environment variables
const projectId = process.env.MOSS_PROJECT_ID;
const projectKey = process.env.MOSS_PROJECT_KEY;
const indexName = process.env.MOSS_INDEX_NAME;
// Validate required environment variables
if (!projectId || !projectKey || !indexName) {
console.error('Error: Missing required environment variables!');
console.error('Please set MOSS_PROJECT_ID, MOSS_PROJECT_KEY, and MOSS_INDEX_NAME in .env file');
return;
}
console.log(`Using index: ${indexName}`);
// Initialize Moss client
const client = new MossClient(projectId, projectKey);
try {
// Load the index for querying
console.log(`\nLoading index...`);
await client.loadIndex(indexName);
console.log(`Index loaded successfully`);
// Perform sample searches
console.log(`\nPerforming sample searches...`);
const queries = [
'how to return damaged item',
'return shipping label process',
'refund processing time and policy'
];
for (let i = 0; i < queries.length; i++) {
const query = queries[i];
console.log(`\nQuery ${i + 1}: '${query}'`);
try {
const results = await client.query(indexName, query, { topK: 3 });
console.log(`Found ${results.docs.length} results in ${results.timeTakenInMs}ms`);
results.docs.forEach((result, j) => {
const preview = result.text.length > 80 ? result.text.substring(0, 80) + '...' : result.text;
console.log(` ${j + 1}. [${result.id}] Score: ${result.score.toFixed(3)}`);
console.log(` ${preview}`);
});
} catch (e) {
console.log(` Query failed: ${String(e)}`);
}
}
console.log(`\nSample completed successfully!`);
} catch (error) {
console.error(`Error: ${error}`);
}
}
// Run the example if this file is executed directly
if (require.main === module) {
loadAndQuerySample().catch(console.error);
}