You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
742 B
Python

from __future__ import annotations
from aiohttp import web
import tiktoken_async
class TikTokenService:
instance = None
@staticmethod
async def create() -> TikTokenService:
if TikTokenService.instance is None:
TikTokenService.instance = TikTokenService()
await TikTokenService.instance.init()
return TikTokenService.instance
def __init__(self):
self.enc: tiktoken_async.Encoding = None
async def init(self):
self.enc = await tiktoken_async.encoding_for_model("gpt-3.5-turbo")
async def get_tokens(self, text: str):
encoded = self.enc.encode(text)
if encoded:
return len(encoded)
else:
return 0