File size: 1,157 Bytes
cf57c77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use crate::long_term_memory::search_messages;
use crate::models::{AppState, SearchPayload};
use actix_web::{error, post, web, HttpResponse, Responder};
use std::ops::Deref;
use std::sync::Arc;

#[post("/sessions/{session_id}/retrieval")]
pub async fn run_retrieval(
    session_id: web::Path<String>,
    web::Json(payload): web::Json<SearchPayload>,
    data: web::Data<Arc<AppState>>,
    redis: web::Data<redis::Client>,
) -> actix_web::Result<impl Responder> {
    if !data.long_term_memory {
        return Ok(HttpResponse::BadRequest().body("Long term memory is disabled"));
    }

    let conn = redis
        .get_tokio_connection_manager()
        .await
        .map_err(error::ErrorInternalServerError)?;

    let client_wrapper = data.openai_pool.get().await.unwrap();
    let openai_client = client_wrapper.deref();

    match search_messages(payload.text, session_id.clone(), openai_client, conn).await {
        Ok(results) => Ok(HttpResponse::Ok().json(results)),
        Err(e) => {
            log::error!("Error Retrieval API: {:?}", e);
            Ok(HttpResponse::InternalServerError().body("Internal server error"))
        }
    }
}