Skip to content

Commit

Permalink
added API call to RAG Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
rathijitpapon committed Apr 5, 2024
1 parent e4ae334 commit 66574d0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
3 changes: 3 additions & 0 deletions server/config/dev.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
db = "postgresql://postgres:postgres@localhost/curieo"
cache = "redis://127.0.0.1/"
rag_api = "http://127.0.0.1:8000"
rag_api_username = "curieo"
rag_api_password = "curieo"

[log]
level = "info"
Expand Down
6 changes: 3 additions & 3 deletions server/migrations/20240403115016_search_history.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
create table search_history (
search_history_id uuid primary key default uuid_generate_v1mc(),
search_text text not null,
response_text text not null,
response_sources text[] not null,
query text not null,
result text not null,
sources text[] not null,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
Expand Down
16 changes: 11 additions & 5 deletions server/src/search/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use sqlx::types::time;
use sqlx::FromRow;
use std::fmt::Debug;

#[derive(Serialize, Deserialize, Debug)]
pub struct RAGTokenResponse {
pub access_token: String,
pub token_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SearchQueryRequest {
pub query: String,
Expand All @@ -16,17 +22,17 @@ pub struct SearchHistoryRequest {

#[derive(Serialize, Deserialize, Debug)]
pub struct SearchResponse {
pub response_text: String,
pub response_sources: Vec<String>,
pub result: String,
pub sources: Vec<String>,
}

#[derive(FromRow, Serialize, Deserialize, Clone, Debug)]
pub struct SearchHistory {
pub search_history_id: uuid::Uuid,
// pub user_id: uuid::Uuid,
pub search_text: String,
pub response_text: String,
pub response_sources: Vec<String>,
pub query: String,
pub result: String,
pub sources: Vec<String>,

pub created_at: time::OffsetDateTime,
pub updated_at: time::OffsetDateTime,
Expand Down
53 changes: 34 additions & 19 deletions server/src/search/services.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
use crate::err::AppError;
use crate::search::{SearchHistory, SearchQueryRequest, SearchResponse};
use crate::search::{RAGTokenResponse, SearchHistory, SearchQueryRequest, SearchResponse};
use crate::settings::SETTINGS;
use color_eyre::eyre::eyre;
use redis::aio::MultiplexedConnection;
use redis::AsyncCommands;
use reqwest::Client as ReqwestClient;
use sqlx::PgPool;

#[tracing::instrument(level = "debug", ret, err)]
pub async fn search(
cache: &mut MultiplexedConnection,
search_query: &SearchQueryRequest,
) -> crate::Result<SearchResponse> {
let cache_response: Option<String> = cache
let cache_response: Option<SearchResponse> = cache
.get(search_query.query.clone())
.await
.map(|response: Option<String>| {
response.and_then(|response| serde_json::from_str(&response).ok())
})
.map_err(|e| AppError::from(e))?;

let cache_response = match cache_response {
Some(response) => response,
None => String::new(),
};

let cache_response: Option<SearchResponse> =
serde_json::from_str(&cache_response).unwrap_or(None);

if let Some(response) = cache_response {
return Ok(response);
}

// sleep for 3 seconds to simulate a slow search
// TODO: replace this with actual search logic using GRPC calls with backend services
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
let rag_api_url = SETTINGS.rag_api.clone() + "/token";
let form_data = [
("username", &SETTINGS.rag_api_username.expose()),
("password", &SETTINGS.rag_api_password.expose()),
];
let token: RAGTokenResponse = ReqwestClient::new()
.post(rag_api_url)
.form(&form_data)
.send()
.await
.map_err(|_| eyre!("unable to send request to rag api"))?
.json()
.await
.map_err(|_| eyre!("unable to parse json response from rag api"))?;

let response = SearchResponse {
response_text: "sample response".to_string(),
response_sources: vec!["www.source1.com".to_string(), "www.source2.com".to_string()],
};
let rag_api_url = SETTINGS.rag_api.clone() + "/search?query=" + &search_query.query;
let response: SearchResponse = ReqwestClient::new()
.get(rag_api_url)
.header("Authorization", format!("Bearer {}", token.access_token))
.send()
.await
.map_err(|_| eyre!("unable to send request to rag api"))?
.json()
.await
.map_err(|_| eyre!("unable to parse json response from rag api"))?;

return Ok(response);
}
Expand All @@ -57,10 +72,10 @@ pub async fn insert_search_history(

let search_history = sqlx::query_as!(
SearchHistory,
"insert into search_history (search_text, response_text, response_sources) values ($1, $2, $3) returning *",
"insert into search_history (query, result, sources) values ($1, $2, $3) returning *",
&search_query.query,
&search_response.response_text,
&search_response.response_sources
&search_response.result,
&search_response.sources
)
.fetch_one(pool)
.await
Expand Down
3 changes: 3 additions & 0 deletions server/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub struct Settings {
pub port: u16,
pub db: Secret<String>,
pub cache: Secret<String>,
pub rag_api: String,
pub rag_api_username: Secret<String>,
pub rag_api_password: Secret<String>,
}

impl Settings {
Expand Down

0 comments on commit 66574d0

Please sign in to comment.