Skip to content

Add Grok bot integration for Jupyter notebooks - #666

Open
hakan263774-web wants to merge 1 commit into
github:mainfrom
hakan263774-web:hakan263774-web-grok-bot-integration
Open

Add Grok bot integration for Jupyter notebooks#666
hakan263774-web wants to merge 1 commit into
github:mainfrom
hakan263774-web:hakan263774-web-grok-bot-integration

Conversation

@hakan263774-web

@hakan263774-web hakan263774-web commented Aug 31, 2026

Copy link
Copy Markdown

Overview

This PR implements a complete Grok bot integration for the Codespaces Jupyter environment, enabling users to leverage XAI's Grok conversational AI model directly within notebooks.

Implementation Details

Package Structure

  • grok_bot/: Python package with two core modules
    • api.py: GrokClient class for API communication with configurable endpoints and auth
    • magic.py: IPython magic commands (%grok and %%grok) for notebook integration
    • __init__.py: Safe imports that gracefully handle missing IPython in non-notebook contexts

Features

  1. IPython Magic Commands:

    • %grok <prompt> for single-line queries
    • %%grok for multi-line cell prompts
    • Responses rendered as Markdown for readable output
  2. Authentication & Configuration:

    • Reads GROK_API_KEY from environment (Codespaces secret or shell)
    • Optional custom GROK_API_URL for alternative endpoints
    • Configurable timeout (default 30s)
  3. Error Handling:

    • Clear error messages when API key is missing
    • HTTP request failures caught and wrapped as RuntimeError
    • Graceful fallback when IPython isn't available

Files Changed

  • grok_bot/: New package (3 modules)
  • requirements.txt: Added requests>=2.30.0
  • GROK_INTEGRATION.md: Comprehensive usage and auth documentation
  • .devcontainer/devcontainer.json: Added containerEnv mapping for GROK_API_KEY
  • README.md: Added Grok bot quick-start section
  • notebooks/grok-example.ipynb: Example notebook demonstrating all features
  • test_grok_bot.py: 11 comprehensive unit tests (all passing)

Testing

All tests pass:

11 passed, 1 skipped (skipped: IPython not available in test environment)

Tests cover:

  • Client initialization (with args, env vars, missing key)
  • Custom API URLs and timeouts
  • Request payload and headers
  • Response parsing (text and choices formats)
  • Error handling
  • Magic module structure

Usage Example

%load_ext grok_bot.magic

%grok What is machine learning?

%%grok
Analyze my dataset with these columns:
- Age, Score, Name
What patterns do you see?

Security Notes

  • API keys should be set via Codespaces secrets or environment variables
  • devcontainer.json uses ${localEnv:GROK_API_KEY} to forward local secrets
  • No API keys committed in code

Verification

✅ All 11 unit tests passing
✅ Package imports work correctly
✅ GrokClient initializes and validates configuration
✅ IPython extension loads without errors
✅ Example notebook provided with comprehensive examples
✅ Documentation complete and clear

Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com

Adds grok_bot package with GrokClient and IPython magic.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant