The Contextualizer project is an AI agent framework built on pydantic-ai
that uses the Berkeley Lab's CBORG API to
access various language models. This guide combines official documentation, notes, and practical experience to help you
get started with the project.
- Python 3.10+ installed (3.10+ is specified in pyproject.toml)
uv
installed (a faster alternative to pip)- CBORG API key (required for all agents)
- Google Maps API key (only required for map functionality in geo_agent.py)
- python-dotenv package (installed automatically as a dependency)
CBORG is Berkeley Lab's AI Portal that provides secure access to various AI models. The CBORG API server is an OpenAI-compatible proxy server built on LiteLLM, which means it can be used as a drop-in replacement for OpenAI's API.
The CBORG API provides access to various models. Based on testing, your account may have access to:
-
LBL-hosted models:
- lbl/cborg-chat:latest
- lbl/cborg-vision:latest
- lbl/nomic-embed-text
-
Commercial models:
- openai/gpt-4.1-nano
- aws/claude-haiku
- (potentially others)
Note that not all models listed in documentation may be available to your specific API key. You can use the test connection script below to see which models are accessible to you.
# Install UV on Unix/macOS
curl -LsSf https://astral.sh/uv/install.sh | sh
# Add to PATH if needed
# Navigate to your repository
cd contextualizer
# Create a virtual environment with UV
uv venv
# Activate the virtual environment
source .venv/bin/activate
# Install development dependencies
uv pip install -e .
The project requires Python 3.10+. If you're using pyenv and encounter version issues:
# Install Python 3.10 with pyenv
pyenv install 3.10
# Or remove .python-version file to use UV's Python directly
rm .python-version
You have several options:
-
Create a .env file (recommended):
echo "CBORG_API_KEY=your_cborg_api_key_here GOOGLE_MAPS_API_KEY=your_google_maps_api_key_here" > .env
The project uses python-dotenv to load environment variables from this file.
-
Export in your shell:
export CBORG_API_KEY="your_cborg_api_key_here" export GOOGLE_MAPS_API_KEY="your_google_maps_api_key_here" # Only if using map functions
-
Use the .venv/bin/python approach:
CBORG_API_KEY="your_cborg_api_key_here" .venv/bin/python your_script.py
- If affiliated with Berkeley Lab: CBORG is free for employees with @lbl.gov, @es.net, or @nersc.gov email.
You can run any of the agents using the Makefile:
# Run the hello world example
make hello-world
# Run the geo agent
make geo
# Run the soil agent
make soil
# Run the weather agent
make weather
# Run the Wikipedia animal QA agent
make wiki
Most agents in this repository follow this pattern:
- Load environment variables and API key (using python-dotenv)
- Configure an AI model with the CBORG provider
- Create an Agent with a system prompt
- Register tools using decorators
- Execute queries with the agent
Tools can be defined and registered using the @agent.tool_plain
decorator, as shown in geo_agent.py:
@geo_agent.tool_plain
def get_elev(
lat: float, lon: float,
) -> float:
"""
Get the elevation of a location.
:param lat: latitude
:param lon: longitude
:return: elevation in m
"""
print(f"Looking up elevation for lat={lat}, lon={lon}")
return elevation((lat, lon))
- hello_world.py: Simple "Hello World" example agent
- geo_agent.py: Geographic data agent (needs GOOGLE_MAPS_API_KEY for map functionality)
- soil_agent.py: Soil science agent
- weather.at.py: Weather information agent
- wikipedia_animal_qa.py: Wikipedia animal information agent
- evelation_info.py: Tool for elevation information (used by geo_agent)
If you see errors like: ModuleNotFoundError: No module named 'agent_test'
:
-
Use the virtual environment Python directly (as configured in the Makefile):
.venv/bin/python -m pytest tests/ # or use the Makefile targets make test-agent make test-minimal
-
Or make sure you've activated the virtual environment:
source .venv/bin/activate pytest tests/
If your CBORG API key isn't being loaded:
- Check that your
.env
file exists and contains the correct key - Verify that python-dotenv is installed (it should be installed automatically with dependencies)
- Ensure the .env file is in the root directory of the project
If you see errors related to model availability:
- Update your agent code to use an available model (like "lbl/cborg-chat:latest")
- Check the error message for specific details about the failure
The repository includes tests in the tests/
directory with corresponding targets in the Makefile
:
# Run all tests
make test-agent test-minimal
# Run specific tests
make test-agent
make test-minimal
The Makefile uses .venv/bin/pytest
to ensure the correct Python environment is used.
For more reliable testing, you can also run tests directly:
.venv/bin/pytest tests/test_agent.py -v
.venv/bin/pytest tests/test_minimal_agent.py -v
The project follows these coding standards:
- Imports: Standard grouping (stdlib, third-party, local)
- Type Annotations: All functions should use Python type hints
- Docstrings: Multi-line docstring with params/returns (triple quotes)
- Naming: snake_case for variables/functions, PascalCase for classes
- Error Handling: Use try/except with logging, avoid silent failures
- Tools: Use @agent.tool_plain decorator for agent functions
- Async: Both sync and async functions are used; choose appropriately
If you're not affiliated with Berkeley Lab and can't get a CBORG API key, you can modify the repository to use other LLM providers:
- Change the base_url to your preferred provider
- Update the model names to ones available on that provider
- Adjust the API authentication methods as needed
Example for using OpenAI directly:
ai_model = OpenAIModel(
"gpt-4-turbo", # Use your available model
provider=OpenAIProvider(
api_key=os.getenv("OPENAI_API_KEY")), # No need for base_url with direct OpenAI
)
- CBORG API Portal: https://cborg.lbl.gov/
- API Documentation: https://cborg.lbl.gov/api_docs/
- API Examples: https://cborg.lbl.gov/api_examples/
- Pydantic-AI: Framework for building AI agents used by this project