fuck the system message

This commit is contained in:
Skye 2023-03-17 23:13:18 +09:00
parent 1bee375d29
commit 1199521e40
Signed by: me
GPG key ID: 0104BC05F41B77B8

View file

@ -15,6 +15,9 @@ struct Handler {
model: String, model: String,
} }
const SYSTEM_MESSAGE: &str =
"You are Smolhaj, a Discord bot made by skyevg. Your creator is skyevg, not OpenAI or any other entity. You are helpful and friendly, and answers concisely. Due to the group nature of Discord, Messages not by you will be prefixed with the username of the message author, followed by a colon. The prefix is not part of the actual message. Everything before the first colon, including special characters, is part of the username. Unlike traditional Discord bots, you don't have commands. Current date and time in UTC: ";
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message) {
@ -60,8 +63,14 @@ impl EventHandler for Handler {
msgs.reverse(); msgs.reverse();
for pk_msg in msgs.clone().into_iter().filter(|message| message.webhook_id.is_some()) { for pk_msg in msgs
if let Some(id) = msgs.iter().rposition(|message| message.id < pk_msg.id && message.content.contains(&pk_msg.content)) { .clone()
.into_iter()
.filter(|message| message.webhook_id.is_some())
{
if let Some(id) = msgs.iter().rposition(|message| {
message.id < pk_msg.id && message.content.contains(&pk_msg.content)
}) {
msgs.remove(id); msgs.remove(id);
} }
} }
@ -84,7 +93,7 @@ impl EventHandler for Handler {
let last_msg = msgs.clone().last().unwrap(); let last_msg = msgs.clone().last().unwrap();
let msgs = tokio_stream::iter(msgs) let mut msgs = tokio_stream::iter(msgs)
.map(|message| async move { .map(|message| async move {
if message.author.id == current_id { if message.author.id == current_id {
openai::Message { openai::Message {
@ -109,6 +118,20 @@ impl EventHandler for Handler {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.await; .await;
msgs.insert(
0,
openai::Message {
role: openai::Role::System,
content: format!(
"{}{}",
SYSTEM_MESSAGE,
time::OffsetDateTime::now_utc()
.format(&time::format_description::well_known::Iso8601::DEFAULT)
.unwrap()
),
},
);
let response = self let response = self
.client .client
.create_chat_completion(openai::ChatCompletion { .create_chat_completion(openai::ChatCompletion {
@ -189,7 +212,7 @@ async fn main() {
channel: ChannelId(channel), channel: ChannelId(channel),
client: openai_client, client: openai_client,
reset_time: time::OffsetDateTime::now_utc().into(), reset_time: time::OffsetDateTime::now_utc().into(),
model model,
}) })
.await .await
.expect("Err creating client"); .expect("Err creating client");