| from fastapi import APIRouter, HTTPException |
|
|
| from app.integrations.ollama import get_chunking_ollama |
| from app.schemas.chunking import ( |
| ChunkEmbedRequest, |
| ChunkEmbedResponse, |
| ChunkEmbeddingResult, |
| ChunkPreviewRequest, |
| ChunkPreviewResponse, |
| ) |
| from app.services.heuristics import ScientificChunker |
|
|
|
|
| router = APIRouter(prefix="/chunking", tags=["chunking"]) |
| chunker = ScientificChunker() |
|
|
|
|
| @router.post("/preview", response_model=ChunkPreviewResponse) |
| def preview_chunks(payload: ChunkPreviewRequest) -> ChunkPreviewResponse: |
| chunks = chunker.preview(payload) |
| return ChunkPreviewResponse(chunk_count=len(chunks), chunks=chunks) |
|
|
|
|
| @router.post("/embed", response_model=ChunkEmbedResponse) |
| def embed_chunks(payload: ChunkEmbedRequest) -> ChunkEmbedResponse: |
| ollama = get_chunking_ollama() |
| if not ollama.settings.enabled: |
| raise HTTPException(status_code=503, detail="Ollama embedding is disabled.") |
| if not ollama.available: |
| raise HTTPException(status_code=503, detail="Ollama is not reachable.") |
| results: list[ChunkEmbeddingResult] = [] |
| for text in payload.texts: |
| vector = ollama.embed(text) |
| results.append( |
| ChunkEmbeddingResult( |
| text=text, |
| embedding=vector, |
| embedding_model=ollama.settings.embedding_model, |
| dimensions=len(vector), |
| ) |
| ) |
| return ChunkEmbedResponse(embeddings=results) |
|
|