Spaces:
Paused

miracle / scripts /seed_sample_kb.py
dushyantsingh0
backend stabilization
812e16e
Raw History Blame Contribute Delete
3.09 kB
import asyncio
import os
import sys
from uuid import UUID
# Add project root to sys.path
sys.path.append(os.getcwd())
from core.database import async_session_factory
from sqlalchemy import select
from models.user import User
from models.kb_article import KBArticle
from api.v1.kb import generate_article_embedding
WORKSPACE_ID = UUID('e7f0b255-1f87-42f9-8d8a-58cd70ce87a0')
SAMPLE_ARTICLES = [
{
"title": "Welcome to Nexus: Getting Started",
"category": "General",
"content": "Welcome to the Nexus AI Platform! To get started, you can create your first ticket by clicking the 'New Ticket' button on the dashboard. Our AI agent will automatically prioritize and route it to the correct team."
},
{
"title": "Password Security Best Practices",
"category": "Security",
"content": "To keep your nexus account secure, always use a strong, unique password. We recommend using a password manager and avoiding common words or birthdays. Never share your password with anyone, including Nexus support staff."
},
{
"title": "Configuring Project Notifications",
"category": "Project Management",
"content": "You can customize how you receive notifications by going to Settings > Notifications. You can choose to receive email alerts for mentions, status changes, or any activity on your assigned tickets."
}
]
async def seed_and_publish():
print(f"Seeding sample articles for workspace {WORKSPACE_ID}...")
async with async_session_factory() as db:
# Find a user to act as the author
user_stmt = select(User).where(User._legacy_workspace_id == WORKSPACE_ID).limit(1)
user_res = await db.execute(user_stmt)
author = user_res.scalar_one_or_none()
if not author:
print(f"Error: No users found in workspace {WORKSPACE_ID}. Cannot seed articles.")
return
print(f"Using author: {author.email} ({author.id})")
new_ids = []
for sample in SAMPLE_ARTICLES:
article = KBArticle(
workspace_id=WORKSPACE_ID,
author_id=author.id,
title=sample["title"],
content=sample["content"],
category=sample["category"],
is_published=True # Bulk publish immediately
)
db.add(article)
await db.flush() # Get the new ID
new_ids.append(article.id)
print(f"Created article: {sample['title']}")
await db.commit()
print(f"Successfully committed {len(new_ids)} articles.")
# Trigger re-indexing for each new article
print("Triggering re-indexing tasks...")
for art_id in new_ids:
try:
await generate_article_embedding(db, art_id)
print(f"Re-indexed: {art_id}")
except Exception as e:
print(f"Error re-indexing {art_id}: {e}")
print("Done!")
if __name__ == "__main__":
asyncio.run(seed_and_publish())