Code Monkey home page Code Monkey logo

openai-token-counter's Introduction

OpenAI Token Counter

PyPI Status Python Version License

Tests Codecov

pre-commit Black

Token counter for OpenAI messages with support for function token calculation. This project was ported to python based on the following repository: https://github.com/hmarr/openai-chat-tokens

As stated in hmarr project:

Estimating token usage for chat completions isn't quite as easy as it sounds. For regular chat messages, you need to consider how the messages are formatted by OpenAI when they're provided to the model, as they don't simply dump the JSON messages they receive via the API into the model. For function calling, things are even more complex, as the OpenAPI-style function definitions get rewritten into TypeScript type definitions. This library handles both of those cases, as well as a minor adjustment needed for handling the results of function calling. tiktoken is used to do the tokenization.

This library is tested nightly againts the openai API to detect for potential breaks if any internal change is made by openai, because as stated above we implement token calculation based on internal OpenAI techniques that are not exposed and can potentially change without notice.

Installation

You can install OpenAI Token Counter via [pip] from [PyPI]:

$ pip install openai-token-counter

Usage

from openai_token_counter import openai_token_counter

messages = [{"role": "user", "content": "hello"}]
functions = [
    {
        "name": "bing_bong",
        "description": "Do a bing bong",
        "parameters": {
            "type": "object",
            "properties": {
                "foo": {"type": "string"},
                "bar": {"type": "number", "description": "A number"},
            }
        }
    }
]

result = openai_token_counter(
    messages=messages,
    model="gpt-3.5-turbo", # Optional, deafults to cl100k_base encoding which is used by GPT models
    functions=functions, # Optional
    function_call="auto" # Optional
)

print(result) # Output: '57'

Contributing

Contributions are very welcome.

  1. Install poetry
  2. Install the project dependencies
poetry install
  1. Make the changes
  2. Test locally using nox (no need to test all python versions, select only 3.10):
nox --python=3.10
  1. Create a PR in GitHub.

License

Distributed under the terms of the MIT, OpenAI Token Counter is free and open source software.

Issues

If you encounter any problems, please [file an issue] along with a detailed description.

openai-token-counter's People

Contributors

eitan1112 avatar m-abboud avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.