Skip to main content

Google Gemini provider forClaif (Command-Line AI Framework)

Project description

#claif_gem - Google Gemini Provider forClaif

Quickstart

CLAIF_GEM provides a Python interface and CLI wrapper for Google's Gemini AI models. It integrates the Gemini CLI tool into theClaif framework, enabling you to query Gemini models with a simple command or Python API call. Version 1.0.4 improves subprocess reliability by switching to native asyncio.

pip install claif_gem && claif-gem query "Explain quantum computing in one sentence"

CLAIF_GEM is the Google Gemini provider implementation for theClaif (Command-Line Artificial Intelligence Framework). It wraps the Gemini CLI to integrate Google's Gemini AI models into the unifiedClaif ecosystem.

What isclaif_gem?

CLAIF_GEM provides a Python interface and command-line wrapper for the Google Gemini CLI tool. It enables seamless integration of Google's Gemini language models with theClaif framework through subprocess management and message translation.

Key features:

  • Subprocess-based integration with the Gemini CLI binary
  • Automatic CLI discovery across multiple platforms (Windows, macOS, Linux)
  • Fire-based CLI with rich terminal output for direct usage
  • Async/await support for efficient concurrent operations
  • Auto-approval and yes-mode for streamlined workflows
  • Flexible configuration via environment variables and options
  • Robust error handling with timeout protection

Installation

Prerequisites

You need to have the Gemini CLI installed. Install it via npm:

npm install -g @google/gemini-cli

Or set the path to an existing installation:

export GEMINI_CLI_PATH=/path/to/gemini

From PyPI

pip install claif_gem

From Source

git clone https://github.com/twardoch/claif_gem.git
cd claif_gem
pip install -e .

WithClaif Framework

# InstallClaif with all providers
pip install claif[all]

# Or just with Gemini support
pip install claif claif_gem

CLI Usage

CLAIF_GEM provides a Fire-based CLI for direct interaction with Gemini:

Basic Commands

# Simple query
claif-gem query "Explain quantum computing"

# Query with specific model
claif-gem query "Write a haiku" --model gemini-2.5-pro

# Query with parameters
claif-gem query "Analyze this code" --temperature 0.3 --max-context 8000

# With system prompt
claif-gem query "Translate to Spanish" --system "You are a professional translator"

# Stream responses
claif-gem stream "Tell me a story"

# Health check
claif-gem health

# List available models
claif-gem models

# Show configuration
claif-gem config show

Options

  • --model: Specify Gemini model (default: gemini-2.5-pro)
  • --temperature: Control randomness (0-1)
  • --system: Set system prompt for context
  • --auto-approve: Auto-approve tool usage (default: true)
  • --yes-mode: Automatically confirm all prompts (default: true)
  • --max-context: Maximum context length
  • --timeout: Query timeout in seconds
  • --show-metrics: Display response metrics
  • --verbose: Enable debug output

Python API Usage

Basic Usage

import asyncio
from claif_gem import query, GeminiOptions

async def main():
    # Simple query
    async for message in query("Hello, Gemini!"):
        print(message.content)
    
    # Query with options
    options = GeminiOptions(
        model="gemini-2.5-pro",
        temperature=0.7,
        system_prompt="You are a helpful coding assistant",
        auto_approve=True,
        yes_mode=True
    )
    
    async for message in query("Explain Python decorators", options):
        print(message.content)

asyncio.run(main())

Direct Client Usage

from claif_gem.client import GeminiClient
from claif_gem.types import GeminiOptions

async def main():
    client = GeminiClient()
    options = GeminiOptions(
        model="gemini-2.5-pro",
        verbose=True
    )
    
    async for message in client.query("What is machine learning?", options):
        print(f"[{message.role}]: {message.content}")

asyncio.run(main())

Transport Layer Access

from claif_gem.transport import GeminiTransport
from claif_gem.types import GeminiOptions

async def main():
    transport = GeminiTransport()
    options = GeminiOptions(timeout=60)
    
    async for response in transport.send_query("Complex analysis", options):
        if hasattr(response, 'content'):
            print(response.content)

asyncio.run(main())

How It Works

Architecture Overview

┌─────────────────────┐
│   User Application  │
├─────────────────────┤
│   Claif Core       │
├─────────────────────┤
│   `claif_gem`        │
│  ┌───────────────┐  │
│  │   __init__.py │  │ ← Main entry point,Claif interface
│  ├───────────────┤  │
│  │    cli.py     │  │ ← Fire-based CLI commands
│  ├───────────────┤  │
│  │   client.py   │  │ ← Client orchestration
│  ├───────────────┤  │
│  │ transport.py  │  │ ← Subprocess management
│  ├───────────────┤  │
│  │   types.py    │  │ ← Type definitions
│  └───────────────┘  │
├─────────────────────┤
│  Subprocess Layer   │
├─────────────────────┤
│   Gemini CLI Binary │ ← External Node.js CLI
└─────────────────────┘

