Skip to content

fix: replace max_length with max_new_tokens and add basic chat context budgeting - #131

Open
Shivang731 wants to merge 1 commit into
sugarlabs:mainfrom
Shivang731:main
Open

fix: replace max_length with max_new_tokens and add basic chat context budgeting#131
Shivang731 wants to merge 1 commit into
sugarlabs:mainfrom
Shivang731:main

Conversation

@Shivang731

Copy link
Copy Markdown

Partially addresses #120

Summary

This PR fixes two concrete problems in the current Hugging Face generation path:

  1. prompted/chat generation used max_length, which limits total tokens (input + output) and could truncate the prompt itself before generation
  2. chat completions accepted full message history with no application-level trimming, making long conversations overflow context unpredictably

What changed

app/context.py

Adds a small context-management helper for the current backend path:

  • ModelMetadata stores:
    • model_name
    • context_window
    • max_output_tokens
    • derived safe_input_budget
  • ContextManager.trim_chat_history(...) drops older non-system messages to fit the configured input budget
  • ContextManager.fits_in_budget(...) provides a simple overflow check for single prompts

app/ai.py

  • initializes self.ctx in RAGAgent
  • updates run_chat_completion(...) to trim chat history before rendering the chat prompt
  • updates run_with_custom_prompt(...) to warn when the prompt exceeds the configured input budget
  • replaces max_length with max_new_tokens in the explicit prompted/chat generation calls

app/config.py

Adds configurable settings for:

  • CONTEXT_WINDOW
  • MAX_OUTPUT_TOKENS

app/routes/api.py

  • removes max_length from PromptedLLMRequest
  • adds GET /model-info to expose:
    • model_name
    • context_window
    • max_output_tokens
    • safe_input_budget

Scope

This PR adds basic application-level safeguards for the current inference path.

It does not yet implement:

  • conversation summarization/compression
  • retrieval-context budgeting
  • provider-agnostic runtime abstraction
  • model-derived context-window discovery across backends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant