feat: add AI assistant with in-session copilot and standalone chat with RAG
Implements three-phase AI assistant feature: - Phase 0: RAG infrastructure with pgvector embeddings, Voyage AI integration, tree chunking service, and semantic search over team's flow library - Phase 1: In-session copilot panel during flow navigation with contextual AI help, current step awareness, and suggested related flows - Phase 2: Standalone AI chat page with persistent conversation history, pin/delete, and configurable retention policies (account-level) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
78
backend/app/services/embedding_service.py
Normal file
78
backend/app/services/embedding_service.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""Embedding provider abstraction for RAG.
|
||||
|
||||
Uses Voyage AI (voyage-3.5, 1024 dims) as the embedding provider.
|
||||
Supports document and query input types for asymmetric search.
|
||||
"""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def get_embedding(
|
||||
text: str,
|
||||
input_type: str = "document",
|
||||
) -> Optional[list[float]]:
|
||||
"""Get embedding vector for text using Voyage AI.
|
||||
|
||||
Args:
|
||||
text: The text to embed.
|
||||
input_type: "document" for indexing, "query" for search queries.
|
||||
|
||||
Returns:
|
||||
List of floats (1024 dims) or None if embedding service unavailable.
|
||||
"""
|
||||
if not settings.VOYAGE_API_KEY:
|
||||
logger.warning("VOYAGE_API_KEY not set — embedding service unavailable")
|
||||
return None
|
||||
|
||||
try:
|
||||
import voyageai
|
||||
|
||||
client = voyageai.AsyncClient(api_key=settings.VOYAGE_API_KEY)
|
||||
result = await client.embed(
|
||||
texts=[text],
|
||||
model=settings.EMBEDDING_MODEL,
|
||||
input_type=input_type,
|
||||
)
|
||||
return result.embeddings[0]
|
||||
except Exception as e:
|
||||
logger.error("Embedding failed: %s", e)
|
||||
return None
|
||||
|
||||
|
||||
async def get_embeddings_batch(
|
||||
texts: list[str],
|
||||
input_type: str = "document",
|
||||
) -> Optional[list[list[float]]]:
|
||||
"""Get embedding vectors for multiple texts in a single API call.
|
||||
|
||||
Args:
|
||||
texts: List of texts to embed.
|
||||
input_type: "document" for indexing, "query" for search queries.
|
||||
|
||||
Returns:
|
||||
List of embedding vectors or None if service unavailable.
|
||||
"""
|
||||
if not texts:
|
||||
return []
|
||||
|
||||
if not settings.VOYAGE_API_KEY:
|
||||
logger.warning("VOYAGE_API_KEY not set — embedding service unavailable")
|
||||
return None
|
||||
|
||||
try:
|
||||
import voyageai
|
||||
|
||||
client = voyageai.AsyncClient(api_key=settings.VOYAGE_API_KEY)
|
||||
result = await client.embed(
|
||||
texts=texts,
|
||||
model=settings.EMBEDDING_MODEL,
|
||||
input_type=input_type,
|
||||
)
|
||||
return result.embeddings
|
||||
except Exception as e:
|
||||
logger.error("Batch embedding failed: %s", e)
|
||||
return None
|
||||
Reference in New Issue
Block a user