Component Details

1. Main Module (__init__.py)

  • Provides the query() function as the main entry point
  • ConvertsClaif's ClaifOptions to GeminiOptions
  • Delegates to the client module for execution
  • Exports public API: query and GeminiOptions

2. CLI Module (cli.py)

  • Fire-based command-line interface
  • Commands: query, stream, health, models, config
  • Rich console output with progress indicators
  • Async execution with proper error handling
  • Response formatting and metrics display

3. Client Module (client.py)

  • GeminiClient class manages the query lifecycle
  • Coordinates with transport layer
  • Converts Gemini messages toClaif message format
  • Module-level _client instance for convenience

4. Transport Module (transport.py)

  • GeminiTransport handles subprocess communication
  • CLI discovery across platforms (npm paths, common locations)
  • Command-line argument construction from options
  • Async subprocess execution with anyio
  • Output parsing (JSON and plain text)
  • Error handling and timeout management

5. Types Module (types.py)

  • GeminiOptions: Configuration dataclass
  • GeminiMessage: Response message type
  • ResultMessage: Metadata and error information
  • Type conversion methods toClaif formats

Message Flow

  1. Query Entry: User calls query() with prompt and options
  2. Option Translation:Claif options → GeminiOptions
  3. Client Processing: GeminiClient validates and prepares query
  4. Transport Execution:
    • Find Gemini CLI binary
    • Build command with arguments
    • Spawn subprocess with anyio
    • Read stdout/stderr streams
  5. Response Parsing:
    • Try JSON parsing first
    • Fallback to plain text
    • Convert to GeminiMessage
  6. Message Conversion: GeminiMessage →Claif Message
  7. Async Yield: Messages yielded to caller

CLI Discovery

The transport layer searches for the Gemini CLI in this order:

  1. GEMINI_CLI_PATH environment variable
  2. System PATH (which gemini)
  3. Common installation paths:
    • ~/.local/bin/gemini
    • /usr/local/bin/gemini
    • /opt/gemini/bin/gemini
  4. NPM global paths:
    • Windows: %APPDATA%/npm/gemini.cmd
    • Unix: ~/.npm-global/bin/gemini
    • System: /usr/local/lib/node_modules/.bin/gemini

Command Construction

The Gemini CLI is invoked with arguments based on options:

gemini \
  -m <model> \
  -a  # auto-approve
  -y  # yes-mode
  -t <temperature> \
  -s <system-prompt> \
  --max-context <length> \
  -p "prompt"

Environment Variables

  • GEMINI_CLI_PATH: Path to Gemini CLI binary
  • GEMINI_SDK=1: Set by transport to indicate SDK usage
  • CLAIF_PROVIDER=gemini: Provider identification

Why Useclaif_gem?

  1. Unified Interface: Access Gemini through the standardClaif API
  2. Cross-Platform: Automatic CLI discovery works on Windows, macOS, Linux
  3. Async First: Built on anyio for efficient concurrent operations
  4. Rich CLI: Fire-based interface with progress indicators and formatting
  5. Type Safety: Full type hints for better IDE support
  6. Error Handling: Robust subprocess management with timeout protection
  7. Flexible Configuration: Environment variables, options, and config files

Development

Project Structure

claif_gem/
├── src/claif_gem/
│   ├── __init__.py      # Main entry point
│   ├── cli.py           # Fire CLI implementation
│   ├── client.py        # Client orchestration
│   ├── transport.py     # Subprocess management
│   └── types.py         # Type definitions
├── tests/
│   └── test_package.py  # Basic tests
├── pyproject.toml       # Package configuration
└── README.md            # This file

Running Tests

# Install development dependencies
pip install -e ".[dev,test]"

# Run tests
pytest

# Run with coverage
pytest --cov=claif_gem

# Run linting
ruff check src/claif_gem
ruff format src/claif_gem

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Ensure all tests pass
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claif_gem-1.0.6.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claif_gem-1.0.6-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file claif_gem-1.0.6.tar.gz.

File metadata

  • Download URL: claif_gem-1.0.6.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for claif_gem-1.0.6.tar.gz
Algorithm Hash digest
SHA256 93d76250a48c64de6f693cfb2019e1e7231612c3a15e2f2e13df100857d56b07
MD5 90fc86099d00d2c3411ff122101000b2
BLAKE2b-256 a29193b1d72d4ca94c2c0053f35ab75d2fe025fbea64430caad9b02f1781a933

See more details on using hashes here.

File details

Details for the file claif_gem-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: claif_gem-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for claif_gem-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 db3b7e25d45174eaa8b68675529359d17e23d600faaa2988afcd6eb3aabfe607
MD5 7c9b255dcf678c9b378e2296b270e57b
BLAKE2b-256 c5f970fc9315cc666724bd598485fc20c6d3d686b50e8c2ba9ceded9e279f2ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page