Quick Start

Build a tool-calling chat widget for notebooks using LangChain. The widget is an anywidget + React UI, so it plays nicely with other Jupyter widgets.

1) Install

pip install langchain-widget

If you don’t already have JupyterLab:

pip install jupyterlab

2) Configure your model

Set the API key for your provider. For OpenAI:

export OPENAI_API_KEY="..."

Tip: you can also use a local .env file and python-dotenv (the examples below call load_dotenv()):

OPENAI_API_KEY=...

3) Create a widget

[3]:
from langchain_widget import LangChainWidget
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv

load_dotenv()

chat_model = ChatOpenAI(model="gpt-4o-mini")

@tool
def add(a: int, b: int) -> int:
    "Add two integers."
    return a + b

w = LangChainWidget(
    chat_model=chat_model,
    tools=[add],
    system_prompt="You are a helpful assistant.",
)
w
[3]:

4) Pass context from your notebook

You can inject extra context from your analysis or other widgets (selection state, current parameters, computed results, etc.).

[4]:
w.add_context(title="Notebook context", content="Selected atoms: [0, 3, 7]")
w
[4]: