Skip to content

Repository files navigation

JupyterLab AI Assistant

一个功能强大的 JupyterLab 智能助手插件,在右侧边栏提供 AI 对话面板,并支持与左侧工作区的 Notebook Cell 无缝联动。

✨ 特性

  • 🤖 AI 智能对话:在右侧边栏与 AI 进行自然语言对话,获取编程帮助
  • 🔗 Cell 联动:一键获取当前选中 Cell 的代码内容,将 AI 生成的代码插入到 Notebook
  • 📝 代码执行:支持远程执行指定的 Cell 并获取执行结果
  • 🎨 侧边栏界面:优雅的可展开/收起右侧面板,与 JupyterLab 原生 UI 完美融合
  • ⚙️ 灵活配置:支持多种 AI 模型配置和个性化设置
  • 📊 Markdown 渲染:支持 AI 响应的 Markdown 格式渲染,包括代码高亮

📦 安装

前置要求

  • JupyterLab >= 4.0.0
  • Node.js >= 18.0.0
  • Python >= 3.8

本地安装

# 克隆项目
git clone https://github.com/ai-team/jupyterlab-ai-assistant.git
cd jupyterlab-ai-assistant

# 安装 Python 依赖
pip install -e .

# 安装前端依赖
npm install

# 构建扩展
npm run build

# 启用扩展
jupyter labextension enable jupyterlab_ai_assistant

开发模式

# 前端开发模式(监听文件变化)
npm run watch

# 后端开发模式
# 在另一个终端运行 JupyterLab
jupyter lab

🚀 使用指南

启动插件

安装完成后,重启 JupyterLab,在右侧边栏找到 AI Assistant 图标,点击即可打开智能助手面板。 alt text alt text

核心功能

1. AI 对话

在聊天输入框中输入问题,发送后 AI 将返回智能回复。支持多轮对话上下文。

2. Cell 代码获取

  • 在 Notebook 中选中一个或多个 Cell
  • 点击 "Get Selected Code" 按钮
  • AI 将自动获取选中 Cell 的代码内容作为对话上下文

3. 代码插入

  • AI 生成的代码块会显示 "Insert Code" 按钮
  • 点击即可将代码插入到当前选中的 Cell 位置

4. Cell 执行

  • 支持执行指定的 Cell
  • 可获取 Cell 执行结果并显示在对话面板中

快捷键

快捷键 功能
Ctrl+Shift+A 打开/关闭 AI 助手面板

🏗️ 技术架构

┌─────────────────────────────────────────────────────────────┐
│                    JupyterLab Frontend                       │
│  ┌──────────────────┐    ┌──────────────────────────────┐  │
│  │   Notebook Panel │    │   AI Assistant Sidebar       │  │
│  │   (左侧工作区)    │◄──►│   (右侧智能助手)              │  │
│  │                  │    │   - 对话界面                  │  │
│  │   - Cells        │    │   - Cell 操作按钮             │  │
│  │   - 代码编辑     │    │   - AI 响应显示               │  │
│  └──────────────────┘    └──────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘
                              │
                              │ HTTP API
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                    AI Service                                │
│  ┌──────────────────────────────────────────────────────┐  │
│  │   AI Provider (OpenAI / Claude / 本地模型)            │  │
│  └──────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘

📁 项目结构

jupyterlab-ai-assistant/
├── src/                          # TypeScript 前端源码
│   ├── index.ts                  # 插件入口
│   ├── assistantPanel.ts         # 智能助手面板组件
│   ├── chatWidget.ts             # 对话界面组件
│   ├── cellOperations.ts         # Cell 操作工具函数
│   ├── aiService.ts              # AI API 调用服务
│   ├── markdownRenderer.ts       # Markdown 渲染组件
│   └── protocol/                 # 协议定义
│       ├── types.ts              # 类型定义
│       ├── contextCollector.ts   # 上下文收集器
│       └── actionExecutor.ts     # 动作执行器
├── style/                        # CSS 样式
│   ├── index.css                 # 主样式文件
│   └── variables.css             # CSS 变量定义
├── schema/                       # 插件设置 schema
│   └── plugin.json               # 设置定义
├── jupyterlab_ai_assistant/      # Python 后端包
│   ├── __init__.py               # 包入口
│   └── handlers.py               # API 处理器
├── pyproject.toml                # Python 项目配置
├── package.json                  # npm 包配置
├── tsconfig.json                 # TypeScript 配置
└── README.md                     # 项目文档

⚙️ 配置

在 JupyterLab 的设置编辑器中配置 AI Assistant:

{
  "jupyterlab_ai_assistant:plugin": {
    "apiKey": "your-api-key",
    "apiEndpoint": "https://api.openai.com/v1/chat/completions",
    "model": "gpt-3.5-turbo",
    "temperature": 0.7,
    "maxTokens": 2048
  }
}

🧪 测试

# 运行单元测试
npm test

# 运行 UI 测试
cd ui-tests
playwright test

🛠️ 开发

构建命令

# 构建生产版本
npm run build:prod

# 仅构建前端库
npm run build:lib

# 清理构建缓存
npm run clean:all

# 代码检查
npm run lint

代码规范

  • 遵循 JupyterLab 代码规范
  • TypeScript 严格模式
  • ESLint + Prettier 代码格式化
  • 详细的代码注释

📝 开发计划

  • 右侧边栏面板基础框架
  • AI 对话功能
  • Cell 代码获取
  • 代码插入功能
  • Cell 执行功能
  • 代码补全建议
  • 变量查看器
  • 执行历史记录
  • 多种 AI 模型选择

🤝 贡献

欢迎提交 Issue 和 Pull Request!

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 开启 Pull Request

📄 许可证

BSD-3-Clause License

👥 作者

AI-Assistant-Team

🔗 相关链接

🙏 致谢

感谢以下开源项目:

About

一个功能强大的 JupyterLab 智能助手插件,在右侧边栏提供 AI 对话面板,并支持与左侧工作区的 Notebook Cell 无缝联动。

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages