Skip to content

Instantly share code, notes, and snippets.

@up1
Last active February 21, 2025 15:29
Show Gist options
  • Save up1/9d9c703402ede8cc5b8527c444f3f062 to your computer and use it in GitHub Desktop.
Save up1/9d9c703402ede8cc5b8527c444f3f062 to your computer and use it in GitHub Desktop.
MongoDB-RAG
// ติดตั้ง cli ของ mongo-rag
$npm install -g mongodb-rag
// สร้าง configuration ของ Mongo-RAG ผ่าน cli
$npx mongodb-rag init
✔ Enter your MongoDB connection string: · mongodb+srv://your_user:your_password@host
✔ Enter the database name: · mongodb-rag
✔ Enter the collection name: · documents
✔ Select an embedding provider: · openai
✔ Enter your API key (skip if using Ollama): · demo
✔ Enter the model name: · text-embedding-3-small
✔ Enter the embedding dimensions: · 4096
🔍 Next steps:
1. Run `npx mongodb-rag test-connection` to verify your setup
2. Run `npx mongodb-rag create-index` to create your vector search index
// จะสร้างไฟล์ .mongodb-rag.json ขึ้นมา
{
"mongoUrl": "mongodb+srv://your_user:your_password@host",
"database": "mongodb-rag",
"collection": "documents",
"embedding": {
"provider": "openai",
"apiKey": "demo",
"model": "text-embedding-3-small",
"dimensions": 4096,
"batchSize": 100,
"baseUrl": "http://localhost:11434"
},
"search": {
"maxResults": 5,
"minScore": 0.7
},
"indexName": "vector_index"
}
// สร้างไฟล์ .env สำหรับ config พวก environment variable ต่าง ๆ ที่ต้องใช้งาน
$npx mongodb-rag create-env
// ไฟล์ .env
MONGODB_URI="mongodb+srv://your_user:your_password@host"
EMBEDDING_PROVIDER="openai"
EMBEDDING_API_KEY="demo"
EMBEDDING_MODEL="text-embedding-3-small"
VECTOR_INDEX="vector_index"
MONGODB_DATABASE="mongodb-rag"
MONGODB_COLLECTION="documents"
// ทำการสร้าง index ใน mongodb atlas
$npx mongodb-rag create-index
// ทำการ load ข้อมูลจากไฟล์ใน directory ที่กำหนด
// จะทำการอ่าน และทำ embedding ข้อมูล สามารถเลือก chunking strategy ได้ เช่น Sliding window, recursive, และ semantic chunking
// บันทึกข้อมูลลงใน mongodb atlas
$npx mongodb-rag ingest --directory ./your-data
// ทดสอบค้นหาผ่าน CLI ได้เลย หรือจะเขียน code ก็ได้
$npx mongodb-rag search "your question"
// code การค้นหา
const results = await rag.search('your question', {
filter: { 'metadata.source': 'docs' },
maxResults: 10
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment