Skip to content

Commit

Permalink
fix: generate summary error when tokens=4097 (langgenius#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
takatost authored Jun 29, 2023
1 parent 745f146 commit 7a897b5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion api/core/generator/llm_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def generate_conversation_summary(cls, tenant_id: str, messages):
prompt = CONVERSATION_SUMMARY_PROMPT
prompt_with_empty_context = prompt.format(context='')
prompt_tokens = TokenCalculator.get_num_tokens(model, prompt_with_empty_context)
rest_tokens = llm_constant.max_context_token_length[model] - prompt_tokens - max_tokens
rest_tokens = llm_constant.max_context_token_length[model] - prompt_tokens - max_tokens - 1

context = ''
for message in messages:
Expand All @@ -56,6 +56,9 @@ def generate_conversation_summary(cls, tenant_id: str, messages):
if rest_tokens - TokenCalculator.get_num_tokens(model, context + message_qa_text) > 0:
context += message_qa_text

if not context:
return '[message too long, no summary]'

prompt = prompt.format(context=context)

llm: StreamableOpenAI = LLMBuilder.to_llm(
Expand Down
2 changes: 1 addition & 1 deletion api/tasks/generate_conversation_summary_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def generate_conversation_summary_task(conversation_id: str):
try:
# get conversation messages count
history_message_count = conversation.message_count
if history_message_count >= 5:
if history_message_count >= 5 and not conversation.summary:
app_model = conversation.app
if not app_model:
return
Expand Down

0 comments on commit 7a897b5

Please sign in to comment.