Skip to main content

A Pythonic interface to LLMs.

Project description

LLaMaBot: A Pythonic bot interface to LLMs

LLaMaBot implements a Pythonic interface to LLMs, making it much easier to experiment with LLMs in a Jupyter notebook and build simple utility apps that utilize LLMs. The model that we default to using is OpenAI's largest GPT-4 model (via an API key); local models through Ollama are also supported.

Install LLaMaBot

To install LLaMaBot:

pip install llamabot

Get access to LLMs

Option 1: Using local models with Ollama

LlamaBot supports using local models through Ollama. To do so, head over to the Ollama website and install Ollama. Then follow the instructions below.

Option 2: Use the OpenAI API

Obtain an OpenAI API key, then configure LlamaBot to use the API key by running:

llamabot configure

How to use

SimpleBot

The simplest use case of LLaMaBot is to create a SimpleBot that keeps no record of chat history. This is effectively the same as a stateless function that you program with natural language instructions rather than code. This is useful for prompt experimentation, or for creating simple bots that are preconditioned on an instruction to handle texts and are then called upon repeatedly with different texts. For example, to create a Bot that explains a given chunk of text like Richard Feynman would:

from llamabot import SimpleBot

feynman = SimpleBot("You are Richard Feynman. You will be given a difficult concept, and your task is to explain it back.")

Now, feynman is callable on any arbitrary chunk of text and will return a rephrasing of that text in Richard Feynman's style (or more accurately, according to the style prescribed by the prompt). For example:

feynman("Enzyme function annotation is a fundamental challenge, and numerous computational tools have been developed. However, most of these tools cannot accurately predict functional annotations, such as enzyme commission (EC) number, for less-studied proteins or those with previously uncharacterized functions or multiple activities. We present a machine learning algorithm named CLEAN (contrastive learning–enabled enzyme annotation) to assign EC numbers to enzymes with better accuracy, reliability, and sensitivity compared with the state-of-the-art tool BLASTp. The contrastive learning framework empowers CLEAN to confidently (i) annotate understudied enzymes, (ii) correct mislabeled enzymes, and (iii) identify promiscuous enzymes with two or more EC numbers—functions that we demonstrate by systematic in silico and in vitro experiments. We anticipate that this tool will be widely used for predicting the functions of uncharacterized enzymes, thereby advancing many fields, such as genomics, synthetic biology, and biocatalysis.")

This will return something that looks like:

Alright, let's break this down.

Enzymes are like little biological machines that help speed up chemical reactions in our
bodies. Each enzyme has a specific job, or function, and we use something called an
Enzyme Commission (EC) number to categorize these functions.

Now, the problem is that we don't always know what function an enzyme has, especially if
it's a less-studied or new enzyme. This is where computational tools come in. They try
to predict the function of these enzymes, but they often struggle to do so accurately.

So, the folks here have developed a new tool called CLEAN, which stands for contrastive
learning–enabled enzyme annotation. This tool uses a machine learning algorithm, which
is a type of artificial intelligence that learns from data to make predictions or
decisions.

CLEAN uses a method called contrastive learning. Imagine you have a bunch of pictures of
cats and dogs, and you want to teach a machine to tell the difference. You'd show it
pairs of pictures, some of the same animal (two cats or two dogs) and some of different
animals (a cat and a dog). The machine would learn to tell the difference by contrasting
the features of the two pictures. That's the basic idea behind contrastive learning.

CLEAN uses this method to predict the EC numbers of enzymes more accurately than
previous tools. It can confidently annotate understudied enzymes, correct mislabeled
enzymes, and even identify enzymes that have more than one function.

The creators of CLEAN have tested it with both computer simulations and lab experiments,
and they believe it will be a valuable tool for predicting the functions of unknown
enzymes. This could have big implications for fields like genomics, synthetic biology,
and biocatalysis, which all rely on understanding how enzymes work.

LlamaBot defaults to using the OpenAI API for convenience. However, if you'd like to use an Ollama local model instead:

from llamabot import SimpleBot
bot = SimpleBot(
    "You are Richard Feynman. You will be given a difficult concept, and your task is to explain it back.",
    model_name="llama2:13b"
)

Simply specify the model_name keyword argument and provide a model name from the Ollama library of models. (The same can be done for the ChatBot and QueryBot classes below!)

Chat Bot

To experiment with a Chat Bot in the Jupyter Notebook, we also provide the ChatBot interface. This interface automagically keeps track of chat history for as long as your Jupyter session is alive. Doing so allows you to use your own local Jupyter Notebook as a chat interface.

