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

Improve token counting; account for functions #4919

Merged
merged 10 commits into from
Jul 9, 2023

Conversation

Pwuts
Copy link
Member

@Pwuts Pwuts commented Jul 8, 2023

Background

Part of #4799

When using the OpenAI Function Call API, the specified functions count towards token consumption. However, they are currently not accounted for in token calculation and compensation logic.

Changes

autogpt.llm.providers.openai:

  • Make get_openai_command_spec take a CommandRegistry and Config as arguments instead of an Agent
  • Add function count_openai_functions_tokens
  • Add function format_function_specs_as_typescript_ns

autogpt.llm.base:

  • Add "function" as a valid role in a message
  • Add ResponseMessageDict
  • In LLMResponse, disable unused attributes prompt_tokens_used, completion_tokens_used

autogpt.llm.utils:

  • Account for functions when calculating max_tokens
  • Use better response object type definitions
  • token_counter > count_message_tokens: allow single message arguments

Documentation

x

Test Plan

CI

PR Quality Checklist

  • My pull request is atomic and focuses on a single change.
  • I have thoroughly tested my changes with multiple different prompts.
  • I have considered potential risks and mitigations for my changes.
  • I have documented my changes clearly and comprehensively.
  • I have not snuck in any "extra" small tweaks changes.
  • I have run the following commands against my code to ensure it passes our linters:
    black .
    isort .
    mypy
    autoflake --remove-all-unused-imports --recursive --ignore-init-module-imports --ignore-pass-after-docstring autogpt tests --in-place

@netlify
Copy link

netlify bot commented Jul 8, 2023

Deploy Preview for auto-gpt-docs canceled.

Name Link
🔨 Latest commit 17ed047
🔍 Latest deploy log https://app.netlify.com/sites/auto-gpt-docs/deploys/64aaf9f8e7faff00081d4fd5

@github-actions github-actions bot added the size/l label Jul 8, 2023
@Pwuts Pwuts added this to the v0.4.5 Release milestone Jul 8, 2023
@Pwuts Pwuts force-pushed the count-function-tokens branch from 3b47cfa to 3d207d9 Compare July 8, 2023 20:20
@Pwuts Pwuts force-pushed the count-function-tokens branch from 3d207d9 to c9c68d5 Compare July 8, 2023 20:22
@Pwuts Pwuts changed the title Improve token counting; include function tokens Improve token counting; include tokens for OpenAI functions Jul 8, 2023
@Pwuts Pwuts changed the title Improve token counting; include tokens for OpenAI functions Improve token counting; account for OpenAI functions Jul 8, 2023
@Pwuts Pwuts added API costs Related to monitoring/reduction of running costs token limts Related to token limits labels Jul 8, 2023
@Pwuts Pwuts changed the title Improve token counting; account for OpenAI functions Improve token counting; account for functions Jul 8, 2023
@Auto-GPT-Bot
Copy link
Contributor

You changed AutoGPT's behaviour. The cassettes have been updated and will be merged to the submodule when this Pull Request gets merged.

@codecov
Copy link

codecov bot commented Jul 8, 2023

Codecov Report

Patch coverage: 64.28% and no project coverage change.

Comparison is base (f6da992) 50.55% compared to head (60f1a87) 50.56%.

❗ Current head 60f1a87 differs from pull request most recent head 7c140b0. Consider uploading reports for the commit 7c140b0 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4919   +/-   ##
=======================================
  Coverage   50.55%   50.56%           
=======================================
  Files         116      115    -1     
  Lines        4860     4818   -42     
  Branches      657      638   -19     
=======================================
- Hits         2457     2436   -21     
+ Misses       2219     2205   -14     
+ Partials      184      177    -7     
Impacted Files Coverage Δ
autogpt/llm/chat.py 61.44% <33.33%> (-4.79%) ⬇️
autogpt/llm/utils/token_counter.py 77.50% <55.55%> (-6.88%) ⬇️
autogpt/llm/providers/openai.py 88.79% <60.00%> (-2.04%) ⬇️
autogpt/llm/utils/__init__.py 48.00% <60.00%> (-0.58%) ⬇️
autogpt/agent/__init__.py 100.00% <100.00%> (ø)
autogpt/llm/base.py 93.33% <100.00%> (+2.42%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Auto-GPT-Bot
Copy link
Contributor

You changed AutoGPT's behaviour. The cassettes have been updated and will be merged to the submodule when this Pull Request gets merged.

collijk
collijk previously approved these changes Jul 9, 2023
Copy link
Contributor

@collijk collijk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice work on figuring out the function token counting. All comments here optional or for discussion.

@Auto-GPT-Bot
Copy link
Contributor

You changed AutoGPT's behaviour. The cassettes have been updated and will be merged to the submodule when this Pull Request gets merged.

@Pwuts Pwuts requested a review from collijk July 9, 2023 17:48
@Pwuts Pwuts force-pushed the count-function-tokens branch from 4cc221b to e081116 Compare July 9, 2023 17:49
collijk
collijk previously approved these changes Jul 9, 2023
@Auto-GPT-Bot
Copy link
Contributor

You changed AutoGPT's behaviour. The cassettes have been updated and will be merged to the submodule when this Pull Request gets merged.

@collijk collijk merged commit 51d8b43 into Significant-Gravitas:master Jul 9, 2023
@Pwuts Pwuts deleted the count-function-tokens branch July 9, 2023 18:32
Alaaelmziat pushed a commit to Alaaelmziat/Auto-GPT that referenced this pull request Jul 12, 2023
)

* Improvements to token counting, including functions
---------

Co-authored-by: James Collins <[email protected]>
dayofthedave pushed a commit to dayofthedave/Auto-GPT that referenced this pull request Jul 17, 2023
)

* Improvements to token counting, including functions
---------

Co-authored-by: James Collins <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API costs Related to monitoring/reduction of running costs behaviour change size/l token limts Related to token limits
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants