Skip to content

Commit

Permalink
fix: input context length logic (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyashankar authored Jan 26, 2025
1 parent 8b33bc9 commit 66a10c5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions docetl/operations/utils/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,18 @@ def truncate_messages(
messages: List[Dict[str, str]], model: str, from_agent: bool = False
) -> List[Dict[str, str]]:
"""Truncate messages to fit within model's context length."""
model_input_context_length = model_cost.get(model.split("/")[-1], {}).get(
"max_input_tokens", 8192
)
model_cost_info = model_cost.get(model, {})
if not model_cost_info:
# Try stripping the first part before the /
split_model = model.split("/")
if len(split_model) > 1:
model_cost_info = model_cost.get("/".join(split_model[1:]), {})

if not model_cost_info:
model_cost_info = model_cost.get(model.split("/")[-1], {})

model_input_context_length = model_cost_info.get("max_input_tokens", 8192)

total_tokens = sum(count_tokens(json.dumps(msg), model) for msg in messages)

if total_tokens <= model_input_context_length - 100:
Expand Down

0 comments on commit 66a10c5

Please sign in to comment.