feat: generate title

This commit is contained in:
2026-05-11 18:28:04 +02:00
parent 1bf4cf2ca3
commit b5ef7e4881
3 changed files with 102 additions and 6 deletions
+52 -3
View File
@@ -1,4 +1,7 @@
use crate::middlewares::auth::middleware::Auth;
use crate::{
databases::postgres::chat::ConversationState, dto::api::BaseLLMRequest,
middlewares::auth::middleware::Auth, providers::ollama::client::OllamaProvider,
};
use axum::{
Json,
extract::{Extension, State},
@@ -8,8 +11,9 @@ use axum::{
},
};
use crate::databases::postgres::chat::get_or_create_conversation;
use crate::databases::postgres::chat::{get_or_create_conversation, set_conversation_title};
use crate::dto::api;
use crate::dto::api::CompletionRequest;
use crate::providers::ollama::errors::into_http_response;
use crate::state::app_state::AppState;
@@ -128,11 +132,35 @@ pub async fn chat_completions(
body.conversation_id.is_some()
);
let conversation_id =
let conversation_state =
get_or_create_conversation(&state.postgres, body.conversation_id, auth.user_id())
.await
.map_err(crate::databases::postgres::errors::into_http_response)?;
let conversation_id = match &conversation_state {
ConversationState::Existing(uuid) => *uuid,
ConversationState::Created(uuid) => {
let conversation_id = *uuid;
let title =
generate_conversation_title(&state.ollama, body.messages[0].content.as_str())
.await;
if let Err(e) =
set_conversation_title(&state.postgres, conversation_id, auth.user_id(), &title)
.await
{
tracing::warn!(
conversation_id = %conversation_id,
error = %e,
"Failed to set conversation title, continuing with no title"
);
}
tracing::debug!(conversation_id = %conversation_id, %title, "generated conversation title");
conversation_id
}
};
tracing::debug!("Using conversation_id: {:?}", conversation_id);
}
@@ -156,3 +184,24 @@ pub async fn chat_completions(
Ok(Json(response).into_response())
}
}
async fn generate_conversation_title(ollama: &OllamaProvider, first_message: &str) -> String {
let request = CompletionRequest {
base: BaseLLMRequest {
model: "llama3:latest".to_string(),
..Default::default()
},
prompt: format!(
"Generate a short title (max 6 words) for the following chat conversation: {}",
first_message
),
};
ollama
.completions(&request)
.await
.ok()
.and_then(|r| r.choices.first().map(|c| c.text.trim().to_string()))
.filter(|t| !t.is_empty())
.unwrap_or_else(|| "New Conversation".to_string()) // ← default on any error
}