Package: hypertion
Effortless function schema creation and efficient invocation based on given function's signature or metadata.🚀 InstallationpipinstallhypertionUsage 🤗Create aHyperFunctioninstancefromhypertionimportHyperFunctionhyperfunction=HyperFunction()Use thetakeoverdecorator to register a function and utilize thecriteriastatic method to define the conditions for parameter evaluation when invoking the function.importjsonfromenumimportEnumfrompydanticimportBaseModel,FieldclassUnit(str,Enum):celsius="celsius"fahrenheit="fahrenheit"classSettings(BaseModel):unit:Unit=Field(description="The unit scale to represent temperature")forecast:bool=Field(default=False,description="If set to True, returns the forecasting.")@hyperfunction.takeover(description="Get the current weather for a given location")defget_current_weather(location:str=HyperFunction.criteria(description="The city and state, e.g. San Francisco, CA"),settings:Settings=HyperFunction.criteria(description="Settings to use for getting current weather.")):info={"location":location,"temperature":"72","unit":settings.unit.value,}ifsettings.forecastisTrue:info=info|{"forecast":["sunny","windy"]}returninfoOpenFunctions Schema Representationprint(json.dumps(hyperfunction.as_open_functions,indent=4))[{"api_call":"get_current_weather","name":"get_current_weather","description":"Get the current weather for a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"settings":{"type":"object","description":"Settings to use for getting current weather.","properties":{"unit":{"type":"string","description":"The unit scale to represent temperature","enum":["celsius","fahrenheit"]},"forecast":{"type":"boolean","description":"If set to True, returns the forecasting."}},"required":["unit"]}},"required":["location","settings"]}}]OpenAIFunctions Schema Representationprint(json.dumps(hyperfunction.as_openai_functions,indent=4))[{"api_call":"get_current_weather","name":"get_current_weather","description":"Get the current weather for a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"settings":{"type":"object","description":"Settings to use for getting current weather.","properties":{"unit":{"type":"string","description":"The unit scale to represent temperature","enum":["celsius","fahrenheit"]},"forecast":{"type":"boolean","description":"If set to True, returns the forecasting."}},"required":["unit"]}},"required":["location","settings"]}}]OpenFunctionsSignatureinvocationimportopenaifromhypertion.typesimportSignaturedefget_openfunctions_response(prompt:str,functions:list[dict]):openai.api_key="EMPTY"openai.api_base="http://luigi.millennium.berkeley.edu:8000/v1""""This API endpoint is only for testing purpose. Do not use it for production use."""try:completion=openai.ChatCompletion.create(model="gorilla-openfunctions-v0",temperature=0.0,messages=[{"role":"user","content":prompt}],functions=functions,)returncompletion.choices[0].message.contentexceptExceptionase:print(e)signature=Signature(get_openfunctions_response(prompt="What is the current weather in Kolkata in fahrenheit scale with forecasting?",functions=hyperfunction.as_open_functions))output=hyperfunction.invoke(signature)print(output){'location': 'Kolkata', 'temperature': '72', 'unit': 'fahrenheit', 'forecast': ['sunny', 'windy']}OpenAIFunctionsMetadatainvocationimportjsonimportopenaifromhypertion.typesimportMetadatadefget_openaifunctions_response(prompt:str,functions:list[dict]):openai.api_key="<OPENAI-API-KEY>"try:completion=openai.ChatCompletion.create(model="gpt-4",temperature=0.0,messages=[{"role":"user","content":prompt}],functions=functions,)function_=completion.choices[0].message.function_callreturnfunction_.name,json.loads(function_.arguments)exceptExceptionase:print(e)name,arguments=get_openaifunctions_response(prompt="What is the current weather in Kolkata in fahrenheit scale with forecasting?",functions=hyperfunction.as_openai_functions)output=hyperfunction.invoke(Metadata(name=name,arguments=arguments))print(output){'location': 'Kolkata', 'temperature': '72', 'unit': 'fahrenheit', 'forecast': ['sunny', 'windy']}Important: Thehypertionlibrary lacks the capability to interact directly with LLM-specific APIs, meaning it cannot directly request gorilla-generated Signatures or GPT-generated Metadata from the LLM. This design choice was made to provide more flexibility to developers, allowing them to integrate or adapt different tools and libraries as per their project needs.Attach newHyperFunctioninstanceNote: A singleHyperFunctioninstance can hold multiple functions. Creating a newHyperFunctioninstance is beneficial only if you need a distinct set of functions. This approach is especially effective when deploying Agent(s) to utilize functions designed for particular tasks.new_hyperfunction=HyperFunction()@new_hyperfunction.takeover(description="<Function's Description>")defnew_function(param1:str=HyperFunction.criteria(description="<Description of the parameter>"),param2:int=HyperFunction.criteria(100,description="<Description of the parameter>")):...Merged Schemaimportjsonhyperfunction.attach_hyperfunction(new_hyperfunction)print(json.dumps(hyperfunction.as_open_functions,indent=4))[{"api_call":"get_current_weather","name":"get_current_weather","description":"Get the current weather for a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"settings":{"type":"object","description":"Settings to use for getting current weather.","properties":{"unit":{"type":"string","description":"The unit scale to represent temperature","enum":["celsius","fahrenheit"]},"forecast":{"type":"boolean","description":"If set to True, returns the forecasting."}},"required":["unit"]}},"required":["location","settings"]}},{"api_call":"new_function","name":"new_function","description":"<Function's Description>","parameters":{"type":"object","properties":{"param1":{"type":"string","description":"<Description of the parameter>"},"param2":{"type":"integer","description":"<Description of the parameter>"}},"required":["param1"]}}]ConclusionThe key strength of this approach lies in its ability to automate schema creation, sparing developers the time and complexity of manual setup. By utilizing thetakeoverdecorator andcriteriamethod, the system efficiently manages multiple functions within aHyperFunctioninstance, a boon for deploying Agents in LLM applications. This automation not only streamlines the development process but also ensures precision and adaptability in handling task-specific functions, making it a highly effective solution for agent-driven scenarios.