
Haverscript is a lightweight Python library designed to manage LLM interactions, built on top of Ollama, and its Python API. Haverscript streamlines LLM interactions by focusing on immutability, automating retries, and utilizing SQLite caching. This ensures efficient, reliable, and repeatable outcomes while reducing the complexity of managing LLM workflows.
First Example
Here’s a basic example demonstrating how to use Haverscript, with the mistral model.
from haverscript import connect
session = connect("mistral").echo()
session = session.chat("In one sentence, why is the sky blue?")
session = session.chat("Rewrite the above sentence in the style of Yoda")
session = session.chat("How many questions did I ask?")
This will give the following output.
> In one sentence, why is the sky blue?
The sky appears blue due to scattering of shorter wavelengths (blue and violet)
more than other colors by the atmosphere when sunlight enters it.
> Rewrite the above sentence in the style of Yoda
In the atmosphere, scattering of blue and violet light, more abundant, is.
This explains why sky looks blue to our eyes.
> How many questions did I ask?
You asked three questions in total: one about the reason for the blue color of the
sky, another asking me to rewrite that answer in the style of Yoda, and a third
confirming how many questions you had asked.
Installing Haverscript
Haverscript is available on GitHub: https://github.com/andygill/haverscript. While it is currently in alpha and considered experimental, it is ready to use out of the box. You need to have Ollama already installed, or have access to an an Ollama compatible API end-point. You can install Haverscript directly from the GitHub repository using pip.
Here's how to set up Haverscript:
- First, create and activate a Python virtual environment if you haven’t already:
python3 -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
- Install Haverscript directly from the GitHub repository:
pip install git+https://github.com/andygill/haverscript.git@v0.1.0
- Run Haverscript. Here is a example running in the REPL.
% python
Python 3.12.5 (main, Aug 6 2024, 19:08:49) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from haverscript import connect
>>> session = connect("mistral").echo()
>>> session = session.chat("In one sentence, why is the sky blue?")
> In one sentence, why is the sky blue?
The sky appears blue due to a process called Rayleigh scattering, where
shorter-wavelength light (blue and violet) is scattered more by the molecules
in Earth's atmosphere, making these colors visible to our eyes while the sun
is overhead.
>>> ...
Here is the same interactions, without echo().
>>> from haverscript import connect
>>> session = connect("mistral")
>>> session = session.chat("In one sentence, why is the sky blue?")
>>> print(str(session))
The sky appears blue during a clear day due to a scattering effect called Rayleigh scattering where shorter-wavelength light (blue and violet) is scattered more effectively by the molecules in Earth's atmosphere than longer-wavelength light (red, yellow, and green). This scattered blue light reaches our eyes from all directions, giving us the perception of a blue sky.
Haverscript is built for scripting interactions and is typically not used interactively - Ollama has a CLI tool for this already. Haverscript goes beyond the CLI tool, and provided support for scripting nested chat sessions, reply, caching using SQL, and other basic plumbing to help connect LLMs together. Good luck, and happy Havering!