Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LangChain] Improve Jupyter Notebooks and add software tests #97

Merged
merged 4 commits into from
Oct 30, 2023

Conversation

amotl
Copy link
Member

@amotl amotl commented Oct 29, 2023

About

This patch adds software tests to the examples around LangChain, in order to validate that the Jupyter Notebooks work well, and to keep them working / get alerted if something breaks.

Backlog

Details

This patch builds upon GH-96 and GH-85.

/cc @andnig, @marijaselakovic, @karynzv, @hlcianfagna, @hammerhead, @ckurze, @WalBeh

@amotl
Copy link
Member Author

amotl commented Oct 29, 2023

Problem

-DELETE OK, 30 rows affected (0.000 sec)
+DELETE OK, 0 rows affected (0.000 sec)

image

-- https://github.com/crate/cratedb-examples/actions/runs/6684017889/job/18160822060?pr=97#step:5:585

Source

"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001B[32mCONNECT OK\r\n",
"\u001B[0m\u001B[32mPSQL OK, 1 row affected (0.001 sec)\r\n",
"\u001B[0m\u001B[32mDELETE OK, 30 rows affected (0.010 sec)\r\n",
"\u001B[0m\u001B[32mINSERT OK, 30 rows affected (0.011 sec)\r\n",
"\u001B[0m\u001B[0m\u001B[32mCONNECT OK\r\n",
"\u001B[0m\u001B[32mREFRESH OK, 1 row affected (0.026 sec)\r\n",
"\u001B[0m\u001B[0m"
]
}
],
"source": [
"!rm -f mlb_teams_2012.sql\n",
"!wget --quiet https://github.com/crate-workbench/langchain/raw/cratedb/docs/docs/integrations/document_loaders/example_data/mlb_teams_2012.sql\n",
"\n",
"!crash --schema=notebook < mlb_teams_2012.sql;\n",
"!crash --schema=notebook --command \"REFRESH TABLE mlb_teams_2012;\""
]
},

@amotl amotl changed the title LangChain: Add software tests [LangChain] Add software tests Oct 29, 2023
@amotl
Copy link
Member Author

amotl commented Oct 29, 2023

-DELETE OK, 30 rows affected (0.000 sec)
+DELETE OK, 0 rows affected (0.000 sec)

To make the output more idempotent, the SQL DDL statements have been adjusted.

@amotl amotl force-pushed the amo/langchain-tests branch 3 times, most recently from 01813ea to 4116268 Compare October 29, 2023 15:43
@amotl amotl requested review from seut, matriv and ckurze October 29, 2023 15:45
@amotl
Copy link
Member Author

amotl commented Oct 29, 2023

Status Quo

It works. 🚀

collecting ... collected 3 items

test.py::test_notebook[document_loader.ipynb] PASSED
test.py::test_notebook[vector_search.ipynb] SKIPPED (OPENAI_API_KEY not given [skip-notebook])
test.py::test_notebook[conversational_memory.ipynb] PASSED

-- https://github.com/crate/cratedb-examples/actions/runs/6684315548/job/18161437538?pr=97#step:5:519

Outlook

test_notebook[vector_search.ipynb] will be addressed on behalf of a subsequent iteration.

@amotl amotl removed the request for review from ckurze October 29, 2023 15:49
@amotl amotl marked this pull request as ready for review October 29, 2023 15:50
@amotl amotl changed the title [LangChain] Add software tests [LangChain] Improve Jupyter Notebooks and add software tests Oct 29, 2023
@amotl amotl force-pushed the amo/langchain-tests branch 3 times, most recently from 54a08b6 to fb462c9 Compare October 29, 2023 22:07
Comment on lines +6 to +21
def monkeypatch_pytest_notebook_treat_cell_exit_as_notebook_skip():
"""
Patch `pytest-notebook`, in fact `nbclient.client.NotebookClient`,
to propagate cell-level `pytest.exit()` invocations as signals
to mark the whole notebook as skipped.

In order not to be too intrusive, the feature only skips notebooks
when being explicitly instructed, by adding `[skip-notebook]` at the
end of the `reason` string. Example:

import pytest
if "ACME_API_KEY" not in os.environ:
pytest.exit("ACME_API_KEY not given [skip-notebook]")

https://github.com/chrisjsewell/pytest-notebook/issues/43
"""
Copy link
Member Author

@amotl amotl Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a utility temporarily added here. Based on the outcome from chrisjsewell/pytest-notebook#43, it may be added to the upstream library, or to a different spot.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has been added to another library, to not clutter the canonical cratedb-examples repository with such utility code.

-- https://github.com/pyveci/pueblo/blob/v0.0.1/pueblo/testing/notebook.py

Comment on lines -164 to +169
"\tid = sa.Column(sa.BigInteger, primary_key=True, default=generate_autoincrement_identifier)\n",
"\tid = sa.Column(sa.BigInteger, primary_key=True, server_default=sa.func.now())\n",
Copy link
Member Author

@amotl amotl Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is so good to have this tested now. 🚀

Even if the Notebook worked once upon a time, it did not in its current form, because the previous implementation changed.

It is difficult to catch such errors without running rigid software testing, minor things like this easily slip through, effectively rendering the notebook unusable for all people trying to exercise it.

Comment on lines +223 to +224
"\t# Make sure to start with a blank canvas.\n",
"\tchat_message_history.clear()\n",
Copy link
Member Author

@amotl amotl Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idempotency flaws when testing. Fixed now.

Base automatically changed from amo/framework-langchain to main October 30, 2023 15:57
@amotl amotl merged commit 5f0f49c into main Oct 30, 2023
1 check passed
@amotl amotl deleted the amo/langchain-tests branch October 30, 2023 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant