Skip to content

The agent engineering platform.

docs: hide jsx in llm chain tutorial (#30187)

Really Himfbd2e107034e
diff --git a/docs/docs/tutorials/llm_chain.ipynb b/docs/docs/tutorials/llm_chain.ipynb
index 0b9547568b..690b51aadd 100644
--- a/docs/docs/tutorials/llm_chain.ipynb
+++ b/docs/docs/tutorials/llm_chain.ipynb
@@ -39,6 +39,7 @@
"\n",
"To install LangChain run:\n",
"\n",
+ "<!-- HIDE_IN_NB\n",
"import Tabs from '@theme/Tabs';\n",
"import TabItem from '@theme/TabItem';\n",
"import CodeBlock from \"@theme/CodeBlock\";\n",
@@ -51,9 +52,28 @@
" <CodeBlock language=\"bash\">conda install langchain -c conda-forge</CodeBlock>\n",
" </TabItem>\n",
"</Tabs>\n",
+ "HIDE_IN_NB -->"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "86874822",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# | output: false\n",
"\n",
- "\n",
- "\n",
+ "# %pip install langchain\n",
+ "# OR\n",
+ "# %conda install langchain -c conda-forge"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a546a5bc",
+ "metadata": {},
+ "source": [
"For more details, see our [Installation guide](/docs/how_to/installation).\n",
"\n",
"### LangSmith\n",
@@ -67,17 +87,45 @@
"```shell\n",
"export LANGSMITH_TRACING=\"true\"\n",
"export LANGSMITH_API_KEY=\"...\"\n",
+ "export LANGSMITH_PROJECT=\"default\" # or any other project name\n",
"```\n",
"\n",
- "Or, if in a notebook, you can set them with:\n",
- "\n",
- "```python\n",
+ "Or, if in a notebook, you can set them with:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "599bb688",
+ "metadata": {},
+ "outputs": [],
+ "source": [
"import getpass\n",
"import os\n",
"\n",
+ "try:\n",
+ " # load environment variables from .env file (requires `python-dotenv`)\n",
+ " from dotenv import load_dotenv\n",
+ "\n",
+ " load_dotenv()\n",
+ "except ImportError:\n",
+ " pass\n",
+ "\n",
"os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n",
- "os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass()\n",
- "```"
+ "if \"LANGSMITH_API_KEY\" not in os.environ:\n",
+ " os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\n",
+ " prompt=\"Enter your LangSmith API key (optional): \"\n",
+ " )\n",
+ "if \"LANGSMITH_PROJECT\" not in os.environ:\n",
+ " os.environ[\"LANGSMITH_PROJECT\"] = getpass.getpass(\n",
+ " prompt='Enter your LangSmith Project Name (default = \"default\"): '\n",
+ " )\n",
+ " if not os.environ.get(\"LANGSMITH_PROJECT\"):\n",
+ " os.environ[\"LANGSMITH_PROJECT\"] = \"default\"\n",
+ "if \"OPENAI_API_KEY\" not in os.environ:\n",
+ " os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\n",
+ " prompt=\"Enter your OpenAI API key (required if using OpenAI): \"\n",
+ " )"
]
},
{
@@ -89,9 +137,11 @@
"\n",
"First up, let's learn how to use a language model by itself. LangChain supports many different language models that you can use interchangeably. For details on getting started with a specific model, refer to [supported integrations](/docs/integrations/chat/).\n",
"\n",
+ "<!-- HIDE_IN_NB>\n",
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
"\n",
- "<ChatModelTabs overrideParams={{openai: {model: \"gpt-4o-mini\"}}} />\n"
+ "<ChatModelTabs overrideParams={{openai: {model: \"gpt-4o-mini\"}}} />\n",
+ "HIDE_IN_NB -->"
]
},
{
diff --git a/docs/scripts/notebook_convert.py b/docs/scripts/notebook_convert.py
index fb0e3c8075..33da9a5d30 100644
--- a/docs/scripts/notebook_convert.py
+++ b/docs/scripts/notebook_convert.py
@@ -9,9 +9,12 @@ import nbformat
from nbconvert.exporters import MarkdownExporter
from nbconvert.preprocessors import Preprocessor
+HIDE_IN_NB_MAGIC_OPEN = "<!-- HIDE_IN_NB"
+HIDE_IN_NB_MAGIC_CLOSE = "HIDE_IN_NB -->"
+
class EscapePreprocessor(Preprocessor):
- def preprocess_cell(self, cell, resources, cell_index):
+ def preprocess_cell(self, cell, resources, index):
if cell.cell_type == "markdown":
# rewrite .ipynb links to .md
cell.source = re.sub(
@@ -61,7 +64,7 @@ class ExtractAttachmentsPreprocessor(Preprocessor):
outputs are returned in the 'resources' dictionary.
"""
- def preprocess_cell(self, cell, resources, cell_index):
+ def preprocess_cell(self, cell, resources, index):
"""
Apply a transformation on each cell,
Parameters
@@ -117,11 +120,19 @@ class CustomRegexRemovePreprocessor(Preprocessor):
return nb, resources
+class UnHidePreprocessor(Preprocessor):
+ def preprocess_cell(self, cell, resources, index):
+ cell.source = cell.source.replace(HIDE_IN_NB_MAGIC_OPEN, "")
+ cell.source = cell.source.replace(HIDE_IN_NB_MAGIC_CLOSE, "")
+ return cell, resources
+
+
exporter = MarkdownExporter(
preprocessors=[
EscapePreprocessor,
ExtractAttachmentsPreprocessor,
CustomRegexRemovePreprocessor,
+ UnHidePreprocessor,
],
template_name="mdoutput",
extra_template_basedirs=["./scripts/notebook_convert_templates"],