Skip to content
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

Cog 970 refactor tokenizing #468

Merged
merged 23 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
93249c7
fix: Initial commit to resolve issue with using tokenizer based on LLMs
dexters1 Jan 21, 2025
294ed1d
feat: Add HuggingFace Tokenizer support
dexters1 Jan 23, 2025
b686376
feat: Add gemini tokenizer to cognee
dexters1 Jan 23, 2025
b25a82e
chore: Add google-generativeai as gemini optional dependency to Cognee
dexters1 Jan 23, 2025
1319944
docs: Update .env.template to include llm and embedding options
dexters1 Jan 23, 2025
6d5679f
Merge branch 'dev' into COG-970-refactor-tokenizing
dexters1 Jan 23, 2025
7dea1d5
refactor: Add specific max token values to embedding models
dexters1 Jan 23, 2025
844d99c
docs: Remove commented code
dexters1 Jan 23, 2025
902979c
refactor: Refactor get source code chunks based on tokenizer rework
dexters1 Jan 24, 2025
77a7285
Merge branch 'dev' into COG-970-refactor-tokenizing
dexters1 Jan 24, 2025
0a9f134
refactor: Change variable and function names based on PR comments
dexters1 Jan 28, 2025
49f6097
Merge branch 'dev' into COG-970-refactor-tokenizing
dexters1 Jan 28, 2025
3db7f85
feat: Add max_chunk_tokens value to chunkers
dexters1 Jan 28, 2025
b6e21ea
Merge branch 'dev' into COG-970-refactor-tokenizing
dexters1 Jan 28, 2025
4154436
test: Change test_by_paragraph tests to accomodate to change
dexters1 Jan 28, 2025
e0b7be7
Merge branch 'COG-970-refactor-tokenizing' of github.com:topoteretes/…
dexters1 Jan 28, 2025
dc0450d
test: Update document tests regrading max chunk tokens
dexters1 Jan 28, 2025
4e56cd6
refactor: Add max chunk tokens to code graph pipeline
dexters1 Jan 28, 2025
3e29c3d
docs: Update notebook to work with changes to max chunk tokens
dexters1 Jan 28, 2025
6f8cbdb
Merge branch 'dev' into COG-970-refactor-tokenizing
dexters1 Jan 28, 2025
710ca78
Merge branch 'dev' into COG-970-refactor-tokenizing
dexters1 Jan 28, 2025
a8644e0
feat: Use litellm max token size as default for model, if model exist…
dexters1 Jan 28, 2025
8602186
refactor: add suggestions from PR
dexters1 Jan 28, 2025
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
18 changes: 17 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
ENV="local"
TOKENIZERS_PARALLELISM="false"
LLM_API_KEY=

# LLM settings
LLM_API_KEY=""
LLM_MODEL="openai/gpt-4o-mini"
dexters1 marked this conversation as resolved.
Show resolved Hide resolved
LLM_PROVIDER="openai"
LLM_ENDPOINT=""
LLM_API_VERSION=""
LLM_MAX_TOKENS="16384"

GRAPHISTRY_USERNAME=
GRAPHISTRY_PASSWORD=

SENTRY_REPORTING_URL=

# Embedding settings
EMBEDDING_PROVIDER="openai"
EMBEDDING_API_KEY=""
EMBEDDING_MODEL="openai/text-embedding-3-large"
dexters1 marked this conversation as resolved.
Show resolved Hide resolved
EMBEDDING_ENDPOINT=""
EMBEDDING_API_VERSION=""
EMBEDDING_DIMENSIONS=3072
EMBEDDING_MAX_TOKENS=8191

# "neo4j" or "networkx"
GRAPH_DATABASE_PROVIDER="networkx"
# Not needed if using networkx
Expand Down
3 changes: 2 additions & 1 deletion cognee/api/v1/cognify/code_graph_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cognee.tasks.repo_processor.get_source_code_chunks import get_source_code_chunks
from cognee.tasks.storage import add_data_points
from cognee.tasks.summarization import summarize_code, summarize_text
from cognee.infrastructure.llm import get_max_chunk_tokens

