Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9e4ab1bdb | ||
|
|
d9a045a5e4 | ||
|
|
393be9be5a |
@@ -1,8 +1,12 @@
|
|||||||
|
# (2024-10-03) Version 1.3.1
|
||||||
|
|
||||||
|
- (**Improvement**) Improves fallback user mentions support for old clients (like Element iOS) which use the bot's display name (not its full Matrix User ID). ([d9a045a5e4](https://github.com/etkecc/baibot/commit/d9a045a5e41d2b99694f92ec9e90f47529546d89))
|
||||||
|
|
||||||
|
|
||||||
# (2024-10-03) Version 1.3.0
|
# (2024-10-03) Version 1.3.0
|
||||||
|
|
||||||
**TLDR**: you can now use OpenAI's [o1](https://platform.openai.com/docs/models/o1) models, benefit from [prompt caching](https://platform.openai.com/docs/guides/prompt-caching) and mention the bot again from old clients lacking proper [user mentions support](https://spec.matrix.org/latest/client-server-api/#user-and-room-mentions) (like Element iOS).
|
**TLDR**: you can now use OpenAI's [o1](https://platform.openai.com/docs/models/o1) models, benefit from [prompt caching](https://platform.openai.com/docs/guides/prompt-caching) and mention the bot again from old clients lacking proper [user mentions support](https://spec.matrix.org/latest/client-server-api/#user-and-room-mentions) (like Element iOS).
|
||||||
|
|
||||||
|
|
||||||
- (**Feature**) Introduces a new `baibot_conversation_start_time_utc` [prompt variable](./docs/configuration/text-generation.md#️-prompt-override) which is not a moving target (like the `baibot_now_utc` variable) and allows [prompt caching](https://platform.openai.com/docs/guides/prompt-caching) to work. All default/sample configs have been adjusted to make use of this new variable, but users need to adjust your existing dynamically-created agents to start using it. ([85e66406dc](https://github.com/etkecc/baibot/commit/85e66406dc6f430741c7819f420e2df4ae6e8d3b))
|
- (**Feature**) Introduces a new `baibot_conversation_start_time_utc` [prompt variable](./docs/configuration/text-generation.md#️-prompt-override) which is not a moving target (like the `baibot_now_utc` variable) and allows [prompt caching](https://platform.openai.com/docs/guides/prompt-caching) to work. All default/sample configs have been adjusted to make use of this new variable, but users need to adjust your existing dynamically-created agents to start using it. ([85e66406dc](https://github.com/etkecc/baibot/commit/85e66406dc6f430741c7819f420e2df4ae6e8d3b))
|
||||||
|
|
||||||
- (**Improvement**) Allows for the `max_response_tokens` configuration value for the [OpenAI provider](./docs/providers.md#openai) to be set to `null` to allow [o1](https://platform.openai.com/docs/models/o1) models (which do not support `max_response_tokens`) to be used. See the new o1 sample config [here](./docs/sample-provider-configs/openai-o1.yml). ([db9422740c](https://github.com/etkecc/baibot/commit/db9422740ceca32956d9628b6326b8be206344e2))
|
- (**Improvement**) Allows for the `max_response_tokens` configuration value for the [OpenAI provider](./docs/providers.md#openai) to be set to `null` to allow [o1](https://platform.openai.com/docs/models/o1) models (which do not support `max_response_tokens`) to be used. See the new o1 sample config [here](./docs/sample-provider-configs/openai-o1.yml). ([db9422740c](https://github.com/etkecc/baibot/commit/db9422740ceca32956d9628b6326b8be206344e2))
|
||||||
|
|||||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -291,7 +291,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "baibot"
|
name = "baibot"
|
||||||
version = "1.3.0"
|
version = "1.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anthropic-rs",
|
"anthropic-rs",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ license = "AGPL-3.0-or-later"
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keywords = ["matrix", "chat", "bot", "AI", "LLM"]
|
keywords = ["matrix", "chat", "bot", "AI", "LLM"]
|
||||||
include = ["/etc/assets/baibot-torso-768.png", "/src", "/README.md", "/CHANGELOG.md", "/LICENSE"]
|
include = ["/etc/assets/baibot-torso-768.png", "/src", "/README.md", "/CHANGELOG.md", "/LICENSE"]
|
||||||
version = "1.3.0"
|
version = "1.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|||||||
@@ -172,6 +172,24 @@ impl Bot {
|
|||||||
self.matrix_link().user_id()
|
self.matrix_link().user_id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn user_display_name_in_room(&self, room: &Room) -> Option<String> {
|
||||||
|
let bot_display_name = self
|
||||||
|
.room_display_name_fetcher()
|
||||||
|
.own_display_name_in_room(room)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match bot_display_name {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(err) => {
|
||||||
|
tracing::warn!(
|
||||||
|
?err,
|
||||||
|
"Failed to fetch bot display name. Proceeding without it"
|
||||||
|
);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn reacting(&self) -> super::reacting::Reacting {
|
pub(crate) fn reacting(&self) -> super::reacting::Reacting {
|
||||||
super::reacting::Reacting::new(self.clone())
|
super::reacting::Reacting::new(self.clone())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -239,8 +239,11 @@ impl Messaging {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let bot_display_name = self.bot.user_display_name_in_room(&room).await;
|
||||||
|
|
||||||
let interaction_context = determine_interaction_context_for_room_event(
|
let interaction_context = determine_interaction_context_for_room_event(
|
||||||
self.bot.user_id(),
|
self.bot.user_id(),
|
||||||
|
&bot_display_name,
|
||||||
&room,
|
&room,
|
||||||
&event,
|
&event,
|
||||||
&payload,
|
&payload,
|
||||||
@@ -279,7 +282,8 @@ impl Messaging {
|
|||||||
self.bot.admin_pattern_regexes().clone(),
|
self.bot.admin_pattern_regexes().clone(),
|
||||||
trigger_event_info,
|
trigger_event_info,
|
||||||
interaction_context.thread_info.clone(),
|
interaction_context.thread_info.clone(),
|
||||||
);
|
)
|
||||||
|
.with_bot_display_name(bot_display_name);
|
||||||
|
|
||||||
let controller_type = crate::controller::determine_controller(
|
let controller_type = crate::controller::determine_controller(
|
||||||
self.bot.command_prefix(),
|
self.bot.command_prefix(),
|
||||||
|
|||||||
@@ -383,24 +383,10 @@ async fn handle_stage_text_generation(
|
|||||||
_ => vec![],
|
_ => vec![],
|
||||||
};
|
};
|
||||||
|
|
||||||
let bot_display_name = bot
|
let bot_user_prefixes_to_strip = create_list_of_bot_user_prefixes_to_strip(
|
||||||
.room_display_name_fetcher()
|
bot.user_id(),
|
||||||
.own_display_name_in_room(message_context.room())
|
message_context.bot_display_name(),
|
||||||
.await;
|
);
|
||||||
|
|
||||||
let bot_display_name = match bot_display_name {
|
|
||||||
Ok(value) => value,
|
|
||||||
Err(err) => {
|
|
||||||
tracing::warn!(
|
|
||||||
?err,
|
|
||||||
"Failed to fetch bot display name. Proceeding without it"
|
|
||||||
);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let bot_user_prefixes_to_strip =
|
|
||||||
create_list_of_bot_user_prefixes_to_strip(bot.user_id(), &bot_display_name);
|
|
||||||
|
|
||||||
let allowed_users = match controller_type {
|
let allowed_users = match controller_type {
|
||||||
// Regular chat completion only operates on messages from allowed users.
|
// Regular chat completion only operates on messages from allowed users.
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ use mxlink::matrix_sdk::{
|
|||||||
ruma::events::{
|
ruma::events::{
|
||||||
relation::Thread,
|
relation::Thread,
|
||||||
room::message::{
|
room::message::{
|
||||||
MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEventContent,
|
sanitize::remove_plain_reply_fallback, MessageType, OriginalSyncRoomMessageEvent,
|
||||||
|
Relation, RoomMessageEventContent,
|
||||||
},
|
},
|
||||||
AnyMessageLikeEvent, AnyMessageLikeEventContent, AnyTimelineEvent, MessageLikeEvent,
|
AnyMessageLikeEvent, AnyMessageLikeEventContent, AnyTimelineEvent, MessageLikeEvent,
|
||||||
},
|
},
|
||||||
@@ -229,7 +230,7 @@ pub fn convert_matrix_native_event_to_matrix_message(
|
|||||||
let text = if is_reply {
|
let text = if is_reply {
|
||||||
// For regular replies, we need to strip the fallback-for-rich replies part.
|
// For regular replies, we need to strip the fallback-for-rich replies part.
|
||||||
// See: https://spec.matrix.org/v1.11/client-server-api/#fallbacks-for-rich-replies
|
// See: https://spec.matrix.org/v1.11/client-server-api/#fallbacks-for-rich-replies
|
||||||
strip_rich_reply_fallback_text(&text)
|
remove_plain_reply_fallback(&text).to_owned()
|
||||||
} else {
|
} else {
|
||||||
text
|
text
|
||||||
};
|
};
|
||||||
@@ -276,13 +277,14 @@ pub fn convert_matrix_native_event_to_matrix_message(
|
|||||||
#[tracing::instrument(name = "determine_interaction_context_for_room_event", skip_all, fields(room_id = room.room_id().as_str(), event_id = current_event.event_id.as_str()))]
|
#[tracing::instrument(name = "determine_interaction_context_for_room_event", skip_all, fields(room_id = room.room_id().as_str(), event_id = current_event.event_id.as_str()))]
|
||||||
pub async fn determine_interaction_context_for_room_event(
|
pub async fn determine_interaction_context_for_room_event(
|
||||||
bot_user_id: &OwnedUserId,
|
bot_user_id: &OwnedUserId,
|
||||||
|
bot_display_name: &Option<String>,
|
||||||
room: &Room,
|
room: &Room,
|
||||||
current_event: &OriginalSyncRoomMessageEvent,
|
current_event: &OriginalSyncRoomMessageEvent,
|
||||||
current_event_payload: &MessagePayload,
|
current_event_payload: &MessagePayload,
|
||||||
event_fetcher: &Arc<RoomEventFetcher>,
|
event_fetcher: &Arc<RoomEventFetcher>,
|
||||||
) -> anyhow::Result<Option<InteractionContext>> {
|
) -> anyhow::Result<Option<InteractionContext>> {
|
||||||
let current_event_is_mentioning_bot =
|
let current_event_is_mentioning_bot =
|
||||||
is_event_mentioning_bot(¤t_event.content, bot_user_id);
|
is_event_mentioning_bot(¤t_event.content, bot_user_id, bot_display_name);
|
||||||
|
|
||||||
let Some(relation) = ¤t_event.content.relates_to else {
|
let Some(relation) = ¤t_event.content.relates_to else {
|
||||||
// This is a top-level message. We consider it the start of the thread.
|
// This is a top-level message. We consider it the start of the thread.
|
||||||
@@ -304,6 +306,7 @@ pub async fn determine_interaction_context_for_room_event(
|
|||||||
Relation::Thread(thread) => {
|
Relation::Thread(thread) => {
|
||||||
determine_interaction_context_for_room_event_related_to_thread(
|
determine_interaction_context_for_room_event_related_to_thread(
|
||||||
bot_user_id,
|
bot_user_id,
|
||||||
|
bot_display_name,
|
||||||
room,
|
room,
|
||||||
current_event,
|
current_event,
|
||||||
event_fetcher,
|
event_fetcher,
|
||||||
@@ -328,6 +331,7 @@ pub async fn determine_interaction_context_for_room_event(
|
|||||||
|
|
||||||
async fn determine_interaction_context_for_room_event_related_to_thread(
|
async fn determine_interaction_context_for_room_event_related_to_thread(
|
||||||
bot_user_id: &OwnedUserId,
|
bot_user_id: &OwnedUserId,
|
||||||
|
bot_display_name: &Option<String>,
|
||||||
room: &Room,
|
room: &Room,
|
||||||
current_event: &OriginalSyncRoomMessageEvent,
|
current_event: &OriginalSyncRoomMessageEvent,
|
||||||
event_fetcher: &Arc<RoomEventFetcher>,
|
event_fetcher: &Arc<RoomEventFetcher>,
|
||||||
@@ -388,6 +392,7 @@ async fn determine_interaction_context_for_room_event_related_to_thread(
|
|||||||
thread_start_timeline_event,
|
thread_start_timeline_event,
|
||||||
thread_info.clone(),
|
thread_info.clone(),
|
||||||
bot_user_id,
|
bot_user_id,
|
||||||
|
bot_display_name,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let Some(detailed_message_payload) = thread_start_detailed_message_payload else {
|
let Some(detailed_message_payload) = thread_start_detailed_message_payload else {
|
||||||
@@ -430,6 +435,7 @@ async fn determine_interaction_context_for_room_event_related_to_reply(
|
|||||||
fn is_event_mentioning_bot(
|
fn is_event_mentioning_bot(
|
||||||
event_content: &RoomMessageEventContent,
|
event_content: &RoomMessageEventContent,
|
||||||
bot_user_id: &OwnedUserId,
|
bot_user_id: &OwnedUserId,
|
||||||
|
bot_display_name: &Option<String>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
if let Some(mentions) = &event_content.mentions {
|
if let Some(mentions) = &event_content.mentions {
|
||||||
mentions
|
mentions
|
||||||
@@ -444,39 +450,18 @@ fn is_event_mentioning_bot(
|
|||||||
// As of 2024-10-03, at least Element iOS does not support the new Mentions specification
|
// As of 2024-10-03, at least Element iOS does not support the new Mentions specification
|
||||||
// and is still quite widespread.
|
// and is still quite widespread.
|
||||||
//
|
//
|
||||||
// It may be even better to match not only against the MXID, but also against the bot's
|
|
||||||
// room-specific display name.
|
|
||||||
//
|
|
||||||
// We may consider dropping this string-matching behavior altogether in the future,
|
// We may consider dropping this string-matching behavior altogether in the future,
|
||||||
// so improving this compatibility block is not a high priority.
|
// so improving this compatibility block is not a high priority.
|
||||||
event_content.body().contains(bot_user_id.as_str())
|
if event_content.body().contains(bot_user_id.as_str()) {
|
||||||
}
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
/// Strips the rich reply fallback text from the given text.
|
|
||||||
/// See: https://spec.matrix.org/v1.11/client-server-api/#fallbacks-for-rich-replies
|
|
||||||
///
|
|
||||||
/// Example:
|
|
||||||
/// ```rust,ignore
|
|
||||||
/// let text = "> <@admin:example.com> What's the difference between Matrix and XMPP?\n\nAnswer me";
|
|
||||||
/// let stripped_text = strip_rich_reply_fallback_text(text);
|
|
||||||
/// assert_eq!(stripped_text, "Answer me");
|
|
||||||
/// ```
|
|
||||||
fn strip_rich_reply_fallback_text(text: &str) -> String {
|
|
||||||
let lines = text.lines();
|
|
||||||
let mut stripped_lines = Vec::new();
|
|
||||||
let mut encountered_non_prefix = false;
|
|
||||||
|
|
||||||
for line in lines {
|
|
||||||
if !encountered_non_prefix && line.starts_with("> ") {
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
encountered_non_prefix = true;
|
|
||||||
stripped_lines.push(line);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
stripped_lines.join("\n").trim().to_owned()
|
if let Some(bot_display_name) = bot_display_name {
|
||||||
|
return event_content.body().contains(bot_display_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn timeline_event_to_detailed_message_payload(
|
fn timeline_event_to_detailed_message_payload(
|
||||||
@@ -484,6 +469,7 @@ fn timeline_event_to_detailed_message_payload(
|
|||||||
timeline_event: TimelineEvent,
|
timeline_event: TimelineEvent,
|
||||||
thread_info: ThreadInfo,
|
thread_info: ThreadInfo,
|
||||||
bot_user_id: &OwnedUserId,
|
bot_user_id: &OwnedUserId,
|
||||||
|
bot_display_name: &Option<String>,
|
||||||
) -> anyhow::Result<Option<DetailedMessagePayload>> {
|
) -> anyhow::Result<Option<DetailedMessagePayload>> {
|
||||||
let timeline_event_deserialized = match timeline_event.event.deserialize() {
|
let timeline_event_deserialized = match timeline_event.event.deserialize() {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
@@ -535,8 +521,11 @@ fn timeline_event_to_detailed_message_payload(
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_mentioning_bot =
|
let is_mentioning_bot = is_event_mentioning_bot(
|
||||||
is_event_mentioning_bot(&room_message_original.content, bot_user_id);
|
&room_message_original.content,
|
||||||
|
bot_user_id,
|
||||||
|
bot_display_name,
|
||||||
|
);
|
||||||
|
|
||||||
(is_mentioning_bot, room_message_payload)
|
(is_mentioning_bot, room_message_payload)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -296,13 +296,6 @@ async fn process_matrix_messages() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn strip_rich_reply_fallback_text() {
|
|
||||||
let text = "> <@admin:example.com> What's the difference between Matrix and XMPP?\n\nAnswer me";
|
|
||||||
let stripped_text = super::strip_rich_reply_fallback_text(text);
|
|
||||||
assert_eq!(stripped_text, "Answer me");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_list_of_bot_user_prefixes_to_strip() {
|
fn create_list_of_bot_user_prefixes_to_strip() {
|
||||||
let bot_user_id =
|
let bot_user_id =
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ pub struct MessageContext {
|
|||||||
admin_whitelist_regexes: Vec<regex::Regex>,
|
admin_whitelist_regexes: Vec<regex::Regex>,
|
||||||
trigger_event_info: TriggerEventInfo,
|
trigger_event_info: TriggerEventInfo,
|
||||||
thread_info: ThreadInfo,
|
thread_info: ThreadInfo,
|
||||||
|
|
||||||
|
bot_display_name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MessageContext {
|
impl MessageContext {
|
||||||
@@ -31,9 +33,20 @@ impl MessageContext {
|
|||||||
admin_whitelist_regexes,
|
admin_whitelist_regexes,
|
||||||
trigger_event_info,
|
trigger_event_info,
|
||||||
thread_info,
|
thread_info,
|
||||||
|
|
||||||
|
bot_display_name: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_bot_display_name(mut self, value: Option<String>) -> Self {
|
||||||
|
self.bot_display_name = value;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bot_display_name(&self) -> &Option<String> {
|
||||||
|
&self.bot_display_name
|
||||||
|
}
|
||||||
|
|
||||||
pub fn room(&self) -> &Room {
|
pub fn room(&self) -> &Room {
|
||||||
&self.room
|
&self.room
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user