Skip to main content

nbwrite

Note: This is an experimental use case for LLMs, the output may at times be unhelpful or inappropriate

nbwrite is a CLI tool which generates notebook-based Python examples using LLMs

Potential use cases include:

  1. You are writing a Python package and you want to produce executable tutorials for your stakeholders
  2. You are using a Python package and you want to generate a kick-start guide
  3. You want to generate regression tests for a python package

Features

  • Converts a set of steps and a task description into an executable Python notebook
  • Configurable OpenAI API parameters
  • Generate notebooks based on your own code using retrieval augmented generation

Getting Started

1. Install via any Python package manager

pip install nbwrite

2. Setup your OpenAI API Access

You will need to create an account and potentially buy credits via https://platform.openai.com/

export OPENAI_API_KEY='sk-xxxx'

3. Create a spec file for your generation job

e.g. nbwrite/example1.yaml:

task: |
  Plot the iris dataset using pandas
generation:
  count: 2

4. Generate some notebooks

nbwrite ./nbwrite/example1.yaml

Your outputs will be in your current directory

Guides

Generate guides for my closed-source code

You will need to install the 'rag' extra pip install 'nbwrite[rag]'

By default, OpenAI's models can generate docs based on parametric knowledge. This is limited to popular open source libraries.

The packages input in the spec file can be used to reference Python packages in your current environment, which will be indexed in a local Vector DB. Code relevant to the task is then stuffed into the prompt.

You can pass in an arbitrary number of packages, just remember that the code will be sent to OpenAI to create embeddings, and this costs money.

example:

packages:
  - my_internal_pkg
  - another.internal.pkg

Customise the OpenAI parameters

You can modify both the system prompt and the llm args to try out different OpenAI models, temperatures, etc. See Langchain's API ref

Note! This is a confusing use case -- change it to something relevant to your work.

task: |
  Create a hello world notebook 'x.ipynb', use nbmake's NotebookRun class to test it from a Python application
steps:
  - Create a hello world notebook using nbformat
  - Use nbmake's NotebookRun class to execute it from a Python application
  - Check the output notebook printed what we were expecting
packages:
  - nbmake
  - nbformat
  - nbclient
generation:
  count: 2 # number of notebooks to generate
  # system_prompt:
  llm_kwargs:
    # https://api.python.langchain.com/en/latest/llms/langchain.llms.openai.BaseOpenAI.html#langchain.llms.openai.BaseOpenAI
    model_name: gpt-3.5-turbo # The API name of the model as per https://platform.openai.com/docs/models
    temperature: 0.5
  retriever_kwargs:
    k: 3
    search_type: similarity

FAQs and Troubleshooting

How much does this cost

It depends on (a) the model you use and other params such as context length, (b) the number of outputs you generate.

See OpenAI usage here https://platform.openai.com/account/usage

Debugging with Phoenix

This is an Alpha stage product, and we encourage you to investigate and report bugs

You will need to install the 'tracing' extra pip install 'nbwrite[tracing]'

For any errors occurring during the main generation process, it's possible to view traces using Phoenix.

  1. Start Phoenix with this script

    #! /usr/bin/env python
    
    import phoenix
    phoenix.launch_app()
    
    input("Press any key to exit...")
    
  2. In another termianl, run nbwrite with the following var set: export NBWRITE_PHOENIX_TRACE=1

  3. Check the phoenix traces in the dashboard (default http://127.0.0.1:6060/)

Metadata

Release files for nbwrite 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nbwrite 0.2
File Size Uploaded
nbwrite-0.2.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nbwrite 0.2
File Interpreter ABI Platform
nbwrite-0.2-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / nbwrite-0.2.tar.gz

Download URL nbwrite-0.2.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
09bd87aea1bdab1aadc78dd4f42d06a746527110e339349995525e74eeef3013
BLAKE2b-256 checksum
How to use checksums
7c8fe54bcb27f1094aea122e02b2ce12a92a9afcbd34bc5911b6341dffad1d9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.6

Release files / nbwrite-0.2-py3-none-any.whl

Download URL nbwrite-0.2-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27d922518527533795bd95f645a73bd2c7b3771e6118df329de391ee21cba065
BLAKE2b-256 checksum
How to use checksums
2e488dc2ce8b400c4f729422fb8ad19bceab865963c44c86a078f95f4d0ddc75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.6

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files

0.1

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page