Package: llm_wrapper
LLM Functions Library Usage GuideThis guide provides a comprehensive overview of how to use thellm_wrapperlibrary, which offers a versatile wrapper,llm_func, designed for seamless interactions with various language learning models (LLMs). The wrapper simplifies model initialization, query execution, and structured output parsing, supporting a wide range of return types including basic data types (int,float,str,bool,set,list,dict) and complex PydanticBaseModelstructures.Getting Startedfromllm_wrapperimportllm_funcfromlangchain_openaiimportOpenAI@llm_funcdeffamous_quote()->str:"""Returns a famous quote according to the subject provided."""passllm=OpenAI()query="Peace and War"quote=famous_quote(llm=llm,query=query)print(quote)# Output: "Peace is not a relationship of nations. It is a condition of mind brought about by a serenity of soul. Peace is not merely the absence of war. It is also a state of mind. Lasting peace can come only to peaceful people. - Jawaharlal Nehru@llm_funcdefcheck_grammar()->float:"""Check the grammar of the sentence and return a float number between 0 and 1 reflecting its correctness."""passquery="I are a student."correctness=check_grammar(llm=llm,query=query)print(correctness)# Output: 0.5query="I am a student."correctness=check_grammar(llm=llm,query=query)print(correctness)# Output: 1.0InstallationEnsure thellm_wrapperlibrary is installed in your environment. You can install it using pip:pipinstallllm_wrapperImporting the LibraryStart by importing the necessary components:fromllm_wrapperimportllm_funcfrompydanticimportBaseModelInitializing Your LLM ObjectYou'll need to instantiate your preferred LLM object. This library is designed to work flexibly with various LLMs:llm=YourPreferredLLM()Using thellm_funcWrapperThellm_funcwrapper is designed to streamline your interaction with LLMs. It automatically handles functions returning basic types (int,float,str,bool) or PydanticBaseModelinstances.Defining Functions with@llm_funcAnnotate your functions with@llm_funcand define a clear return type. Here's how to define functions returning basic types and Pydantic models:Basic Types@llm_funcdefcalculate_score()->int:"""Returns an integer score based on the input text."""pass@llm_funcdefis_valid()->bool:"""Determines if the text meets certain criteria, returning True or False."""passPydantic BaseModelclassUser(BaseModel):name:strage:int@llm_funcdefget_user_details()->User:"""Extracts user details from the text and returns them as a User model."""passExecuting QueriesPass your query to the function, along with the instantiated LLM object. The wrapper will process the input and return a structured output based on the defined return type.query="Calculate the score for the following text..."score=calculate_score(llm=llm,query=query)print(score)# Output will be of type intquery="Check if the following text is valid..."validity=is_valid(llm=llm,query=query)print(validity)# Output will be of type boolquery="Extract user details from the following text..."user_details=get_user_details(llm=llm,query=query)print(user_details)# Output will be a User instanceSupport and DevelopmentCurrently,llm_funcsupports functions returning basic data types (int,float,str,bool,set,list,dict) and PydanticBaseModelinstances. Support for additional types is under active development, and updates will be released periodically to enhance the library's functionality.By following these guidelines, you can efficiently use thellm_wrapperlibrary to interact with language models, perform queries, and handle structured outputs, all while writing clear and maintainable code.Documentationllm_wrapper Tutorial