File size: 1,241 Bytes
a8c4fcf
 
 
 
43270c7
a8c4fcf
 
 
 
68fc6df
a8c4fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use axum::{Json, extract::State, http::StatusCode};
use utoipa_axum::{router::OpenApiRouter, routes};

use crate::{
    constants::OPENAPI_TAG,
    models::{
        dtos::vectors::keyframes::{VectorizedKeyframeDto, VectorizedKeyframeRequestDto},
        states::AppState,
    },
    services::queries::keyframes::VectorizedKeyframeService,
};

pub fn router() -> OpenApiRouter<AppState> {
    OpenApiRouter::new().routes(routes!(find_nearest_top_k_by_text))
}

#[utoipa::path(
    post,
    path = "/searches",
    tag = OPENAPI_TAG,
    request_body = VectorizedKeyframeRequestDto,
    responses(
        (status = 200, description = "Top k documents nearest to prompt", body = Vec<VectorizedKeyframeDto>)
    )
)]
async fn find_nearest_top_k_by_text(
    State(state): State<AppState>,
    Json(dto): Json<VectorizedKeyframeRequestDto>,
) -> Result<Json<Vec<VectorizedKeyframeDto>>, StatusCode> {
    let service = VectorizedKeyframeService::from(&state);
    match service
        .find_nearest_top_k_by_text(dto.prompt(), dto.top_k())
        .await
        .map(Json)
    {
        Ok(value) => Ok(value),
        Err(e) => {
            tracing::error!("{:?}", e);
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }
    }
}