Package: llm_strategy
llm-strategyImplementing the Strategy Pattern using LLMs.Also, please seehttps://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 decoratorllm_strategythat 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 thellm-strategypackage 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.Github repository:https://github.com/blackhc/llm-strategy/Documentationhttps://blackhc.github.io/llm-strategy/Examplefromdataclassesimportdataclassfromllm_strategyimportllm_strategyfromlangchain.llmsimportOpenAI@llm_strategy(OpenAI(max_tokens=256))@dataclassclassCustomer:key:strfirst_name:strlast_name:strbirthdate:straddress:str@propertydefage(self)->int:"""Return the current age of the customer.This is a computed property based on `birthdate` and the current year (2022)."""raiseNotImplementedError()@dataclassclassCustomerDatabase:customers:list[Customer]deffind_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 queryReturns:The index of the best matching customer in the database."""raiseNotImplementedError()defload(self):"""Load the customer database from a file."""raiseNotImplementedError()defstore(self):"""Store the customer database to a file."""raiseNotImplementedError()@llm_strategy(OpenAI(max_tokens=1024))@dataclassclassMockCustomerDatabase(CustomerDatabase):defload(self):self.customers=self.create_mock_customers(10)defstore(self):pass@staticmethoddefcreate_mock_customers(num_customers:int=1)->list[Customer]:"""Create mock customers with believable data (our customers are world citizens)."""raiseNotImplementedError()Seeexamples/customer_database_search.pyfor a full example.Getting started with contributingClone the repository first. Then, install the environment and the pre-commit hooks withmakeinstallThe 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, seehere.
For activating the automatic documentation with MkDocs, seehere.
To enable the code coverage reports, seehere.Releasing a new versionCreate an API Token onPypi.Add the API Token to your projects secrets with the namePYPI_TOKENby visitingthis page.Create anew releaseon Github.
Create a new tag in the form*.*.*.For more details, seehere.Repository initiated withfpgmaas/cookiecutter-poetry.