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