diff --git a/.env.example b/.env.example index d39bda6..f511424 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,5 @@ SHOW_USAGE=True TEMPERATURE=0 TOP_P=1 MAX_TOKENS=1000 +MAX_CONTEXT_TOKENS=8192 MEMORY_KEEP_COUNT=10 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..edb16a0 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,35 @@ +name: Tests + +on: + push: + pull_request: + +permissions: + contents: read + +jobs: + test: + name: Python ${{ matrix.python-version }} + runs-on: ubuntu-latest + timeout-minutes: 10 + + strategy: + fail-fast: false + matrix: + python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] + + steps: + - name: Check out repository + uses: actions/checkout@v7 + + - name: Set up Python + uses: actions/setup-python@v7 + with: + python-version: ${{ matrix.python-version }} + cache: pip + + - name: Install dependencies + run: python -m pip install -r requirements.txt + + - name: Run tests + run: python -m unittest discover -v diff --git a/main.py b/main.py index d4d3d0e..66541e8 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ from openai import OpenAI, OpenAIError -from utils import get_required_config, read_input +from utils import calculate_input_token_budget, get_required_config, read_input def trim_history(messages, keep_count): @@ -26,6 +26,11 @@ def main(): temperature = get_required_config("TEMPERATURE", cast=float) top_p = get_required_config("TOP_P", cast=float) memory_keep_count = get_required_config("MEMORY_KEEP_COUNT", cast=int) + max_context_tokens = get_required_config( + "MAX_CONTEXT_TOKENS", + cast=int, + ) + calculate_input_token_budget(max_context_tokens, max_tokens) if memory_keep_count < 0: raise ValueError("MEMORY_KEEP_COUNT must be 0 or greater") diff --git a/utils.py b/utils.py index 92905ed..cf95b0f 100644 --- a/utils.py +++ b/utils.py @@ -23,4 +23,18 @@ def read_input(prompt): return None -__all__ = ["get_required_config", "read_input"] +def calculate_input_token_budget(context_limit, output_limit): + """Calculate how many tokens are available for input messages.""" + if context_limit <= 0: + raise ValueError("MAX_CONTEXT_TOKENS must be greater than 0") + + if output_limit <= 0: + raise ValueError("MAX_TOKENS must be greater than 0") + + if output_limit >= context_limit: + raise ValueError("MAX_TOKENS must be smaller than MAX_CONTEXT_TOKENS") + + return context_limit - output_limit + + +__all__ = ["calculate_input_token_budget", "get_required_config", "read_input"]