Use openai to generate chat thread titles

This commit is contained in:
Benjamin Sherriff
2024-01-29 11:39:52 -05:00
parent d04c34d555
commit 096a47b96e
4 changed files with 38 additions and 7 deletions

View File

@@ -65,8 +65,9 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
}; };
// Get the thread channel ID // Get the thread channel ID
let thread_name = generate_thread_name(oai, &parsed_content, 99).await;
let response_channel = match msg.channel_id.create_private_thread(&ctx.http, |thread| { let response_channel = match msg.channel_id.create_private_thread(&ctx.http, |thread| {
thread.name(truncate(&parsed_content, 99)).kind(ChannelType::PublicThread) thread.name(thread_name).kind(ChannelType::PublicThread)
}).await { }).await {
Ok(c) => { Ok(c) => {
let allow = Permissions::SEND_MESSAGES; let allow = Permissions::SEND_MESSAGES;
@@ -140,9 +141,40 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
// }; // };
} }
fn truncate(s: &str, max_chars: usize) -> &str { async fn generate_thread_name(oai: &OAI, s: &str, max_chars: usize) -> String {
match s.char_indices().nth(max_chars) { println!("HERE: {}", s);
let message = ChatCompletionMessage {
role: GPTRole::User,
content: format!("---\n{}\n---\nSummarize the message above into a concise Discord thread title", s)
};
let request = ChatCompletionRequest {
model: oai.default_model.clone(),
messages: vec![message],
temperature: Some(0.5),
top_p: None,
n: None,
max_tokens: Some(oai.max_tokens),
presence_penalty: Some(0.6),
frequency_penalty: Some(0.0),
user: None
};
// Truncate the response to the max number of characters
let mut response = match s.char_indices().nth(max_chars) {
None => s, None => s,
Some((idx, _)) => &s[..idx], Some((idx, _)) => &s[..idx]
}.to_string();
// Set the response to the OAI response
match oai.chat_completion(request).await {
Ok(r) => {
if !r.choices.is_empty() {
response = r.choices[0].message.content.clone();
} else {
warn!("No choices received in the response from OpenAI");
} }
}
Err(err) => {
error!("Could not get response from OpenAI: {}", err.message);
}
};
return response;
} }

View File

@@ -1,6 +1,5 @@
pub mod audio; pub mod audio;
pub mod help; pub mod help;
pub mod message;
pub mod chat; pub mod chat;
pub mod ping; pub mod ping;
pub mod schedule; pub mod schedule;