[GH-ISSUE #7991] Context length parameter doesn't work #69557

Closed
opened 2026-05-13 01:49:24 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @nengoxx on GitHub (Dec 21, 2024).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/7991

Bug Report

Installation Method

pip

Environment

  • Open WebUI Version: 0.4.7

  • Operating System: Windows 11

  • Browser (if applicable): Firefox

Confirmation:

  • I have read and followed all the instructions provided in the README.md.
  • I am on the latest version of both Open WebUI and Ollama.
  • I have included the browser console logs.
  • I have included the Docker container logs.
  • I have provided the exact steps to reproduce the bug in the "Steps to Reproduce" section below.

Expected Behavior:

Context shouldn't be longer than the number set in any of the options.

Actual Behavior:

It doesn't seem to trim the context regardless of where you set it (general settings, per model or per chat).

Description

Bug Summary:
Setting up a number doesn't trim the context, still sends the whole chat to the API.

Reproduction Details

Steps to Reproduce:
Set up a context length in Admin Console>Models, also in the Personal settings and in the chat settings.

Logs and Screenshots

Screenshots/Screen Recordings (if applicable):
Screenshot 2024-12-21 173739
Screenshot 2024-12-21 173904
Screenshot 2024-12-21 173944

Additional Information

This makes APIs like groq's unusable for longer chats.

Originally created by @nengoxx on GitHub (Dec 21, 2024). Original GitHub issue: https://github.com/open-webui/open-webui/issues/7991 # Bug Report ## Installation Method pip ## Environment - **Open WebUI Version:** 0.4.7 - **Operating System:** Windows 11 - **Browser (if applicable):** Firefox **Confirmation:** - [x] I have read and followed all the instructions provided in the README.md. - [ ] I am on the latest version of both Open WebUI and Ollama. - [ ] I have included the browser console logs. - [ ] I have included the Docker container logs. - [x] I have provided the exact steps to reproduce the bug in the "Steps to Reproduce" section below. ## Expected Behavior: Context shouldn't be longer than the number set in any of the options. ## Actual Behavior: It doesn't seem to trim the context regardless of where you set it (general settings, per model or per chat). ## Description **Bug Summary:** Setting up a number doesn't trim the context, still sends the whole chat to the API. ## Reproduction Details **Steps to Reproduce:** Set up a context length in Admin Console>Models, also in the Personal settings and in the chat settings. ## Logs and Screenshots **Screenshots/Screen Recordings (if applicable):** ![Screenshot 2024-12-21 173739](https://github.com/user-attachments/assets/ec1a4382-d46e-4a7d-9e8f-fa6243731daa) ![Screenshot 2024-12-21 173904](https://github.com/user-attachments/assets/70acbdae-1e73-4f04-bffe-7631c8b78f55) ![Screenshot 2024-12-21 173944](https://github.com/user-attachments/assets/28c1a70d-72bc-4621-ab0a-dbe36cb77e42) ## Additional Information This makes APIs like groq's unusable for longer chats.
Author
Owner

@tjbck commented on GitHub (Dec 21, 2024):

Not what context length param is for.

<!-- gh-comment-id:2558171732 --> @tjbck commented on GitHub (Dec 21, 2024): Not what context length param is for.
Author
Owner

@nengoxx commented on GitHub (Dec 21, 2024):

Is it just for local inference requests with ollama perhaps?

Edit: I didn't have time to check the code this afternoon, but I guessed that was it, did a quick and dirty fix to hardcode it (in the main.py), not sure how usable this is but just in case you wanna check it, and also to have some future reference for myself.

import tiktoken

def count_tokens(messages, model):
    encoding = tiktoken.get_encoding("gpt2")
    #encoding = tiktoken.encoding_for_model(model) #extract external model name??
    total_tokens = 0

    for message in messages:
        total_tokens += len(encoding.encode(message['role']))
        total_tokens += len(encoding.encode(message['content']))

    return total_tokens

def trim_context(messages, model, max_tokens):

    #Leave the system role.
    preserved_messages = [msg for msg in messages if msg['role'] not in ['user', 'assistant']]
    trimmable_messages = [msg for msg in messages if msg['role'] in ['user', 'assistant']]

    while count_tokens(preserved_messages + trimmable_messages, model) > max_tokens:
        trimmable_messages.pop(0)

    return preserved_messages + trimmable_messages

Then inside the /chat/completions route:

    max_tokens = api_config.get("max_context_tokens", 8192)  #Hardcoded max tokens.
    if "messages" in payload:
        payload["messages"] = trim_context(payload["messages"], payload["model"], max_tokens)
<!-- gh-comment-id:2558181757 --> @nengoxx commented on GitHub (Dec 21, 2024): Is it just for local inference requests with ollama perhaps? Edit: I didn't have time to check the code this afternoon, but I guessed that was it, did a quick and dirty fix to hardcode it (in the main.py), not sure how usable this is but just in case you wanna check it, and also to have some future reference for myself. ``` import tiktoken def count_tokens(messages, model): encoding = tiktoken.get_encoding("gpt2") #encoding = tiktoken.encoding_for_model(model) #extract external model name?? total_tokens = 0 for message in messages: total_tokens += len(encoding.encode(message['role'])) total_tokens += len(encoding.encode(message['content'])) return total_tokens def trim_context(messages, model, max_tokens): #Leave the system role. preserved_messages = [msg for msg in messages if msg['role'] not in ['user', 'assistant']] trimmable_messages = [msg for msg in messages if msg['role'] in ['user', 'assistant']] while count_tokens(preserved_messages + trimmable_messages, model) > max_tokens: trimmable_messages.pop(0) return preserved_messages + trimmable_messages ``` Then inside the /chat/completions route: ``` max_tokens = api_config.get("max_context_tokens", 8192) #Hardcoded max tokens. if "messages" in payload: payload["messages"] = trim_context(payload["messages"], payload["model"], max_tokens) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#69557