6b7e6b44f1
Python Build and Type Check / python-ci (ubuntu-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
gh-pages / build (push) Has been cancelled
Python Publish (pypi) / Upload release to PyPI (push) Has been cancelled
Spellcheck / spellcheck (push) Has been cancelled
52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
# Copyright (c) 2024 Microsoft Corporation.
|
|
# Licensed under the MIT License
|
|
|
|
"""Tokenizer model configuration."""
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
|
|
from graphrag_llm.config.types import TokenizerType
|
|
|
|
|
|
class TokenizerConfig(BaseModel):
|
|
"""Configuration for a tokenizer."""
|
|
|
|
model_config = ConfigDict(extra="allow")
|
|
"""Allow extra fields to support custom LLM provider implementations."""
|
|
|
|
type: str = Field(
|
|
default=TokenizerType.LiteLLM,
|
|
description="The type of tokenizer to use. [litellm] (default: litellm).",
|
|
)
|
|
|
|
model_id: str | None = Field(
|
|
default=None,
|
|
description="The identifier for the tokenizer model. Example: openai/gpt-4o. Used by the litellm tokenizer.",
|
|
)
|
|
|
|
encoding_name: str | None = Field(
|
|
default=None,
|
|
description="The encoding name for the tokenizer. Example: gpt-4o.",
|
|
)
|
|
|
|
def _validate_litellm_config(self) -> None:
|
|
"""Validate LiteLLM tokenizer configuration."""
|
|
if self.model_id is None or self.model_id.strip() == "":
|
|
msg = "model_id must be specified for LiteLLM tokenizer."
|
|
raise ValueError(msg)
|
|
|
|
def _validate_tiktoken_config(self) -> None:
|
|
"""Validate TikToken tokenizer configuration."""
|
|
if self.encoding_name is None or self.encoding_name.strip() == "":
|
|
msg = "encoding_name must be specified for TikToken tokenizer."
|
|
raise ValueError(msg)
|
|
|
|
@model_validator(mode="after")
|
|
def _validate_model(self):
|
|
"""Validate the tokenizer configuration based on its type."""
|
|
if self.type == TokenizerType.LiteLLM:
|
|
self._validate_litellm_config()
|
|
elif self.type == TokenizerType.Tiktoken:
|
|
self._validate_tiktoken_config()
|
|
return self
|