text
stringlengths 0
131
|
|---|
import json
|
import logging
|
import uuid
|
import math
|
import os
|
import sys
|
from dataclasses import dataclass, field
|
from typing import List, Dict, Any, Set, Optional
|
# -----------------------------------------------------------------------------
|
# Imports & Dependency Checks
|
# -----------------------------------------------------------------------------
|
try:
|
import yaml
|
except ImportError:
|
print("Error: 'PyYAML' is required. Install via 'pip install pyyaml'.")
|
sys.exit(1)
|
try:
|
from openai import OpenAI, OpenAIError
|
except ImportError:
|
print("Error: 'openai' is required. Install via 'pip install openai'.")
|
sys.exit(1)
|
# We check for transformers inside the class to avoid crashing if
|
# the user wants heuristic mode but doesn't have transformers installed.
|
try:
|
from transformers import AutoTokenizer
|
TRANSFORMERS_AVAILABLE = True
|
except ImportError:
|
TRANSFORMERS_AVAILABLE = False
|
# -----------------------------------------------------------------------------
|
# Logging
|
# -----------------------------------------------------------------------------
|
logging.basicConfig(
|
level=logging.DEBUG,
|
format='[%(levelname)s] %(asctime)s - %(funcName)s:%(lineno)d - %(message)s',
|
datefmt='%H:%M:%S'
|
)
|
logger = logging.getLogger(__name__)
|
# -----------------------------------------------------------------------------
|
# Configuration
|
# -----------------------------------------------------------------------------
|
@dataclass
|
class GroupInterval:
|
start: int
|
end: int
|
line_numbers: Set[int]
|
@dataclass
|
class ChunkingConfig:
|
"""Configuration object loaded from YAML."""
|
api_key: str
|
llm_model_name: str
|
temperature: float
|
# Tokenization
|
tokenizer_method: str
|
hf_model_name: str
|
heuristic_chars_per_token: int
|
# Limits
|
llm_token_limit: int
|
overlap_token_count: int
|
model_token_limit: int
|
# Prompts
|
system_prompt_base: str
|
@classmethod
|
def from_yaml(cls, path: str) -> 'ChunkingConfig':
|
if not os.path.exists(path):
|
raise FileNotFoundError(f"Config file not found at: {path}")
|
logger.info(f"Loading configuration from {path}...")
|
with open(path, 'r') as f:
|
data = yaml.safe_load(f)
|
oa = data.get('openai', {})
|
tok = data.get('tokenization', {})
|
tok_heu = tok.get('heuristic', {})
|
tok_hf = tok.get('huggingface', {})
|
lim = data.get('limits', {})
|
prompts = data.get('prompts', {})
|
raw_key = oa.get('api_key', 'ENV')
|
api_key = os.getenv("OPENAI_API_KEY") if raw_key == "ENV" else raw_key
|
return cls(
|
api_key=api_key or "MISSING_KEY",
|
llm_model_name=oa.get('model_name', 'gpt-4o-mini'),
|
temperature=oa.get('temperature', 0.0),
|
# Tokenizer Config
|
tokenizer_method=tok.get('method', 'heuristic'),
|
hf_model_name=tok_hf.get('model_name', 'gpt2'),
|
heuristic_chars_per_token=tok_heu.get('chars_per_token', 4),
|
End of preview. Expand
in Data Studio
No dataset card yet
- Downloads last month
- 20