Skip to content

Commit

Permalink
docs: notebook linting (#14366)
Browse files Browse the repository at this point in the history
Many jupyter notebooks didn't pass linting. List of these files are
presented in the [tool.ruff.lint.per-file-ignores] section of the
pyproject.toml . Addressed these bugs:
- fixed bugs; added missed imports; updated pyproject.toml
 Only the `document_loaders/tensorflow_datasets.ipyn`,
`cookbook/gymnasium_agent_simulation.ipynb` are not completely fixed.
I'm not sure about imports.

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
  • Loading branch information
leo-gan and efriis authored Dec 7, 2023
1 parent 52052cc commit 18aba7f
Show file tree
Hide file tree
Showing 38 changed files with 244 additions and 142 deletions.
4 changes: 2 additions & 2 deletions cookbook/multi_modal_output_agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"source": [
"import re\n",
"\n",
"from IPython.display import Image\n",
"from IPython.display import Image, display\n",
"from steamship import Block, Steamship"
]
},
Expand Down Expand Up @@ -180,7 +180,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
20 changes: 15 additions & 5 deletions cookbook/qianfan_baidu_elasticesearch_RAG.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"source": [
"#!pip install qianfan\n",
"#!pip install bce-python-sdk\n",
"#!pip install elasticsearch == 7.11.0"
"#!pip install elasticsearch == 7.11.0\n",
"#!pip install sentence-transformers"
]
},
{
Expand All @@ -54,8 +55,10 @@
"metadata": {},
"outputs": [],
"source": [
"import sentence_transformers\n",
"from baidubce.auth.bce_credentials import BceCredentials\n",
"from baidubce.bce_client_configuration import BceClientConfiguration\n",
"from langchain.chains.retrieval_qa import RetrievalQA\n",
"from langchain.document_loaders.baiducloud_bos_directory import BaiduBOSDirectoryLoader\n",
"from langchain.embeddings.huggingface import HuggingFaceEmbeddings\n",
"from langchain.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint\n",
Expand Down Expand Up @@ -161,21 +164,28 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"version": "3.9.17"
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
6 changes: 3 additions & 3 deletions cookbook/retrieval_in_sql.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"from tqdm import tqdm\n",
"\n",
"for i in tqdm(range(len(title_embeddings))):\n",
" title = titles[i].replace(\"'\", \"''\")\n",
" title = song_titles[i].replace(\"'\", \"''\")\n",
" embedding = title_embeddings[i]\n",
" sql_command = (\n",
" f'UPDATE \"Track\" SET \"embeddings\" = ARRAY{embedding} WHERE \"Name\" ='\n",
Expand Down Expand Up @@ -681,9 +681,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion cookbook/wikibase_agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
" for key in path:\n",
" try:\n",
" current = current[key]\n",
" except:\n",
" except KeyError:\n",
" return None\n",
" return current\n",
"\n",
Expand Down
12 changes: 6 additions & 6 deletions docs/docs/expression_language/how_to/fallbacks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n",
" try:\n",
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
" except RateLimitError:\n",
" print(\"Hit error\")"
]
},
Expand All @@ -114,7 +114,7 @@
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n",
" try:\n",
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
" except RateLimitError:\n",
" print(\"Hit error\")"
]
},
Expand Down Expand Up @@ -156,7 +156,7 @@
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n",
" try:\n",
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
" except:\n",
" except RateLimitError:\n",
" print(\"Hit error\")"
]
},
Expand Down Expand Up @@ -190,10 +190,10 @@
")\n",
"\n",
"chain = prompt | llm\n",
"with patch(\"openai.ChatCompletion.create\", side_effect=error):\n",
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n",
" try:\n",
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
" except:\n",
" except RateLimitError:\n",
" print(\"Hit error\")"
]
},
Expand Down Expand Up @@ -291,7 +291,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/guides/fallbacks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n",
" try:\n",
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
" except RateLimitError:\n",
" print(\"Hit error\")"
]
},
Expand All @@ -116,7 +116,7 @@
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n",
" try:\n",
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
" except RateLimitError:\n",
" print(\"Hit error\")"
]
},
Expand Down Expand Up @@ -158,7 +158,7 @@
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n",
" try:\n",
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
" except:\n",
" except RateLimitError:\n",
" print(\"Hit error\")"
]
},
Expand Down
25 changes: 15 additions & 10 deletions docs/docs/integrations/chat/google_vertex_ai_palm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -64,7 +64,7 @@
"AIMessage(content=\" J'aime la programmation.\")"
]
},
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -98,7 +98,7 @@
"AIMessage(content=' プログラミングが大好きです')"
]
},
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -142,7 +142,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"tags": []
},
Expand Down Expand Up @@ -198,7 +198,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -212,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -221,12 +221,17 @@
"AIMessage(content=' Why do you love programming?')"
]
},
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"system = (\n",
" \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n",
")\n",
"human = \"{text}\"\n",
"prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
"chain = prompt | chat\n",
"\n",
"asyncio.run(\n",
Expand All @@ -251,7 +256,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down
12 changes: 4 additions & 8 deletions docs/docs/integrations/chat/promptlayer_chatopenai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@
"**The above request should now appear on your [PromptLayer dashboard](https://www.promptlayer.com).**"
]
},
{
"cell_type": "markdown",
"id": "05e9e2fe",
"metadata": {},
"source": []
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -152,6 +146,8 @@
"metadata": {},
"outputs": [],
"source": [
"import promptlayer\n",
"\n",
"chat = PromptLayerChatOpenAI(return_pl_id=True)\n",
"chat_results = chat.generate([[HumanMessage(content=\"I am a cat and I want\")]])\n",
"\n",
Expand All @@ -172,7 +168,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "base",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -186,7 +182,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8 (default, Apr 13 2021, 12:59:45) \n[Clang 10.0.0 ]"
"version": "3.10.12"
},
"vscode": {
"interpreter": {
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/integrations/chat_loaders/imessage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"source": [
"# Now all of the Tortoise's messages will take the AI message class\n",
"# which maps to the 'assistant' role in OpenAI's training format\n",
"alternating_sessions[0][\"messages\"][:3]"
"chat_sessions[0][\"messages\"][:3]"
]
},
{
Expand Down Expand Up @@ -191,7 +191,7 @@
}
],
"source": [
"training_data = convert_messages_for_finetuning(alternating_sessions)\n",
"training_data = convert_messages_for_finetuning(chat_sessions)\n",
"print(f\"Prepared {len(training_data)} dialogues for training\")"
]
},
Expand Down Expand Up @@ -416,7 +416,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
16 changes: 13 additions & 3 deletions docs/docs/integrations/document_loaders/arcgis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@
"source": [
"from langchain.document_loaders import ArcGISLoader\n",
"\n",
"url = \"https://maps1.vcgov.org/arcgis/rest/services/Beaches/MapServer/7\"\n",
"loader = ArcGISLoader(url)"
"URL = \"https://maps1.vcgov.org/arcgis/rest/services/Beaches/MapServer/7\"\n",
"loader = ArcGISLoader(URL)\n",
"\n",
"docs = loader.load()"
]
},
{
"cell_type": "markdown",
"id": "1e174ebd-bbbd-4a66-a644-51e0df12982d",
"metadata": {},
"source": [
"Let's measure loader latency."
]
},
{
Expand Down Expand Up @@ -261,7 +271,7 @@
"metadata": {},
"outputs": [],
"source": [
"loader_geom = ArcGISLoader(url, return_geometry=True)"
"loader_geom = ArcGISLoader(URL, return_geometry=True)"
]
},
{
Expand Down
Loading

0 comments on commit 18aba7f

Please sign in to comment.