Repository navigation
Expand file tree
/
Copy pathrag-chat.ts
More file actions
48 lines (41 loc) · 1.42 KB
/
Copy pathrag-chat.ts
File metadata and controls
48 lines (41 loc) · 1.42 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
import GroqRAG from 'groq-rag';
async function main() {
const client = new GroqRAG({
apiKey: process.env.GROQ_API_KEY,
});
// Initialize RAG with in-memory vector store
await client.initRAG({
embedding: { provider: 'groq' },
vectorStore: { provider: 'memory' },
});
// Add documents to the knowledge base
await client.rag.addDocument(
`Our company's refund policy allows returns within 30 days of purchase.
Items must be in original condition with tags attached.
Refunds are processed within 5-7 business days.
Digital products are non-refundable.`,
{ source: 'refund-policy', category: 'policies' }
);
await client.rag.addDocument(
`Our shipping policy:
- Standard shipping: 5-7 business days ($5.99)
- Express shipping: 2-3 business days ($12.99)
- Overnight shipping: Next business day ($24.99)
Free shipping on orders over $50.`,
{ source: 'shipping-policy', category: 'policies' }
);
// Chat with RAG-augmented context
const response = await client.chat.withRAG({
messages: [
{ role: 'user', content: 'What is your refund policy?' },
],
topK: 3,
});
console.log('Response:', response.content);
console.log('\nSources used:');
for (const source of response.sources) {
console.log(`- Score: ${source.score.toFixed(3)}`);
console.log(` Content: ${source.document.content.substring(0, 100)}...`);
}
}
main().catch(console.error);