Skip to main content

Streamlined and Efficient LLM function calling.

Project description

hypertion

Effortless schema creation and smooth invocation based on given function's signature or metadata.

🚀 Installation

pip install hypertion

Usage 🤗

Create schema for function calling

from enum import Enum
from hypertion import HyperFunction

hyperfunction = HyperFunction()

class Unit(str, Enum):
    celsius = "celsius"
    fahrenheit = "fahrenheit"

@hyperfunction.takeover(
    description="Get the current weather in a given location"
)
def get_current_weather(
    location: str = HyperFunction.criteria(
        description="The city and state, e.g. San Francisco, CA"), 
    unit: Unit = HyperFunction.criteria(
        default=Unit.fahrenheit, description="The temperature unit scale"
    )
):
    return {
        "location": location,
        "temperature": "72",
        "unit": unit.value,
        "forecast": ["sunny", "windy"],
    }

functions = hyperfunction.as_open_functions

Pass the functions to LLM to generate signature or metadata

import openai

def get_function_signature(prompt: str, functions: list[dict]):
    openai.api_key = "EMPTY"
    openai.api_base = "http://luigi.millennium.berkeley.edu:8000/v1"
    try:
        completion = openai.ChatCompletion.create(
            model="gorilla-openfunctions-v1",
            temperature=0.0,
            messages=[{"role": "user", "content": prompt}],
            functions=functions,
        )
        return completion.choices[0].message.content
    except Exception as e:
        print(e)

signature = get_function_signature(
    prompt="What's the weather like in Boston?", functions=functions
)

Invoke the generated signature

print(hyperfunction.invoke_from_signature(signature=signature))
{'location': 'Boston', 'temperature': '72', 'unit': 'celsius', 'forecast': ['sunny', 'windy']}

Deep Dive 🤗

Create a HyperFunction instance

from hypertion import HyperFunction

hyperfunction = HyperFunction()

Use the takeover decorator to register a function and utilize the criteria static method to define the conditions for parameter evaluation when invoking the function.

import json
from enum import Enum

class Choice(Enum):
    choice1 = '1'
    choice2 = '2'

@hyperfunction.takeover(
    description="<Function's Description>"
)
def function(
    string_param: str = HyperFunction.criteria(
        description="<Description of the parameter>"),
    enum_param: Choice = HyperFunction.criteria(
        default=Choice.choice1, description="<Description of the parameter>"),
    int_param: int = HyperFunction.criteria(
        10, description="<Description of the parameter>")
):
    ...

Only str, int, list, dict and enum.Enum types are supported.

Retrieve the schema specific to the LLM function.

  • OpenAI function schema

    openai_functions = hyperfunction.as_openai_functions
    print(json.dumps(openai_functions, indent=4))
    
    [
        {
            "name": "function",
            "description": "<Function's Description>",
            "parameters": {
                "type": "object",
                "properties": {
                    "string_param": {
                        "type": "string",
                        "description": "<Description of the parameter>"
                    },
                    "enum_param": {
                        "type": "string",
                        "description": "<Description of the parameter>",
                        "enum": [
                            "choice1",
                            "choice2"
                        ]
                    },
                    "int_param": {
                        "type": "integer",
                        "description": "<Description of the parameter>"
                    }
                },
                "required": [
                    "string_param"
                ]
            }
        }
    ]
    
  • Gorilla function schema

    open_functions = hyperfunction.as_open_functions
    print(json.dumps(open_functions, indent=4))
    
    [
        {
            "api_call": "function",
            "name": "function",
            "description": "<Function's Description>",
            "parameters": {
                "type": "object",
                "properties": {
                    "string_param": {
                        "type": "string",
                        "description": "<Description of the parameter>"
                    },
                    "enum_param": {
                        "type": "string",
                        "description": "<Description of the parameter>",
                        "enum": [
                            "choice1",
                            "choice2"
                        ]
                    },
                    "int_param": {
                        "type": "integer",
                        "description": "<Description of the parameter>"
                    }
                },
                "required": [
                    "string_param"
                ]
            }
        }
    ]
    

