Skip to main content

reclm

When building AI based tooling and packaging we often call LLMs while prototyping and testing our code. A single LLM call can take 100’s of ms to run and the output isn’t deterministic. This can really slow down development especially if our notebook contains many LLM calls 😞.

While LLMs are new, working with external APIs in our code isn’t. Plenty of tooling already exists that make working with APIs much easier. For example, Python’s unittest mock object is commonly used to simulate or mock an API call so that it returns a hardcoded response. This works really well in the traditional Python development workflow and can make our tests fast and predictable.

However, it doesn’t work well in the nbdev workflow where oftentimes we’ll want to quickly run all cells in our notebook while we’re developing our code. While we can use mocks in our test cells we don’t want our exported code cells to be mocked. This leaves us with two choices:

  • we temporarily mock our exported code cells but undo the mocking before we export these cells.
  • we do nothing and just live with notebooks that take a long time to run.

Both options are pretty terrible as they pull us out of our flow state and slow down development 😞.

reclm builds on the underlying idea of mocks but adapts them to exploratory workflows. It initializes each sdk client (e.g. AsyncAnthropic) with a custom http_client. This http_client intercepts each LLM call, caches the response, and returns this cached response if the user makes the same LLM call again.

Usage

To use reclm

  • install the package: pip install git+https://github.com/AnswerDotAI/reclm.git
  • import the package from reclm.core import enable_reclm in each notebook
  • add enable_reclm() to the top of each notebook

Note: enable_reclm should be added after you import the OpenAI and/or Anthropic SDK.

Every LLM call you make using OpenAI/Anthropic will now be cached in reclm.json.

[!NOTE]

Cache Location

If you’re using reclm in an nbdev project the cache (i.e. reclm.json) will be added to your project’s root dir. For all other projects the cache will be added to the current working directory. You can set a custom location for the cache by passing cache_dir to enable_reclm (e.g. enable_reclm(cache_dir='/path/to/cache')).

Your Tests

nbdev_test will automatically read from the cache. However, if your notebooks contain LLM calls that haven’t been cached, nbdev_test will call the OpenAI/Anthropic APIs and then cache the responses.

Cleaning the cache

It is recommended that you clean the cache before committing it. This will remove any stale LLM requests you’ve accumulated during development.

To clean the cache, run update_reclm_cache from your project’s root directory.

Note: Your LLM request/response data is stored in a file called reclm.json. Depending on your setup, it might be stored in your project’s root dir or your current working directory.

Note: All request headers are removed so it is safe to include this file in your version control system (e.g. git). In fact, it is expected that you’ll include this file in your vcs. There is one scenario where your api key could end up in the cache. This occurs if you make an LLM call that includes your api key in the response such as an authentication error.

Reclm Tests

The reclm test suite is found in test_reclm.py. Use the command python test_reclm.py to run the test suite.

Each test checks a specific SDK endpoint (e.g. anthropic streaming). If reclm is working correctly the SDK endpoint won’t be called and instead the response will be pulled from reclm.json.

Release files for reclm 0.0.6

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

Source distribution (sdist)

Source distribution for reclm 0.0.6
File Size Uploaded
reclm-0.0.6.tar.gz 11.8 kB Details

Built distribution (wheel)

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

Total release size: 22.5 kB

Release files / reclm-0.0.6.tar.gz

Download URL reclm-0.0.6.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
32212d6dad8cd419446dff22b6bc39bd27d28b552c8d5b47bf6a685b656c1cad
BLAKE2b-256 checksum
How to use checksums
2b70c57e9fa277c142fc584e09780c0e9b31d4aa75e645137fcd3b7efd3c8bfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / reclm-0.0.6-py3-none-any.whl

Download URL reclm-0.0.6-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e667c66ee3636b0f96f96c1beea19c3636f376f113dfa8f5eb6e99ad8069ee5
BLAKE2b-256 checksum
How to use checksums
3cde711e6ac46eb94cb5338ed741bde8a9a355fb53bd544c540c5d7d639b2a40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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