Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the token usage for every conversation and workspace #788

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/update-model-costs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This workflow updates the file in /model_cost_data/model_prices_and_context_window.json
name: Update model prices and context window JSON file

on:
workflow_call:
schedule:
- cron: '0 2 * * 0' # Run every Sunday at 2:00 AM

jobs:
update_model_prices:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write

steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

- name: Get the latest file
run: |
curl -Ss 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json' > model_cost_data/model_prices_and_context_window.json

- name: Check if file changed
id: check-model-prices
run: |
if ! git diff --quiet model_cost_data/model_prices_and_context_window.json ; then
echo "changed=true" >> "$GITHUB_OUTPUT"
else
echo "changed=false" >> "$GITHUB_OUTPUT"
fi

- name: Set git config
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"

- name: Get current date
id: date
run: |
echo "CURRENT_DATETIME=$(date +%Y-%m-%d)" >> $GITHUB_ENV

- name: Generate PR if needed
if: steps.check-model-prices.outputs.changed == 'true'
run: |
git checkout -b update-model-prices-$GITHUB_SHA

git add model_cost_data/model_prices_and_context_window.json
git commit -m "Update model_prices_and_context_window.json to version generated on ${{ env.CURRENT_DATETIME }}"

echo "Pushing branch so we can create a PR..."
git push --set-upstream origin update-model-prices-$GITHUB_SHA

gh pr create --title "Update model_prices_and_context_window.json" \
--body "This PR updates the model_prices_and_context_window.json definition to the version generated on ${{ env.CURRENT_DATETIME }}" \
--repo "$GITHUB_REPOSITORY" \
--base main \
--head update-model-prices-$GITHUB_SHA
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""add token usage columns

Revision ID: 0c3539f66339
Revises: 0f9b8edc8e46
Create Date: 2025-01-28 09:15:54.767311+00:00

"""

from typing import Sequence, Union

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0c3539f66339"
down_revision: Union[str, None] = "0f9b8edc8e46"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# Begin transaction
op.execute("BEGIN TRANSACTION;")

# We add the columns to the outputs table
# Add the columns with default values to avoid issues with the existing data
# The prices of the tokens may change in the future,
# so we need to store the cost of the tokens at the time of the request
op.execute("ALTER TABLE outputs ADD COLUMN input_tokens INT DEFAULT NULL;")
op.execute("ALTER TABLE outputs ADD COLUMN output_tokens INT DEFAULT NULL;")
op.execute("ALTER TABLE outputs ADD COLUMN input_cost FLOAT DEFAULT NULL;")
op.execute("ALTER TABLE outputs ADD COLUMN output_cost FLOAT DEFAULT NULL;")

# Finish transaction
op.execute("COMMIT;")


def downgrade() -> None:
# Begin transaction
op.execute("BEGIN TRANSACTION;")

op.execute("ALTER TABLE outputs DROP COLUMN input_tokens;")
op.execute("ALTER TABLE outputs DROP COLUMN output_tokens;")
op.execute("ALTER TABLE outputs DROP COLUMN input_cost;")
op.execute("ALTER TABLE outputs DROP COLUMN output_cost;")

# Finish transaction
op.execute("COMMIT;")
Loading