For example:

from llamabot import ChatBot

feynman = ChatBot("You are Richard Feynman. You will be given a difficult concept, and your task is to explain it back.")
feynman("Enzyme function annotation is a fundamental challenge, and numerous computational tools have been developed. However, most of these tools cannot accurately predict functional annotations, such as enzyme commission (EC) number, for less-studied proteins or those with previously uncharacterized functions or multiple activities. We present a machine learning algorithm named CLEAN (contrastive learning–enabled enzyme annotation) to assign EC numbers to enzymes with better accuracy, reliability, and sensitivity compared with the state-of-the-art tool BLASTp. The contrastive learning framework empowers CLEAN to confidently (i) annotate understudied enzymes, (ii) correct mislabeled enzymes, and (iii) identify promiscuous enzymes with two or more EC numbers—functions that we demonstrate by systematic in silico and in vitro experiments. We anticipate that this tool will be widely used for predicting the functions of uncharacterized enzymes, thereby advancing many fields, such as genomics, synthetic biology, and biocatalysis.")

With the chat history available, you can ask a follow-up question:

feynman("Is there a simpler way to rephrase the text?")

And your bot will work with the chat history to respond.

QueryBot

The final bot provided is a QueryBot. This bot lets you query a collection of documents. To use it, you have two options:

  1. Pass in a list of paths to text files, or
  2. Pass in a pre-computed GPTSimpleIndex from LlamaIndex.

As an illustrative example:

from llamabot import QueryBot
from pathlib import Path

blog_index = Path("/path/to/index.json")
bot = QueryBot(system_message="You are a Q&A bot.", saved_index_path=blog_index)
result = bot("Do you have any advice for me on career development?", similarity_top_k=5)
display(Markdown(result.response))

ImageBot

With the release of the OpenAI API updates, as long as you have an OpenAI API key, you can generate images with LlamaBot:

from llamabot import ImageBot

bot = ImageBot()
# Within a Jupyter notebook:
url = bot("A painting of a dog.")

# Or within a Python script
filepath = bot("A painting of a dog.")

# Now, you can do whatever you need with the url or file path.

If you're in a Jupyter Notebook, you'll see the image show up magically as part of the output cell as well.

CLI Demos

Llamabot comes with CLI demos of what can be built with it and a bit of supporting code.

Here is one where I expose a chatbot directly at the command line using llamabot chat:

<script async id="asciicast-594332" src="https://asciinema.org/a/594332.js"></script>

And here is another one where llamabot is used as part of the backend of a CLI app to chat with one's Zotero library using llamabot zotero chat:

<script async id="asciicast-594326" src="https://asciinema.org/a/594326.js"></script>

And finally, here is one where I use llamabot's SimpleBot to create a bot that automatically writes commit messages for me.

<script async id="asciicast-594334" src="https://asciinema.org/a/594334.js"></script>

Contributing

New features

New features are welcome! These are early and exciting days for users of large language models. Our development goals are to keep the project as simple as possible. Features requests that come with a pull request will be prioritized; the simpler the implementation of a feature (in terms of maintenance burden), the more likely it will be approved.

Bug reports

Please submit a bug report using the issue tracker.

Questions/Discussions

Please use the issue tracker on GitHub.

Contributors

Eric Ma
Eric Ma

💻 📖

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llamabot-0.1.0.tar.gz (51.2 kB view details)

Uploaded Source

Built Distribution

llamabot-0.1.0-py3-none-any.whl (56.7 kB view details)

Uploaded Python 3

File details

Details for the file llamabot-0.1.0.tar.gz.

File metadata

  • Download URL: llamabot-0.1.0.tar.gz
  • Upload date:
  • Size: 51.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for llamabot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2c444c9afc2a1047c78ebe2c9914f051439b56d61aced6529c21a19c4d9f85e3
MD5 709b4ada595c8c830214563840bf8f7d
BLAKE2b-256 b180e6998998d960cb971290f76c9121cd67436f5558c17500101562661d102f

See more details on using hashes here.

File details

Details for the file llamabot-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llamabot-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 56.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for llamabot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a9589cb47764e6b14d1b2be8852490edc955d998484a911df5159ae57671b7f
MD5 3ccfa492a991712084538bbe46ce1923
BLAKE2b-256 a7c6a0124ac1d608aae9b1fcd65c8bbaeb2a84970bb081c8cc8d2f6e52ccfe20

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page