From c91aa461851aa31c7bb89d80e2f8f4a26f9761bd Mon Sep 17 00:00:00 2001 From: dal Date: Thu, 13 Mar 2025 15:29:19 -0600 Subject: [PATCH] raw_llm_message endpoint for evals --- .../src/chats/get_raw_llm_messages_handler.rs | 30 +++++++++++++++++++ api/libs/handlers/src/chats/mod.rs | 2 ++ .../routes/chats/get_chat_raw_llm_messages.rs | 21 +++++++++++++ api/src/routes/rest/routes/chats/mod.rs | 3 ++ 4 files changed, 56 insertions(+) create mode 100644 api/libs/handlers/src/chats/get_raw_llm_messages_handler.rs create mode 100644 api/src/routes/rest/routes/chats/get_chat_raw_llm_messages.rs diff --git a/api/libs/handlers/src/chats/get_raw_llm_messages_handler.rs b/api/libs/handlers/src/chats/get_raw_llm_messages_handler.rs new file mode 100644 index 000000000..6d7f2f6e3 --- /dev/null +++ b/api/libs/handlers/src/chats/get_raw_llm_messages_handler.rs @@ -0,0 +1,30 @@ +use anyhow::Result; +use diesel::prelude::*; +use diesel_async::RunQueryDsl; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use uuid::Uuid; + +use database::{pool::get_pg_pool, schema::messages}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct GetRawLlmMessagesRequest { + pub chat_id: Uuid, +} + +pub async fn get_raw_llm_messages_handler(chat_id: Uuid) -> Result { + let pool = get_pg_pool(); + let mut conn = pool.get().await?; + + // Get messages for the chat, ordered by creation time + let raw_llm_messages: Value = messages::table + .filter(messages::chat_id.eq(chat_id)) + .filter(messages::deleted_at.is_null()) + .order_by(messages::created_at.desc()) + .select(messages::raw_llm_messages) + .order_by(messages::created_at.desc()) + .first::(&mut conn) + .await?; + + Ok(raw_llm_messages) +} diff --git a/api/libs/handlers/src/chats/mod.rs b/api/libs/handlers/src/chats/mod.rs index a36afa343..da9791951 100644 --- a/api/libs/handlers/src/chats/mod.rs +++ b/api/libs/handlers/src/chats/mod.rs @@ -1,4 +1,5 @@ pub mod get_chat_handler; +pub mod get_raw_llm_messages_handler; pub mod post_chat_handler; pub mod update_chats_handler; pub mod delete_chats_handler; @@ -8,6 +9,7 @@ pub mod context_loaders; pub mod list_chats_handler; pub use get_chat_handler::get_chat_handler; +pub use get_raw_llm_messages_handler::get_raw_llm_messages_handler; pub use post_chat_handler::post_chat_handler; pub use update_chats_handler::update_chats_handler; pub use delete_chats_handler::delete_chats_handler; diff --git a/api/src/routes/rest/routes/chats/get_chat_raw_llm_messages.rs b/api/src/routes/rest/routes/chats/get_chat_raw_llm_messages.rs new file mode 100644 index 000000000..512d39695 --- /dev/null +++ b/api/src/routes/rest/routes/chats/get_chat_raw_llm_messages.rs @@ -0,0 +1,21 @@ +use axum::{ + extract::{Path, State}, + Extension, Json, + http::StatusCode, + response::IntoResponse, +}; +use handlers::chats::get_raw_llm_messages_handler; +use middleware::AuthenticatedUser; +use serde_json::Value; +use uuid::Uuid; +use crate::routes::rest::ApiResponse; + +pub async fn get_chat_raw_llm_messages( + Extension(user): Extension, + Path(chat_id): Path, +) -> Result, (StatusCode, &'static str)> { + match get_raw_llm_messages_handler(chat_id).await { + Ok(response) => Ok(ApiResponse::JsonData(response)), + Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, "Failed to get raw LLM messages")), + } +} diff --git a/api/src/routes/rest/routes/chats/mod.rs b/api/src/routes/rest/routes/chats/mod.rs index 1bdc0074c..dd05489a9 100644 --- a/api/src/routes/rest/routes/chats/mod.rs +++ b/api/src/routes/rest/routes/chats/mod.rs @@ -5,12 +5,14 @@ use axum::{ mod delete_chats; mod get_chat; +mod get_chat_raw_llm_messages; mod list_chats; mod post_chat; mod update_chats; pub use delete_chats::delete_chats_route; pub use get_chat::get_chat_route; +pub use get_chat_raw_llm_messages::get_chat_raw_llm_messages; pub use list_chats::list_chats_route; pub use post_chat::post_chat_route; pub use update_chats::update_chats_route; @@ -22,4 +24,5 @@ pub fn router() -> Router { .route("/", put(update_chats_route)) .route("/", delete(delete_chats_route)) .route("/:id", get(get_chat_route)) + .route("/:id/raw_llm_messages", get(get_chat_raw_llm_messages)) }