Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adding new API for Counting Number of Token from Input Text #13

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
__pycache__
/public/dist
.idea
.python-version
.python-version
build/
dist/
16 changes: 16 additions & 0 deletions openai/api_resources/tokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from openai.api_resources.abstract import (
ListableAPIResource,
DeletableAPIResource,
)
from openai import util


class CountTokens(ListableAPIResource, DeletableAPIResource):

@classmethod
def count_tokens(
cls, api_key=None, api_base=None, api_version=None, organization=None, **params
):
return util.check_tokens(
params
)
13 changes: 12 additions & 1 deletion openai/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def list(cls, args):
display(engines)


class Tokens:
@classmethod
def count(cls, args):
count = openai.Tokens.retrieve(id=args.text)
display(count)


class Completion:
@classmethod
def create(cls, args):
Expand Down Expand Up @@ -345,7 +352,7 @@ def help(args):
sub.add_argument("-q", "--query", required=True, help="Search query")
sub.set_defaults(func=Engine.search)

## Completions
# Completions
sub = subparsers.add_parser("completions.create")
sub.add_argument("-e", "--engine", required=True, help="The engine to use")
sub.add_argument(
Expand Down Expand Up @@ -460,3 +467,7 @@ def help(args):
sub = subparsers.add_parser("fine_tunes.cancel")
sub.add_argument("-i", "--id", required=True, help="The id of the fine-tune job")
sub.set_defaults(func=FineTuneCLI.cancel)

sub = subparsers.add_parser("tokens.count_tokens")
sub.add_argument("-t", "--text", required=True)
sub.set_defaults(func=Tokens.count_tokens)
5 changes: 5 additions & 0 deletions openai/common_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Constants:
"""Set common constants."""

# Constants
NUMBER_OF_CHARACTERS_PER_TOKEN = 4
8 changes: 7 additions & 1 deletion openai/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import re

import openai
from common_constants import Constants
from openai import six
from openai.six.moves.urllib.parse import parse_qsl


OPENAI_LOG = os.environ.get("OPENAI_LOG")
Expand Down Expand Up @@ -235,6 +235,12 @@ def merge_dicts(x, y):
return z


def check_tokens(data):
number_of_characters = len(data)
tokens = number_of_characters / Constants.NUMBER_OF_CHARACTERS_PER_TOKEN
return tokens


class class_method_variant(object):
def __init__(self, class_method_name):
self.class_method_name = class_method_name
Expand Down