A flexible LLM client with multi-provider support, built using LiteLLM + Pydantic for seamless integration across OpenAI, Anthropic, and other providers.
# Clone the repository
git clone https://github.com/Cellular-Semantics/cellsem_llm_client.git
cd cellsem_llm_client
# Install uv (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create environment and install dependencies
uv sync --dev
# Set up pre-commit hooks (optional but recommended)
uv run pre-commit installCreate a .env file in the project root:
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here
# Anthropic Configuration
ANTHROPIC_API_KEY=your_anthropic_api_key_herefrom cellsem_llm_client import create_openai_agent, create_anthropic_agent
# Create agents with automatic environment configuration
openai_agent = create_openai_agent()
anthropic_agent = create_anthropic_agent()
# Simple queries
openai_response = openai_agent.query("Explain quantum computing in 50 words")
claude_response = anthropic_agent.query("What are the benefits of renewable energy?")
# Custom configuration
custom_agent = create_openai_agent(
model="gpt-4",
max_tokens=2000
)Full Documentation: https://cellular-semantics.github.io/cellsem_llm_client/
The documentation is built automatically from the docs/ folder on each push to main using GitHub Actions.
Quick links:
- Installation Guide
- Quick Start Tutorial
- Development Guidelines
- API Reference (auto-generated)
- Schema Enforcement
- Cost Tracking
STATUS - beta
- β Multi-Provider Support: Seamless switching between OpenAI, Anthropic, and other LiteLLM-supported providers
- β Environment-Based Configuration: Automatic API key loading from environment variables
- β Type Safety: Full MyPy type checking with strict configuration
- β Abstract Base Classes: Clean architecture with provider-specific implementations
- β Error Handling: Robust validation and error management
- β Configuration Utilities: Helper functions for quick agent setup
STATUS - beta
- β Real-time Cost Tracking: Direct integration with OpenAI and Anthropic usage APIs (aggregate per-key)
- β Token Usage Metrics: Detailed tracking of input, output, cached, and thinking tokens
- β
Cost Calculation: Automated cost computation with fallback rate database (per-request precision); enabled by default when
track_usage=True(opt-out available) - β Usage Analytics: Comprehensive reporting and cost optimization insights
STATUS - beta
- β
Native Schema Support: OpenAI structured outputs with
strict=trueenforcement - β Tool Use Integration: Anthropic schema validation via tool use patterns
- β Pydantic Integration: Automatic model validation and retry logic
- β Cross-Provider Compatibility: Unified schema interface across all providers
- β JSON-First Inputs: Prefer plain JSON Schema dicts; Pydantic models are optional helpers
STATUS - alpha
- β
LiteLLM Tool Loop:
LiteLLMAgent.query_with_toolsexecutes tool calls and resumes the conversation - β
OLS4 MCP Tool: Built-in
ols4_searchhelper targeting the EBI OLS4 MCP (with legacy fallback) for ontology lookups - β Integration Coverage: Live test hits OLS4 for βBergmann glial cellβ to verify real responses
- β Composable: Tool definitions + handlers returned together for easy plug-in to agents
STATUS - beta
- π― Use JSON Schema Directly: Pass a JSON Schema
dictto enforce structure; optionally derive it from a Pydantic model viamodel_json_schema()or by schema name (resolves<name>.jsonin your schema directory). - π Provider-Aware: OpenAI uses strict
response_format; Anthropic converts the schema to a single enforced tool call; other providers get prompt hints plus post-validation. - π Validate & Retry: Responses are parsed and validated against the derived Pydantic model with lightweight retries; hard failures raise
SchemaValidationException. - π§° Runtime Model Generation:
SchemaManagerloads schemas from dict/file/URL and generates/caches Pydantic models on the fly. - πͺ Example (JSON-first):
from cellsem_llm_client.agents import LiteLLMAgent schema = { "type": "object", "properties": { "term": {"type": "string", "description": "Cell type name"}, "iri": {"type": "string", "format": "uri"}, }, "required": ["term", "iri"], "additionalProperties": False, } agent = LiteLLMAgent(model="gpt-4o", api_key="your-key") result = agent.query_with_schema( message="Return a cell type name and IRI.", schema=schema, # JSON-first ) print(result.model_dump()) # Pydantic model generated at runtime
- β³ Multi-Format Support: Images (PNG, JPEG, WebP), PDFs, and documents
- β³ Provider Abstraction: Unified file API across different LLM providers
- β³ Capability Detection: Automatic model file support validation
- β³ Flexible Input: Base64, URL, and file path support
- β³ Task Complexity Analysis: AI-powered prompt difficulty assessment
- β³ Model Selection: Intelligent recommendations based on task requirements
- β³ Cost Optimization: Balance performance and cost for optimal model choice
- β³ Token Estimation: Predict token usage for better planning
cellsem_llm_client/
βββ agents/ # Core LLM agent implementations
βββ utils/ # Configuration and helper utilities
βββ tracking/ # Token usage and cost monitoring
βββ schema/ # JSON schema validation and compliance
βββ files/ # File attachment processing (Stub)
βββ advisors/ # AI-powered model recommendations (Stub)
- Python: 3.11+
- Dependencies: LiteLLM, Pydantic, python-dotenv
- API Keys: OpenAI and/or Anthropic API keys for full functionality
- Follow the TDD workflow defined in
CLAUDE.md - Write tests first, implement features to pass tests
- Ensure all quality checks pass:
ruff,mypy,pytest - Maintain >85% test coverage
- Use conventional commit messages
See planning/ROADMAP.md for detailed implementation plans for pending features.
- Unit Tests: Fast, isolated tests with mocked dependencies
- Integration Tests: Real API validation in development, controlled mocks in CI
- Environment-Based:
USE_MOCKS=truefor CI, real APIs for local development - Coverage: >90% code coverage maintained across all modules
# Run tests
uv run pytest # All tests
uv run pytest -m unit # Unit tests only
uv run pytest -m integration # Integration tests only
# Code quality checks
uv run ruff check --fix src/ tests/ # Lint and auto-fix
uv run ruff format src/ tests/ # Format code
uv run mypy src/ # Type checking
# Run with coverage
uv run pytest --cov=cellsem_llm_client --cov-report=htmlMIT License - see LICENSE file for details.