Skip to content

Commit dc2a7fb

Browse files
committed
Refactor model definitions and message handling: update Person model to use optional fields, enhance asynchronous context handling in database operations, and streamline prompt generation in MessageHandler for improved clarity and efficiency.
1 parent 2647660 commit dc2a7fb

9 files changed

Lines changed: 27 additions & 18 deletions

File tree

bot/models/handlers_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class Person(BaseModel):
99
user_handle: str
10-
first_name: str
11-
last_name: str
10+
first_name: Optional[str] = None
11+
last_name: Optional[str] = None
1212

1313

1414
class Context(BaseModel):

bot/rp_bot/ai_agent/agent_tools/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def get_streaming_reply(
3838
) -> AsyncIterator[AIAgentStreamingResponse]:
3939
total_text = ""
4040
async for response in self.models_toolkit.text_model.astream_default(
41-
user_input, system_prompt
41+
user_input=user_input, system_prompt=system_prompt
4242
):
4343
total_text += response.text_chunk
4444
if response is not None:

bot/rp_bot/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def __init__(
4848

4949
async def create_if_not_exists(self, person: Person, context: Context) -> None:
5050
for model in self.models:
51-
await model.create_if_not_exists(person, context)
51+
await model.create_if_not_exists(context=context, person=person)
5252

5353
async def update_if_needed(self, person: Person, context: Context) -> None:
5454
for model in self.models:
55-
await model.update_if_needed(person, context)
55+
await model.update_if_needed(person=person, context=context)

bot/rp_bot/db_models/chat_modes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ def __init__(
2525
self.chat_modes = db.chat_modes
2626
self.default_chat_modes = default_chat_modes
2727

28-
async def create_chat_modes_if_not_exist(
29-
self, person: Person, context: Context
30-
) -> None:
28+
async def create_if_not_exists(self, context: Context, person: Person) -> None:
3129
for _, mode in self.default_chat_modes.default_chat_modes.items():
3230
self.chat_modes.update_one(
3331
{"chat_id": context.chat_id, "mode_name": mode.name},
@@ -53,7 +51,9 @@ async def get_chat_mode(self, context: Context) -> ChatModeResponse:
5351
# find the first mode by default
5452
chat_data = await self.chat_modes.find_one({"chat_id": chat_id})
5553
return ChatModeResponse(
56-
chat_data["_id"], chat_data["mode_name"], chat_data["mode_description"]
54+
id=chat_data["_id"],
55+
mode_name=chat_data["mode_name"],
56+
mode_description=chat_data["mode_description"],
5757
)
5858

5959
async def get_mode_name_by_id(self, context: Context, mode_id: str) -> str:

bot/rp_bot/db_models/user_facts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def get_user_facts(self, context: Context, person: Person) -> List[str]:
2626
facts = await self.user_facts.find_one(
2727
{"chat_id": context.chat_id, "user_handle": person.user_handle}
2828
)
29-
return facts.get("facts", [])
29+
return facts.get("facts", []) if facts else []
3030

3131
async def get_facts_for_user_handle(
3232
self, context: Context, user_handle: str
@@ -43,7 +43,7 @@ async def get_facts_for_user_handle(
4343
facts = await self.user_facts.find_one(
4444
{"chat_id": context.chat_id, "user_handle": user_handle}
4545
)
46-
return facts.get("facts", [])
46+
return facts.get("facts", []) if facts else []
4747

4848
async def add_fact(self, context: Context, person: Person, fact: str) -> None:
4949
await self.user_facts.update_one(

bot/rp_bot/db_models/user_introductions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ async def get_user_introduction(self, context: Context, person: Person) -> str:
2222
introduction = await self.user_introductions.find_one(
2323
{"chat_id": context.chat_id, "user_handle": person.user_handle}
2424
)
25-
return introduction.get("introduction", "")
25+
return introduction.get("introduction", "") if introduction else ""

bot/rp_bot/localizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def get_command_response(
2222
if context is None:
2323
language = self.default_language
2424
else:
25-
language = self.db.chats.get_language(context)
25+
language = await self.db.chats.get_language(context)
2626
if text not in self.translations.translations:
2727
return None
2828
localizer_translation = self.translations.translations[text]

bot/rp_bot/messages/message_handler.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ async def get_usage_over_limit_response(self, person: Person) -> CommandResponse
7777
async def get_prompt_from_transcribed_message(
7878
self, person: Person, context: Context, transcribed_message: TranscribedMessage
7979
) -> str:
80-
prompt = await self.prompt_manager.compose_prompt(transcribed_message, context)
80+
prompt = await self.prompt_manager.compose_prompt(
81+
initiator=person,
82+
context=context,
83+
user_transcribed_message=transcribed_message,
84+
)
8185
self.logger.info(
8286
"Using AI to generate a response to the message from "
8387
f"{person.user_handle} in chat {context.chat_id}"
@@ -155,15 +159,20 @@ async def stream_get_response(
155159
"as the agent detected a question or a request for information"
156160
)
157161

158-
if not self.is_usage_under_limit(person, context, message):
162+
if not await self.is_usage_under_limit(person, context, message):
159163
yield await self.get_usage_over_limit_response(person)
160164
return
161165

162166
prompt = await self.get_prompt_from_transcribed_message(
163-
person, context, message
167+
person,
168+
context,
169+
TranscribedMessage(
170+
message_text=message.message_text,
171+
timestamp=message.timestamp,
172+
),
164173
)
165174
response_message = ""
166-
system_prompt = self.prompt_manager.get_reply_system_prompt(context)
175+
system_prompt = await self.prompt_manager.get_reply_system_prompt(context)
167176
ai_agent = AIAgent(
168177
person,
169178
context,

bot/rp_bot/prompt_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async def compose_prompt(
114114
async def get_reply_system_prompt(self, context: Context) -> str:
115115
chat_name = context.chat_name
116116
chat_mode_prompt = await self._compose_chat_mode_prompt(context)
117-
chat_language = self.db.chats.get_language(context)
117+
chat_language = await self.db.chats.get_language(context)
118118
return (
119119
"You are a helpful assistant. "
120120
f"You are currently in the chat: {chat_name}. "

0 commit comments

Comments
 (0)