Package: fastllm
FastLLMFast 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 theroadmap.InstallationpipinstallfastllmSamplesRequire an openai api key inOPENAI_API_KEYenvironment variable or.envfile.exportOPENAI_API_KEY=...AgentsfromfastllmimportAgentfind_cities=Agent("List {{ n }} cities comma separated in {{ country }}.")cities=find_cities(n=3,country="Austria").split(",")print(cities)['Vienna','Salzburg','Graz']fromfastllmimportAgent,Message,Model,Prompt,Rolecreative_name_finder=Agent(Message("You are an expert name finder.",Role.SYSTEM),Prompt("Find {{ n }} names.",temperature=2.0),Prompt("Print names comma separated, nothing else!"),model=Model(name="gpt-4"),)names=creative_name_finder(n=3).split(",")print(names)['Ethan Gallagher, Samantha Cheng, Max Thompson']FunctionsFunctions 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.fromtypingimportLiteralfromfastllmimportAgent,Promptcalculator_agent=Agent(Prompt("Calculate the result for task: {{ task }}"),Prompt("Only give the result number as result without anything else!"),)@calculator_agent.functiondefcalculator(a,b,operator:Literal["+","-","*","/"]):"""A basic calculator using various operators."""matchoperator:case"+":returna+bcase"-":returna-bcase"*":returna*bcase"/":returna/bcase_:raiseValueError(f"Unknown operator{operator}")result=calculator_agent(task="give the final result for (11 + 14) * (6 - 2)")print(result)100another_result=calculator_agent(task="If I have 114 apples and 3 elephants, how many apples will each elephant get?")print(another_result)38Avoid words/phrasesAvoid/ban word and phrases - supports patterns. Patterns follow regex syntax but do not support all features. If the number of possible strings matching the pattern is too large, the pattern is ignored.For avoiding/banning words typically it is advised to put ablank spacein front of the word.cat=Agent(Prompt("Say Cat!"),)print(cat())Cat!No we avoid/ban the regex patternr"[ ]?Cat"(e.g. " Cat" or "Cat") from the response.not_cat=Agent(Prompt("Say Cat!",avoid=r"[ ]?Cat"),)print(not_cat())OpenAI is making fun of us (that really happened!) - obviously we need to be more specific (e.g. ban lowercase and uppercase)Dog!Justkidding,cat!Ok let's try again.seriously_not_a_cat=Agent(Prompt("Say Cat!, PLEEASSEE",avoid=r"[ ]?[Cc][aA][tT]]"),)print(seriously_not_a_cat())Well no cat but kudos for the effort.Sure,hereyougo:"Meow! "Prefer words/phrasesPrefer words/phrases - supports patterns. Patterns follow regex syntax but do not support all features. Only supports pattern matching a limited number of strings. The max token length is set to the longest possible string in the pattern. Also the order of token can not be guaranteed.meow=Agent(Prompt("Say Hi!",prefer="Meow!"),)print(meow())Meow!austria_wins=Agent(Prompt("Predict the score for Austria against Germany.",prefer=r"Austria: [3-4], Germany: [0-1]"),)print(austria_wins())Order of tokens is not guaranteed (and this is obviously false ;))Austria,1:Germany,3meow=Agent(Prompt("Say Hi!",prefer="Meow!",max_tokens=10),)print(meow())Our model can only say "Meow" or "!" up to 10 tokens.Meow!!!!Meow!Meow!Meow!Meow!MeowRoadmapFeaturesPrompts using jinja2 templatesLLM calling with backoff and retryAble to register functions to agents, models and prompts using decoratorsPossible 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 nameFunction 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 LLMStreaming with function callingFunction 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.Force particular function call by providing function call argumentOption to "smartly forget" conversation history in case context length is too long.Prompts with pattern using logit bias to guide LLM completion.Able to switch between models (e.g. 3.5 and 4) within one agent over different prompts.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 etc.)Supporting local LLMs (e.g. llama-1, llama-2, etc.)PackageBasic package structure and functionalityTest cases and high test coverageMock implementation of OpenAI's API for testsTests against multiple python versions100% test coverage (at the moment around 90%)Better documentation including readthedocs site.Better error handling and loggingBetter samples using jupyter notebooksSet up of pre-commitCI using github actionsRelease and versioningDevelopmentUsingpoetry.poetryinstallTestspoetryrunpytestPublishpoetrypublish--build