Skip to main content

Fast and easy wrapper around LLMs.

Project description

FastLLM

Fast and simple wrapper around LLMs. The package aims to be simply, precise and allows for fast prototyping of agents and applications around LLMs. At the moment focus around OpenAI's chat models.

Warning - experimental package and subject to change. For features and plans see the roadmap.

Samples

Require an openai api key in OPENAI_API_KEY environment variable or .env file.

export OPENAI_API_KEY=...

Agents

from fastllm import Agent

find_cities = Agent("List {{ n }} cities comma separated in {{ country }}.")
cities = find_cities(n=3, country="Austria").split(",")

print(cities)
['Vienna', 'Salzburg', 'Graz']
from fastllm import Agent, Message, Model, Prompt, Role

s = ";"

creative_name_finder = Agent(
    Message("You are an expert name finder.", Role.SYSTEM),
    Prompt("Find {{ n }} names.", temperature=2.0),
    Prompt("Print names {{ s }} separated, nothing else!"),
    model=Model("gpt-4"),
)

names = creative_name_finder(n=3, s=s).split(s)

print(names)
['Ethan Gallagher, Samantha Cheng, Max Thompson']

Functions

Functions can be added to Agents, Models or Prompts. Either as initial arguments or as decorator. Functions type hints, documentation and name are inferred from the function and added to the model call.

from typing import Literal

from fastllm import Agent, Prompt

calculator_agent = Agent(
    Prompt("Calculate the result for task: {{ task }}"),
    Prompt("Only give the result number as result without anything else!"),
)


@calculator_agent.function
def calculator(a, b, operator: Literal["+", "-", "*", "/"]):
    """A basic calculator using various operators."""

    match operator:
        case "+":
            return a + b
        case "-":
            return a - b
        case "*":
            return a * b
        case "/":
            return a / b
        case _:
            raise ValueError(f"Unknown operator {operator}")


result = calculator_agent(task="give the final result for (11 + 14) * (6 - 2)")

print(result)

another_result = calculator_agent(
    task="if I have 114 apples and 3 children, how many apples will each child get?"
)

print(another_result)
100
38

Roadmap

Features

  • Prompts using jinja2 templates
  • LLM calling with backoff and retry
  • Able to register functions to agents, models and prompts using decorators
  • Possible to register functions on multiple levels (agent, model, prompt). The function call is only available on the level it was registered.
  • Conversation history. The Model class keeps track of the conversation history.
  • Function schema is inferred from python function type hints, documentation and name
  • Function calling is handled by the Model class itself. Meaning if a LLM response indicate a function call, the Model class will call the function and return the result back to the LLM
  • Function calling can result in an infinite loop if LLM can not provide function name or arguments properly. This needs to be handled by the Model class.
  • Prompts with pattern using logit bias to guide LLM completion.
  • Handling of multiple response messages from LLMs in a single call. At the moment only the first response is kept.
  • Supporting non chat based LLMs (e.g. OpenAI's completion LLMs).
  • Supporting other LLMs over APIs except OpenAI's. (e.g. google, anthropics, etc.)
  • Supporting local LLMs (e.g. llama-1, llama-2, mpt, etc.)

Package

  • Basic package structure and functionality
  • Test cases and high test coverage
  • Tests against multiple python versions
  • 100% test coverage (at the moment around 90%)
  • Better documentation including readthedocs site.
  • Better error handling and logging
  • Better samples using jupyter notebooks
  • Set up of pre-commit
  • CI using github actions
  • Prober release and versioning

Development

Using poetry.

poetry install

Tests

poetry run pytest

Project details


Download files

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

Source Distribution

fastllm-0.1.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

fastllm-0.1.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file fastllm-0.1.1.tar.gz.

File metadata

  • Download URL: fastllm-0.1.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.19.0-46-generic

File hashes

Hashes for fastllm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3a50cb7fc851b7df6a1950fb086a7210fb87e528c016e9c01e4216325deec062
MD5 908f29147225a4b31cdab5a04c623049
BLAKE2b-256 53af88967521f323c9b63130155f42a06994d6ef8323183dc14c71cefbe48fdb

See more details on using hashes here.

File details

Details for the file fastllm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fastllm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.19.0-46-generic

File hashes

Hashes for fastllm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5d54c4e2c526256234cba5fdaf5cd6ea17f9719a5c3a0034fce9a5384486480f
MD5 bad9ddce345d80baa5ab5c510349282c
BLAKE2b-256 3c7f7a9ad8f6dfa5f13128fee5a40b2ca36839a77ecd71e2c5160b023eacc865

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