Skip to content

Commit

Permalink
Fix the message history from becoming too large due to images
Browse files Browse the repository at this point in the history
- Bump to 0.22.1
  • Loading branch information
smathot committed Jul 22, 2024
1 parent d0126ab commit 4db0222
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sigmund/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""AI-based chatbot that provides sensible answers based on documentation"""

__version__ = '0.22.0'
__version__ = '0.22.1'
3 changes: 3 additions & 0 deletions sigmund/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
# The number of previous messages for which tool results should be
# retained.
keep_tool_results = 4
# Tool results larger than this are not included in the prompt used for
# generating
large_tool_result_length = 1024

# RATE LIMITS
#
Expand Down
6 changes: 4 additions & 2 deletions sigmund/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def delete(self, message_id):
if self._persistent:
self.save()

def prompt(self, system_prompt=None):
def prompt(self, system_prompt=None, skip_large_tool_results=False):
"""The prompt consists of the system prompt followed by a sequence of
AI, user, and tool/ function messages.
Expand All @@ -104,6 +104,8 @@ def prompt(self, system_prompt=None):
elif role == 'tool':
if msg_nr + config.keep_tool_results < msg_len:
continue
if len(content) > config.large_tool_result_length:
continue
model_prompt.append(FunctionMessage(content=content,
name='tool_function'))
else:
Expand Down Expand Up @@ -232,4 +234,4 @@ def save(self):
'condensed_message_history': self._condensed_message_history,
'title': self._conversation_title,
}
self._sigmund.database.update_active_conversation(conversation)
self._sigmund.database.update_active_conversation(conversation)
6 changes: 4 additions & 2 deletions sigmund/sigmund.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def _search(self, message: str) -> GeneratorType:
# Then search based on the search-model queries derived from the user
# question
reply = self.search_model.predict(self.messages.prompt(
system_prompt=prompt.SYSTEM_PROMPT_SEARCH))
system_prompt=prompt.SYSTEM_PROMPT_SEARCH,
skip_large_tool_results=True))
if config.log_replies:
logger.info(f'[search state] reply: {reply}')
if callable(reply):
Expand All @@ -140,7 +141,8 @@ def _answer(self, state: str = 'answer') -> GeneratorType:
# We first collect a regular reply to the user message. While doing so
# we also keep track of the number of tokens consumed.
tokens_consumed_before = self.answer_model.total_tokens_consumed
reply = self.answer_model.predict(self.messages.prompt())
reply = self.answer_model.predict(self.messages.prompt(
skip_large_tool_results=True))
tokens_consumed = self.answer_model.total_tokens_consumed \
- tokens_consumed_before
logger.info(f'tokens consumed: {tokens_consumed}')
Expand Down
2 changes: 1 addition & 1 deletion sigmund/tools/_generate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class generate_image(BaseTool):
},
'quality': {
'type': 'string',
'description': 'The quality of the image',
'description': 'The quality of the image. Use standard unless there is a good reason to use HD.',
'enum': ['standard', 'hd']
},
'size': {
Expand Down

0 comments on commit 4db0222

Please sign in to comment.