Attach new HyperFunction instance

Note: A single HyperFunction instance can hold multiple functions. Creating a new HyperFunction instance 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>"
)
def new_function(
    param1: str = HyperFunction.criteria(
        description="<Description of the parameter>"),
    param2: int = HyperFunction.criteria(
        100, description="<Description of the parameter>")
):
    ...

hyperfunction.attach_hyperfunction(new_hyperfunction)
open_functions = hyperfunction.as_open_functions

print(json.dumps(open_functions, indent=4))
[
    {
        "api_call": "function",
        "name": "function",
        "description": "<Function's Description>",
        "parameters": {
            "type": "object",
            "properties": {
                "string_param": {
                    "type": "string",
                    "description": "<Description of the parameter>"
                },
                "enum_param": {
                    "type": "string",
                    "description": "<Description of the parameter>",
                    "enum": [
                        "choice1",
                        "choice2"
                    ]
                },
                "int_param": {
                    "type": "integer",
                    "description": "<Description of the parameter>"
                }
            },
            "required": [
                "string_param"
            ]
        }
    },
    {
        "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"
            ]
        }
    }
]

Invoking the function using LLM generated Signature or Metadata

Note: The hypertion module does not have access to any LLM-specific API, meaning it cannot directly invoke LLM to obtain gorilla-generated Signatures or GPT-generated Metadata. Implementing this functionality seems unnecessary, as various libraries produce outputs in different schemas.

  • From Gorilla's OpenFunction Signature

    import openai
    
    def get_function_signature(prompt: str, functions: list[dict]):
        openai.api_key = "EMPTY"
        openai.api_base = "http://luigi.millennium.berkeley.edu:8000/v1"
        try:
            completion = openai.ChatCompletion.create(
                model="gorilla-openfunctions-v1",
                temperature=0.0,
                messages=[{"role": "user", "content": prompt}],
                functions=functions,
            )
            return completion.choices[0].message.content
        except Exception as e:
            print(e)
    
    signature = get_function_signature(
        prompt="<Your Prompt>", functions=hyperfunction.as_open_functions
    )
    
    output = hyperfunction.invoke_from_signature(signature=signature)
    
  • From OpenAI's Function Metadata

    import json
    import openai
    
    def get_function_metadata(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_call
            return function_.name, json.loads(function_.arguments)
    
        except Exception as e:
            print(e)
    
    name, arguments = get_function_metadata(
        prompt="<Your Prompt>", functions=hyperfunction.as_openai_functions
    )
    
    output = hyperfunction.invoke(name=name, arguments=arguments)
    

Conclusion

The key strength of this approach lies in its ability to automate schema creation, sparing developers the time and complexity of manual setup. By utilizing the takeover decorator and criteria method, the system efficiently manages multiple functions within a HyperFunction instance, 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.

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

hypertion-0.1.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

hypertion-0.1.0-py3-none-any.whl (31.1 kB view details)

Uploaded Python 3

File details

Details for the file hypertion-0.1.0.tar.gz.

File metadata

  • Download URL: hypertion-0.1.0.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.11 Windows/10

File hashes

Hashes for hypertion-0.1.0.tar.gz
Algorithm Hash digest
SHA256 66551f2f860015d116f4c8e59826ed2053eb8940b258a12093813e3ed5460fe5
MD5 26ad46f05221d33fec1bb8f87318431e
BLAKE2b-256 4e50a844dfac0534fe4703bebc3483d4df48b9212faf19defeb9b8109871346c

See more details on using hashes here.

File details

Details for the file hypertion-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hypertion-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.11 Windows/10

File hashes

Hashes for hypertion-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3417e6b1d5f80fc0c8f01b2589c8770bd8e47558dfa0780c5ffbf09fbe56808b
MD5 5e18535b1c5aacc80dcdc7ddffa81f16
BLAKE2b-256 673fb2ab4ccb201f8f223de1b948327c0b0011d871a203fcef1319fb0b83ec68

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