monitoring = get_base_config().monitoring_tool
if monitoring == MonitoringTool.LANGFUSE:
Expand Down Expand Up @@ -57,7 +58,7 @@ async def run_code_graph_pipeline(repo_path, include_docs=True):
Task(ingest_data, dataset_name="repo_docs", user=user),
Task(get_data_list_for_user, dataset_name="repo_docs", user=user),
Task(classify_documents),
Task(extract_chunks_from_documents, max_tokens=cognee_config.max_tokens),
Task(extract_chunks_from_documents, max_chunk_tokens=get_max_chunk_tokens()),
Task(
extract_graph_from_data, graph_model=KnowledgeGraph, task_config={"batch_size": 50}
),
Expand Down
5 changes: 4 additions & 1 deletion cognee/api/v1/cognify/cognify_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pydantic import BaseModel

from cognee.infrastructure.llm import get_max_chunk_tokens
from cognee.modules.cognify.config import get_cognify_config
from cognee.modules.data.methods import get_datasets, get_datasets_by_name
from cognee.modules.data.methods.get_dataset_data import get_dataset_data
Expand Down Expand Up @@ -151,7 +152,9 @@ async def get_default_tasks(
default_tasks = [
Task(classify_documents),
Task(check_permissions_on_documents, user=user, permissions=["write"]),
Task(extract_chunks_from_documents), # Extract text chunks based on the document type.
Task(
extract_chunks_from_documents, max_chunk_tokens=get_max_chunk_tokens()
), # Extract text chunks based on the document type.
Task(
extract_graph_from_data, graph_model=graph_model, task_config={"batch_size": 10}
), # Generate knowledge graphs from the document chunks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import os
from cognee.infrastructure.databases.vector.embeddings.EmbeddingEngine import EmbeddingEngine
from cognee.infrastructure.databases.exceptions.EmbeddingException import EmbeddingException
from cognee.infrastructure.llm.tokenizer.Gemini import GeminiTokenizer
from cognee.infrastructure.llm.tokenizer.HuggingFace import HuggingFaceTokenizer
from cognee.infrastructure.llm.tokenizer.TikToken import TikTokenTokenizer

litellm.set_verbose = False
logger = logging.getLogger("LiteLLMEmbeddingEngine")
Expand All @@ -15,23 +18,29 @@ class LiteLLMEmbeddingEngine(EmbeddingEngine):
api_key: str
endpoint: str
api_version: str
provider: str
model: str
dimensions: int
mock: bool

def __init__(
self,
provider: str = "openai",
model: Optional[str] = "text-embedding-3-large",
dimensions: Optional[int] = 3072,
api_key: str = None,
endpoint: str = None,
api_version: str = None,
max_tokens: int = 512,
):
self.api_key = api_key
self.endpoint = endpoint
self.api_version = api_version
self.provider = provider
self.model = model
self.dimensions = dimensions
self.max_tokens = max_tokens
self.tokenizer = self.get_tokenizer()

enable_mocking = os.getenv("MOCK_EMBEDDING", "false")
if isinstance(enable_mocking, bool):
Expand Down Expand Up @@ -104,3 +113,18 @@ async def exponential_backoff(attempt):

def get_vector_size(self) -> int:
return self.dimensions

def get_tokenizer(self):
logger.debug(f"Loading tokenizer for model {self.model}...")
# If model also contains provider information, extract only model information
model = self.model.split("/")[-1]

if "openai" in self.provider.lower():
tokenizer = TikTokenTokenizer(model=model, max_tokens=self.max_tokens)
elif "gemini" in self.provider.lower():
tokenizer = GeminiTokenizer(model=model, max_tokens=self.max_tokens)
else:
tokenizer = HuggingFaceTokenizer(model=self.model, max_tokens=self.max_tokens)

logger.debug(f"Tokenizer loaded for model: {self.model}")
return tokenizer
5 changes: 3 additions & 2 deletions cognee/infrastructure/databases/vector/embeddings/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@


class EmbeddingConfig(BaseSettings):
embedding_model: Optional[str] = "text-embedding-3-large"
embedding_provider: Optional[str] = "openai"
embedding_model: Optional[str] = "openai/text-embedding-3-large"
embedding_dimensions: Optional[int] = 3072
embedding_endpoint: Optional[str] = None
embedding_api_key: Optional[str] = None
embedding_api_version: Optional[str] = None

embedding_max_tokens: Optional[int] = 8191
model_config = SettingsConfigDict(env_file=".env", extra="allow")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ def get_embedding_engine() -> EmbeddingEngine:

return LiteLLMEmbeddingEngine(
# If OpenAI API is used for embeddings, litellm needs only the api_key.
provider=config.embedding_provider,
api_key=config.embedding_api_key or llm_config.llm_api_key,
endpoint=config.embedding_endpoint,
api_version=config.embedding_api_version,
model=config.embedding_model,
dimensions=config.embedding_dimensions,
max_tokens=config.embedding_max_tokens,
)
1 change: 1 addition & 0 deletions cognee/infrastructure/llm/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .config import get_llm_config
from .utils import get_max_chunk_tokens
3 changes: 2 additions & 1 deletion cognee/infrastructure/llm/anthropic/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class AnthropicAdapter(LLMInterface):
name = "Anthropic"
model: str

def __init__(self, model: str = None):
def __init__(self, max_tokens: int, model: str = None):
self.aclient = instructor.patch(
create=anthropic.Anthropic().messages.create, mode=instructor.Mode.ANTHROPIC_TOOLS
)
self.model = model
self.max_tokens = max_tokens
Comment on lines +17 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Use configured max_tokens instead of hardcoded value

The constructor accepts max_tokens but the acreate_structured_output method uses a hardcoded value of 4096.

Apply this diff to use the configured value:

     async def acreate_structured_output(
         self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
     ) -> BaseModel:
         """Generate a response from a user query."""
 
         return await self.aclient(
             model=self.model,
-            max_tokens=4096,
+            max_tokens=self.max_tokens,
             max_retries=5,
             messages=[

Committable suggestion skipped: line range outside the PR's diff.


async def acreate_structured_output(
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
Expand Down
2 changes: 2 additions & 0 deletions cognee/infrastructure/llm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class LLMConfig(BaseSettings):
llm_api_version: Optional[str] = None
llm_temperature: float = 0.0
llm_streaming: bool = False
llm_max_tokens: int = 16384
transcription_model: str = "whisper-1"

model_config = SettingsConfigDict(env_file=".env", extra="allow")
Expand All @@ -24,6 +25,7 @@ def to_dict(self) -> dict:
"api_version": self.llm_api_version,
"temperature": self.llm_temperature,
"streaming": self.llm_streaming,
"max_tokens": self.llm_max_tokens,
"transcription_model": self.transcription_model,
}

Expand Down
4 changes: 3 additions & 1 deletion cognee/infrastructure/llm/generic_llm_api/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
from typing import List, Type

from pydantic import BaseModel
import instructor
from cognee.infrastructure.llm.llm_interface import LLMInterface
Expand All @@ -16,11 +17,12 @@ class GenericAPIAdapter(LLMInterface):
model: str
api_key: str

def __init__(self, endpoint, api_key: str, model: str, name: str):
def __init__(self, endpoint, api_key: str, model: str, name: str, max_tokens: int):
self.name = name
self.model = model
self.api_key = api_key
self.endpoint = endpoint
self.max_tokens = max_tokens

Comment on lines +20 to 26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

🛠️ Refactor suggestion

Confirmed: max_tokens parameter not utilized in API calls

The max_tokens parameter is added to the constructor but not used in the acreate_structured_output method across all LLM adapters. This leads to inconsistent token limits:

  • GenericAPIAdapter: Not using max_tokens at all
  • OpenAIAdapter: Only using it in transcribe_image
  • AnthropicAdapter: Using hardcoded value 4096

Apply this fix to utilize the max_tokens parameter consistently:

     async def acreate_structured_output(
         self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
     ) -> BaseModel:
         """Generate a response from a user query."""
 
         return await self.aclient.chat.completions.create(
             model=self.model,
+            max_tokens=self.max_tokens,
             messages=[
                 {
                     "role": "user",
🔗 Analysis chain

Utilize max_tokens parameter in API calls

The max_tokens parameter is added to the constructor but not used in the acreate_structured_output method. This could lead to inconsistent token limits.

Apply this diff to utilize the max_tokens parameter:

     async def acreate_structured_output(
         self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
     ) -> BaseModel:
         """Generate a response from a user query."""
 
         return await self.aclient.chat.completions.create(
             model=self.model,
+            max_tokens=self.max_tokens,
             messages=[
                 {
                     "role": "user",
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Find the complete implementation of GenericAPIAdapter
ast-grep --pattern 'class GenericAPIAdapter {
  $$$
}'

# Search for any API call patterns similar to acreate_structured_output
rg "acreate_structured_output|chat\.completions\.create" -A 5

# Look for other adapter implementations
fd "adapter.py|.*adapter.*\.py"

Length of output: 10481


Script:

#!/bin/bash
# Search for max_tokens parameter in API calls
rg "max_tokens.*chat\.completions\.create|chat\.completions\.create.*max_tokens" -B 5 -A 10

# Get complete implementation of the adapters
cat cognee/infrastructure/llm/generic_llm_api/adapter.py
cat cognee/infrastructure/llm/openai/adapter.py
cat cognee/infrastructure/llm/anthropic/adapter.py

Length of output: 9076

llm_config = get_llm_config()

Expand Down
24 changes: 21 additions & 3 deletions cognee/infrastructure/llm/get_llm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def get_llm_client():

provider = LLMProvider(llm_config.llm_provider)

# Check if max_token value is defined in liteLLM for given model
# if not use value from cognee configuration
from cognee.infrastructure.llm.utils import (
get_model_max_tokens,
) # imported here to avoid circular imports

model_max_tokens = get_model_max_tokens(llm_config.llm_model)
max_tokens = model_max_tokens if model_max_tokens else llm_config.llm_max_tokens

if provider == LLMProvider.OPENAI:
if llm_config.llm_api_key is None:
raise InvalidValueError(message="LLM API key is not set.")
Expand All @@ -32,6 +41,7 @@ def get_llm_client():
api_version=llm_config.llm_api_version,
model=llm_config.llm_model,
transcription_model=llm_config.transcription_model,
max_tokens=max_tokens,
streaming=llm_config.llm_streaming,
)

Expand All @@ -42,13 +52,17 @@ def get_llm_client():
from .generic_llm_api.adapter import GenericAPIAdapter

return GenericAPIAdapter(
llm_config.llm_endpoint, llm_config.llm_api_key, llm_config.llm_model, "Ollama"
llm_config.llm_endpoint,
llm_config.llm_api_key,
llm_config.llm_model,
"Ollama",
max_tokens=max_tokens,
)

elif provider == LLMProvider.ANTHROPIC:
from .anthropic.adapter import AnthropicAdapter

return AnthropicAdapter(llm_config.llm_model)
return AnthropicAdapter(max_tokens=max_tokens, model=llm_config.llm_model)

elif provider == LLMProvider.CUSTOM:
if llm_config.llm_api_key is None:
Expand All @@ -57,7 +71,11 @@ def get_llm_client():
from .generic_llm_api.adapter import GenericAPIAdapter

return GenericAPIAdapter(
llm_config.llm_endpoint, llm_config.llm_api_key, llm_config.llm_model, "Custom"
llm_config.llm_endpoint,
llm_config.llm_api_key,
llm_config.llm_model,
"Custom",
max_tokens=max_tokens,
)

else:
Expand Down
2 changes: 2 additions & 0 deletions cognee/infrastructure/llm/openai/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
api_version: str,
model: str,
transcription_model: str,
max_tokens: int,
streaming: bool = False,
):
self.aclient = instructor.from_litellm(litellm.acompletion)
Expand All @@ -41,6 +42,7 @@ def __init__(
self.api_key = api_key
self.endpoint = endpoint
self.api_version = api_version
self.max_tokens = max_tokens
self.streaming = streaming

@observe(as_type="generation")
Expand Down
1 change: 1 addition & 0 deletions cognee/infrastructure/llm/tokenizer/Gemini/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .adapter import GeminiTokenizer
44 changes: 44 additions & 0 deletions cognee/infrastructure/llm/tokenizer/Gemini/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import List, Any

from ..tokenizer_interface import TokenizerInterface


class GeminiTokenizer(TokenizerInterface):
def __init__(
self,
model: str,
max_tokens: int = 3072,
):
self.model = model
self.max_tokens = max_tokens

# Get LLM API key from config
from cognee.infrastructure.databases.vector.embeddings.config import get_embedding_config
from cognee.infrastructure.llm.config import get_llm_config

config = get_embedding_config()
llm_config = get_llm_config()

import google.generativeai as genai

genai.configure(api_key=config.embedding_api_key or llm_config.llm_api_key)
Comment on lines +16 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for configuration retrieval and API setup.

The configuration retrieval and API setup lack error handling. Consider adding try-catch blocks to handle potential configuration errors gracefully.

-        config = get_embedding_config()
-        llm_config = get_llm_config()
-
-        import google.generativeai as genai
-
-        genai.configure(api_key=config.embedding_api_key or llm_config.llm_api_key)
+        try:
+            config = get_embedding_config()
+            llm_config = get_llm_config()
+            
+            import google.generativeai as genai
+            
+            api_key = config.embedding_api_key or llm_config.llm_api_key
+            if not api_key:
+                raise ValueError("No API key found in configuration")
+                
+            genai.configure(api_key=api_key)
+        except Exception as e:
+            raise RuntimeError(f"Failed to initialize Gemini tokenizer: {str(e)}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
from cognee.infrastructure.databases.vector.embeddings.config import get_embedding_config
from cognee.infrastructure.llm.config import get_llm_config
config = get_embedding_config()
llm_config = get_llm_config()
import google.generativeai as genai
genai.configure(api_key=config.embedding_api_key or llm_config.llm_api_key)
from cognee.infrastructure.databases.vector.embeddings.config import get_embedding_config
from cognee.infrastructure.llm.config import get_llm_config
try:
config = get_embedding_config()
llm_config = get_llm_config()
import google.generativeai as genai
api_key = config.embedding_api_key or llm_config.llm_api_key
if not api_key:
raise ValueError("No API key found in configuration")
genai.configure(api_key=api_key)
except Exception as e:
raise RuntimeError(f"Failed to initialize Gemini tokenizer: {str(e)}")


def extract_tokens(self, text: str) -> List[Any]:
raise NotImplementedError
dexters1 marked this conversation as resolved.
Show resolved Hide resolved

def count_tokens(self, text: str) -> int:
"""
Returns the number of tokens in the given text.
Args:
text: str

Returns:
number of tokens in the given text

"""
import google.generativeai as genai

return len(genai.embed_content(model=f"models/{self.model}", content=text))
Comment on lines +29 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for token counting.

The count_tokens method should handle potential API errors and validate inputs.

     def count_tokens(self, text: str) -> int:
-        """
-        Returns the number of tokens in the given text.
-        Args:
-            text: str
-
-        Returns:
-            number of tokens in the given text
-
-        """
-        import google.generativeai as genai
-
-        return len(genai.embed_content(model=f"models/{self.model}", content=text))
+        """Returns the number of tokens in the given text.
+        
+        Args:
+            text: str
+
+        Returns:
+            int: number of tokens in the given text
+            
+        Raises:
+            ValueError: If text is empty or None
+            RuntimeError: If token counting fails
+        """
+        if not text:
+            raise ValueError("Input text cannot be empty")
+            
+        try:
+            import google.generativeai as genai
+            result = genai.embed_content(
+                model=f"models/{self.model}",
+                content=text
+            )
+            return len(result)
+        except Exception as e:
+            raise RuntimeError(f"Failed to count tokens: {str(e)}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def count_tokens(self, text: str) -> int:
"""
Returns the number of tokens in the given text.
Args:
text: str
Returns:
number of tokens in the given text
"""
import google.generativeai as genai
return len(genai.embed_content(model=f"models/{self.model}", content=text))
def count_tokens(self, text: str) -> int:
"""Returns the number of tokens in the given text.
Args:
text: str
Returns:
int: number of tokens in the given text
Raises:
ValueError: If text is empty or None
RuntimeError: If token counting fails
"""
if not text:
raise ValueError("Input text cannot be empty")
try:
import google.generativeai as genai
result = genai.embed_content(
model=f"models/{self.model}",
content=text
)
return len(result)
except Exception as e:
raise RuntimeError(f"Failed to count tokens: {str(e)}")


def trim_text_to_max_tokens(self, text: str) -> str:
raise NotImplementedError
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .adapter import HuggingFaceTokenizer
36 changes: 36 additions & 0 deletions cognee/infrastructure/llm/tokenizer/HuggingFace/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List, Any

from transformers import AutoTokenizer

from ..tokenizer_interface import TokenizerInterface


class HuggingFaceTokenizer(TokenizerInterface):
def __init__(
self,
model: str,
max_tokens: int = 512,
):
self.model = model
self.max_tokens = max_tokens

self.tokenizer = AutoTokenizer.from_pretrained(model)
dexters1 marked this conversation as resolved.
Show resolved Hide resolved

def extract_tokens(self, text: str) -> List[Any]:
tokens = self.tokenizer.tokenize(text)
return tokens

def count_tokens(self, text: str) -> int:
"""
Returns the number of tokens in the given text.
Args:
text: str

Returns:
number of tokens in the given text

"""
return len(self.tokenizer.tokenize(text))

def trim_text_to_max_tokens(self, text: str) -> str:
raise NotImplementedError
dexters1 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions cognee/infrastructure/llm/tokenizer/TikToken/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .adapter import TikTokenTokenizer
Loading
Loading