Skip to main content

Directly Connecting Python to LLMs - Dataclasses & Interfaces <-> LLMs

Project description

llm-strategy

Release Build status codecov Commit activity License

Implementing the Strategy Pattern using LLMs.

Also, please see https://blog.blackhc.net/2022/12/llm_software_engineering/ for a wider perspective on why this could be important in the future.

This package adds a decorator llm_strategy that connects to an LLM (such as OpenAI’s GPT-3) and uses the LLM to "implement" abstract methods in interface classes. It does this by forwarding requests to the LLM and converting the responses back to Python data using Python's @dataclasses.

It uses the doc strings, type annotations, and method/function names as prompts for the LLM, and can automatically convert the results back into Python types (currently only supporting @dataclasses). It can also extract a data schema to send to the LLM for interpretation. While the llm-strategy package still relies on some Python code, it has the potential to reduce the need for this code in the future by using additional, cheaper LLMs to automate the parsing of structured data.

Example

from dataclasses import dataclass
from llm_strategy import llm_strategy
from langchain.llms import OpenAI


@llm_strategy(OpenAI(max_tokens=256))
@dataclass
class Customer:
    key: str
    first_name: str
    last_name: str
    birthdate: str
    address: str

    @property
    def age(self) -> int:
        """Return the current age of the customer.

        This is a computed property based on `birthdate` and the current year (2022).
        """

        raise NotImplementedError()


@dataclass
class CustomerDatabase:
    customers: list[Customer]

    def find_customer_key(self, query: str) -> list[str]:
        """Find the keys of the customers that match a natural language query best (sorted by closeness to the match).

        We support semantic queries instead of SQL, so we can search for things like
        "the customer that was born in 1990".

        Args:
            query: Natural language query

        Returns:
            The index of the best matching customer in the database.
        """
        raise NotImplementedError()

    def load(self):
        """Load the customer database from a file."""
        raise NotImplementedError()

    def store(self):
        """Store the customer database to a file."""
        raise NotImplementedError()


@llm_strategy(OpenAI(max_tokens=1024))
@dataclass
class MockCustomerDatabase(CustomerDatabase):
    def load(self):
        self.customers = self.create_mock_customers(10)

    def store(self):
        pass

    @staticmethod
    def create_mock_customers(num_customers: int = 1) -> list[Customer]:
        """
        Create mock customers with believable data (our customers are world citizens).
        """
        raise NotImplementedError()

See examples/customer_database_search.py for a full example.

Customer Database Viewer

Searching for a Customer

Searching for a Customer

Getting started with contributing

Clone the repository first. Then, install the environment and the pre-commit hooks with

make install

The CI/CD pipeline will be triggered when you open a pull request, merge to main, or when you create a new release.

To finalize the set-up for publishing to PyPi or Artifactory, see here. For activating the automatic documentation with MkDocs, see here. To enable the code coverage reports, see here.

Releasing a new version

  • Create an API Token on Pypi.
  • Add the API Token to your projects secrets with the name PYPI_TOKEN by visiting this page.
  • Create a new release on Github. Create a new tag in the form *.*.*.

For more details, see here.


Repository initiated with fpgmaas/cookiecutter-poetry.

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

llm_strategy-1.1.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

llm_strategy-1.1.0-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

Details for the file llm_strategy-1.1.0.tar.gz.

File metadata

  • Download URL: llm_strategy-1.1.0.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.8 Linux/5.19.0-50-generic

File hashes

Hashes for llm_strategy-1.1.0.tar.gz
Algorithm Hash digest
SHA256 716f8a19788e956b969f98ddfd08fbb3de0c632426fba94cf61ac73201cc862d
MD5 47b91b1cb3851354b2954e3ba4eb34be
BLAKE2b-256 fab6b5fd90a5a0969e53303e37498f2e4fab05462e12207e93206f5722f10eb4

See more details on using hashes here.

File details

Details for the file llm_strategy-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: llm_strategy-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.8 Linux/5.19.0-50-generic

File hashes

Hashes for llm_strategy-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c2a8a22fe798f46b72fb0e788e79b7e43fb1e392c96f4f2ce41dc7cc50c7e2c
MD5 1b1bbcbd8b3f512780721977b82c5a7a
BLAKE2b-256 4141f0bc459a1067131869f8ae07088a0154a35bc169ef986a064c52e59e172a

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