File size: 1,458 Bytes
08fd094
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)