Utils Guide
LangCrew includes utility functions for common development tasks. These are primarily internal functions, but can be useful for advanced use cases.
Available Utilities
Section titled “Available Utilities”File Detection
Section titled “File Detection”Check if files are binary or text:
from langcrew.utils import is_binary_file, is_text_file
with open('document.pdf', 'rb') as f: data = f.read() if is_binary_file(data): print("Binary file") elif is_text_file(data): print("Text file")Token Counting
Section titled “Token Counting”Count tokens for LLM models:
from langcrew.context.token_utils import count_message_tokensfrom langchain_core.messages import HumanMessagefrom langchain_openai import ChatOpenAI
# Create an LLM instance (required for token counting)llm = ChatOpenAI(model="gpt-4o-mini")
messages = [HumanMessage(content="Hello world")]token_count = count_message_tokens(messages, llm)print(f"Tokens: {token_count}")Language Detection
Section titled “Language Detection”Simple Chinese/English detection:
from langcrew.utils import detect_language, detect_chinese
# Detect languagelang = detect_language("你好世界") # "zh"lang = detect_language("Hello") # "en"
# Check for Chineseis_chinese = detect_chinese("你好世界") # TrueMessage ID Generation
Section titled “Message ID Generation”Generate unique message IDs:
from langcrew.utils import generate_message_id
msg_id = generate_message_id() # "1748438204041_a7k9"When to Use
Section titled “When to Use”Most users won’t need these utilities directly. They’re primarily used internally by LangCrew components. Focus on the main Agent, Task, and Crew concepts instead.
API Reference
Section titled “API Reference”For complete API details, see the source code documentation in the langcrew.utils module.