PicoBot/src/topic_description.rs

27 lines
903 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use crate::providers::{ChatCompletionRequest, LLMProvider, Message};
pub async fn generate_topic_description(
provider: &dyn LLMProvider,
first_user_message: &str,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let prompt = format!(
"请根据用户的第一句话用简短的词语不超过15字描述这个对话的主题或意图。只输出描述内容不要其他解释。\n\n用户消息:{}",
first_user_message
);
let request = ChatCompletionRequest {
messages: vec![Message::user(prompt)],
temperature: Some(0.3),
max_tokens: Some(50),
tools: None,
};
let response = provider.chat(request).await?;
let description = response.content.trim();
if description.len() > 50 {
Ok(description.chars().take(50).collect())
} else {
Ok(description.to_string())
}
}