Giter Site home page Giter Site logo

geminitokencounterapi's Introduction

Token Counter For Text Based Gemini Models

A very simple implementation of token counter for gemini models.

You can always use the token counter directly in the python code when making request to gemini api to give you live token count of your prompt and responses with a simple python line :

num_tokens = gemini_model.count_tokens(text).total_tokens

This was made to give a api endpoint where requests can be made to get token count in prompts.

Currently tracking following models. Support for vision, image models coming soon

gemini_models = [
    "gemini-1.0-pro",
    "gemini-1.0-pro-001",
    "gemini-1.0-pro-latest",
    "gemini-1.5-pro-latest",
    "gemini-pro",
]

Prerequisite

  • Get your API_KEY from google ai studio,

  • rename .env_local to .env

  • replace API_KEY with yours in .env

Counter is exposed through post request on /tokenize endpoint. We need to send json payload to the endpoint e.g http://127.0.0.1:5000/tokenize as:

token counter api request

{ "text": "count tokens in this prompt", "model": "gemini-1.0-pro" }
# And we are extracting details from request as following
data = request.json
text = data["text"]
model = data["model"]

If the model matches to the ones are tracking the json response will be following format :

{
  "data": 27,
  "error": false,
  "message": "Token count successful"
}

Unsuccessful response:

{
    "error": True,
    "message": "Error counting tokens",
    "data": -1
}

CORS comes into question as we deploy our api endpoint to production. This is tracked using the environment key in the .env file.

# Make sure to change the origins to your web app url post requests are coming from
if environment != "dev":
    CORS(
        app,
        resources={
            r"/*": {
                "origins": [
                    "https://geminitokencounter.dbanswan.com",
                ],
                "methods": ["GET", "POST"],
            }
        },
    )

When set to "dev" CORS will be off, change it to "prod" on deployment (I use vercel so in the project settings can add env variables).

geminitokencounterapi's People

Contributors

dbanswan avatar

Watchers

 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.