Skip to content

Commit 219237f

Browse files
authored
Consider system prompt in calculations (#1233)
* Consider system prompt in calculations * Explicit cast to dict * Add type ignore * Rename variable with different type
1 parent 0ee189d commit 219237f

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

app/backend/approaches/chatapproach.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ def get_messages_from_history(
107107
append_index = len(few_shots) + 1
108108

109109
message_builder.insert_message(self.USER, user_content, index=append_index)
110-
total_token_count = message_builder.count_tokens_for_message(dict(message_builder.messages[-1])) # type: ignore
110+
111+
total_token_count = 0
112+
for existing_message in message_builder.messages:
113+
total_token_count += message_builder.count_tokens_for_message(dict(existing_message)) # type: ignore
111114

112115
newest_to_oldest = list(reversed(history[:-1]))
113116
for message in newest_to_oldest:
114117
potential_message_count = message_builder.count_tokens_for_message(message)
115118
if (total_token_count + potential_message_count) > max_tokens:
116-
logging.debug("Reached max tokens of %d, history will be truncated", max_tokens)
119+
logging.info("Reached max tokens of %d, history will be truncated", max_tokens)
117120
break
118121
message_builder.insert_message(message["role"], message["content"], index=append_index)
119122
total_token_count += potential_message_count

tests/test_chatapproach.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ def test_get_messages_from_history_truncated_break_pair(chat_approach):
186186
{"role": "user", "content": "What happens in a performance review?"}, # 10 tokens
187187
{
188188
"role": "assistant",
189-
"content": "During the performance review at Contoso Electronics, the supervisor will discuss the employee's performance over the past year and provide feedback on areas for improvement. They will also provide an opportunity for the employee to discuss their goals and objectives for the upcoming year. The review is a two-way dialogue between managers and employees, and employees will receive a written summary of their performance review which will include a rating of their performance, feedback, and goals and objectives for the upcoming year [employee_handbook-3.pdf].",
190-
}, # 102 tokens
189+
"content": "The supervisor will discuss the employee's performance and provide feedback on areas for improvement. They will also provide an opportunity for the employee to discuss their goals and objectives for the upcoming year. The review is a two-way dialogue between managers and employees, and employees will receive a written summary of their performance review which will include a rating of their performance, feedback, and goals for the upcoming year [employee_handbook-3.pdf].",
190+
}, # 87 tokens
191191
{"role": "user", "content": "Is there a dress code?"}, # 9 tokens
192192
{
193193
"role": "assistant",
@@ -202,7 +202,7 @@ def test_get_messages_from_history_truncated_break_pair(chat_approach):
202202
{"role": "system", "content": "You are a bot."},
203203
{
204204
"role": "assistant",
205-
"content": "During the performance review at Contoso Electronics, the supervisor will discuss the employee's performance over the past year and provide feedback on areas for improvement. They will also provide an opportunity for the employee to discuss their goals and objectives for the upcoming year. The review is a two-way dialogue between managers and employees, and employees will receive a written summary of their performance review which will include a rating of their performance, feedback, and goals and objectives for the upcoming year [employee_handbook-3.pdf].",
205+
"content": "The supervisor will discuss the employee's performance and provide feedback on areas for improvement. They will also provide an opportunity for the employee to discuss their goals and objectives for the upcoming year. The review is a two-way dialogue between managers and employees, and employees will receive a written summary of their performance review which will include a rating of their performance, feedback, and goals for the upcoming year [employee_handbook-3.pdf].",
206206
},
207207
{"role": "user", "content": "Is there a dress code?"},
208208
{
@@ -213,6 +213,36 @@ def test_get_messages_from_history_truncated_break_pair(chat_approach):
213213
]
214214

215215

216+
def test_get_messages_from_history_system_message(chat_approach):
217+
"""Tests that the system message token count is considered."""
218+
messages = chat_approach.get_messages_from_history(
219+
system_prompt="Assistant helps the company employees with their healthcare plan questions, and questions about the employee handbook. Be brief in your answers.", # 24 tokens
220+
model_id="gpt-35-turbo",
221+
history=[
222+
{"role": "user", "content": "What happens in a performance review?"}, # 10 tokens
223+
{
224+
"role": "assistant",
225+
"content": "During the performance review at Contoso Electronics, the supervisor will discuss the employee's performance over the past year and provide feedback on areas for improvement. They will also provide an opportunity for the employee to discuss their goals and objectives for the upcoming year. The review is a two-way dialogue between managers and employees, and employees will receive a written summary of their performance review which will include a rating of their performance, feedback, and goals and objectives for the upcoming year [employee_handbook-3.pdf].",
226+
}, # 102 tokens
227+
{"role": "user", "content": "Is there a dress code?"}, # 9 tokens
228+
{
229+
"role": "assistant",
230+
"content": "Yes, there is a dress code at Contoso Electronics. Look sharp! [employee_handbook-1.pdf]",
231+
}, # 26 tokens
232+
{"role": "user", "content": "What does a Product Manager do?"}, # 10 tokens
233+
],
234+
user_content="What does a Product Manager do?",
235+
max_tokens=36,
236+
)
237+
assert messages == [
238+
{
239+
"role": "system",
240+
"content": "Assistant helps the company employees with their healthcare plan questions, and questions about the employee handbook. Be brief in your answers.",
241+
},
242+
{"role": "user", "content": "What does a Product Manager do?"},
243+
]
244+
245+
216246
def test_extract_followup_questions(chat_approach):
217247
content = "Here is answer to your question.<<What is the dress code?>>"
218248
pre_content, followup_questions = chat_approach.extract_followup_questions(content)

0 commit comments

Comments
 (0)