Override session name generator for ollama provider (#3710)

This commit is contained in:
Angela Ning
2025-08-02 13:36:21 -04:00
committed by GitHub
parent 9cd07b88c9
commit d93063b686
2 changed files with 92 additions and 25 deletions
+27 -25
View File
@@ -340,30 +340,22 @@ pub trait Provider: Send + Sync {
}
}
/// Generate a session name/description based on the conversation history
/// This method can be overridden by providers to implement custom session naming strategies.
/// The default implementation creates a prompt asking for a concise description in 4 words or less.
async fn generate_session_name(&self, messages: &[Message]) -> Result<String, ProviderError> {
// Create a prompt for a concise description
let mut description_prompt = "Based on the conversation so far, provide a concise description of this session in 4 words or less. This will be used for finding the session later in a UI with limited space - reply *ONLY* with the description".to_string();
// Get context from the first 3 user messages
let context: Vec<String> = messages
/// Returns the first 3 user messages as strings for session naming
fn get_initial_user_messages(&self, messages: &[Message]) -> Vec<String> {
messages
.iter()
.filter(|m| m.role == rmcp::model::Role::User)
.take(3)
.map(|m| m.as_concat_text())
.collect();
.collect()
}
if !context.is_empty() {
description_prompt = format!(
"Here are the first few user messages:\n{}\n\n{}",
context.join("\n"),
description_prompt
);
}
let message = Message::user().with_text(&description_prompt);
/// Generate a session name/description based on the conversation history
/// Creates a prompt asking for a concise description in 4 words or less.
async fn generate_session_name(&self, messages: &[Message]) -> Result<String, ProviderError> {
let context = self.get_initial_user_messages(messages);
let prompt = self.create_session_name_prompt(&context);
let message = Message::user().with_text(&prompt);
let result = self
.complete(
"Reply with only a description in four words or less",
@@ -373,13 +365,23 @@ pub trait Provider: Send + Sync {
.await?;
let description = result.0.as_concat_text();
let sanitized_description = if description.chars().count() > 100 {
safe_truncate(&description, 100)
} else {
description
};
Ok(sanitized_description)
Ok(safe_truncate(&description, 100))
}
// Generate a prompt for a session name based on the conversation history
fn create_session_name_prompt(&self, context: &[String]) -> String {
// Create a prompt for a concise description
let mut prompt = "Based on the conversation so far, provide a concise description of this session in 4 words or less. This will be used for finding the session later in a UI with limited space - reply *ONLY* with the description".to_string();
if !context.is_empty() {
prompt = format!(
"Here are the first few user messages:\n{}\n\n{}",
context.join("\n"),
prompt
);
}
prompt
}
}