hypertion


Namehypertion JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/synacktraa/hypertion
SummaryStreamlined and Efficient LLM function calling.
upload_time2023-12-06 18:53:42
maintainer
docs_urlNone
authorHarsh Verma
requires_python>=3.7
licenseGPL-3.0-only
keywords function-calling openfunctions openai gorilla gpt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">
  <img src="./assets/main.gif" alt="hypertion">
</div>

---

<p align="center">Effortless function schema creation and efficient invocation based on given function's signature or metadata.</p>


## 🚀 Installation

```sh
pip install hypertion
```

---

## Usage 🤗

#### Create a `HyperFunction` instance

```py
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.


```py
import json

from enum import Enum
from pydantic import BaseModel, Field

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

class Settings(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"
)
def get_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,
    }
    if settings.forecast is True:
        info = info | {"forecast": ["sunny", "windy"]}
    
    return info
```

---

<details>

<summary>
OpenFunctions Schema Representation

```py
print(json.dumps(hyperfunction.as_open_functions, indent=4))
```
</summary>

```json
[
    {
        "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"
            ]
        }
    }
]
```
</details>

---

<details>

<summary>
OpenAIFunctions Schema Representation

```py
print(json.dumps(hyperfunction.as_openai_functions, indent=4))
```
</summary>

```json
[
    {
        "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"
            ]
        }
    }
]
```

</details>

---

#### OpenFunctions `Signature` invocation

```py
import openai
from hypertion.types import Signature

def get_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,
        )
        return completion.choices[0].message.content
    except Exception as e:
        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']}
```

#### OpenAIFunctions `Metadata` invocation

```py
import json

import openai
from hypertion.types import Metadata

def get_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_call
        return function_.name, json.loads(function_.arguments)

    except Exception as e:
        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: The `hypertion` library 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 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.

```py
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>")
):
    ...

```
<details>    
<summary>
Merged Schema

```py
import json

hyperfunction.attach_hyperfunction(new_hyperfunction)
print(json.dumps(hyperfunction.as_open_functions, indent=4))
```
</summary>

```json
[
    {
        "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"
            ]
        }
    }
]
```
</details>

### 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.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/synacktraa/hypertion",
    "name": "hypertion",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "function-calling,openfunctions,openai,gorilla,gpt",
    "author": "Harsh Verma",
    "author_email": "synacktra.work@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b4/4b/352c52d866c5d58508c9bef7fbb8c5dad2b766b09db8931ca60b0ba0e392/hypertion-1.0.0.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n  <img src=\"./assets/main.gif\" alt=\"hypertion\">\n</div>\n\n---\n\n<p align=\"center\">Effortless function schema creation and efficient invocation based on given function's signature or metadata.</p>\n\n\n## \ud83d\ude80 Installation\n\n```sh\npip install hypertion\n```\n\n---\n\n## Usage \ud83e\udd17\n\n#### Create a `HyperFunction` instance\n\n```py\nfrom hypertion import HyperFunction\n\nhyperfunction = HyperFunction()\n```\n\n#### 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.\n\n\n```py\nimport json\n\nfrom enum import Enum\nfrom pydantic import BaseModel, Field\n\nclass Unit(str, Enum):\n    celsius = \"celsius\"\n    fahrenheit = \"fahrenheit\"\n\nclass Settings(BaseModel):\n    unit: Unit = Field(description=\"The unit scale to represent temperature\")\n    forecast: bool = Field(\n        default=False, description=\"If set to True, returns the forecasting.\"\n    )\n\n@hyperfunction.takeover(\n    description=\"Get the current weather for a given location\"\n)\ndef get_current_weather(\n    location: str = HyperFunction.criteria(\n        description=\"The city and state, e.g. San Francisco, CA\"),\n    settings: Settings = HyperFunction.criteria(\n        description=\"Settings to use for getting current weather.\"\n    )\n):\n    info = {\n        \"location\": location,\n        \"temperature\": \"72\",\n        \"unit\": settings.unit.value,\n    }\n    if settings.forecast is True:\n        info = info | {\"forecast\": [\"sunny\", \"windy\"]}\n    \n    return info\n```\n\n---\n\n<details>\n\n<summary>\nOpenFunctions Schema Representation\n\n```py\nprint(json.dumps(hyperfunction.as_open_functions, indent=4))\n```\n</summary>\n\n```json\n[\n    {\n        \"api_call\": \"get_current_weather\",\n        \"name\": \"get_current_weather\",\n        \"description\": \"Get the current weather for a given location\",\n        \"parameters\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"location\": {\n                    \"type\": \"string\",\n                    \"description\": \"The city and state, e.g. San Francisco, CA\"\n                },\n                \"settings\": {\n                    \"type\": \"object\",\n                    \"description\": \"Settings to use for getting current weather.\",\n                    \"properties\": {\n                        \"unit\": {\n                            \"type\": \"string\",\n                            \"description\": \"The unit scale to represent temperature\",\n                            \"enum\": [\n                                \"celsius\",\n                                \"fahrenheit\"\n                            ]\n                        },\n                        \"forecast\": {\n                            \"type\": \"boolean\",\n                            \"description\": \"If set to True, returns the forecasting.\"\n                        }\n                    },\n                    \"required\": [\n                        \"unit\"\n                    ]\n                }\n            },\n            \"required\": [\n                \"location\",\n                \"settings\"\n            ]\n        }\n    }\n]\n```\n</details>\n\n---\n\n<details>\n\n<summary>\nOpenAIFunctions Schema Representation\n\n```py\nprint(json.dumps(hyperfunction.as_openai_functions, indent=4))\n```\n</summary>\n\n```json\n[\n    {\n        \"api_call\": \"get_current_weather\",\n        \"name\": \"get_current_weather\",\n        \"description\": \"Get the current weather for a given location\",\n        \"parameters\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"location\": {\n                    \"type\": \"string\",\n                    \"description\": \"The city and state, e.g. San Francisco, CA\"\n                },\n                \"settings\": {\n                    \"type\": \"object\",\n                    \"description\": \"Settings to use for getting current weather.\",\n                    \"properties\": {\n                        \"unit\": {\n                            \"type\": \"string\",\n                            \"description\": \"The unit scale to represent temperature\",\n                            \"enum\": [\n                                \"celsius\",\n                                \"fahrenheit\"\n                            ]\n                        },\n                        \"forecast\": {\n                            \"type\": \"boolean\",\n                            \"description\": \"If set to True, returns the forecasting.\"\n                        }\n                    },\n                    \"required\": [\n                        \"unit\"\n                    ]\n                }\n            },\n            \"required\": [\n                \"location\",\n                \"settings\"\n            ]\n        }\n    }\n]\n```\n\n</details>\n\n---\n\n#### OpenFunctions `Signature` invocation\n\n```py\nimport openai\nfrom hypertion.types import Signature\n\ndef get_openfunctions_response(prompt: str, functions: list[dict]):\n    openai.api_key = \"EMPTY\"\n    openai.api_base = \"http://luigi.millennium.berkeley.edu:8000/v1\"\n    \"\"\"This API endpoint is only for testing purpose. Do not use it for production use.\"\"\"\n    try:\n        completion = openai.ChatCompletion.create(\n            model=\"gorilla-openfunctions-v0\",\n            temperature=0.0,\n            messages=[{\"role\": \"user\", \"content\": prompt}],\n            functions=functions,\n        )\n        return completion.choices[0].message.content\n    except Exception as e:\n        print(e)\n\nsignature = Signature(get_openfunctions_response(\n    prompt=\"What is the current weather in Kolkata in fahrenheit scale with forecasting?\", \n    functions=hyperfunction.as_open_functions\n))\n\noutput = hyperfunction.invoke(signature)\nprint(output)\n```\n```\n{'location': 'Kolkata', 'temperature': '72', 'unit': 'fahrenheit', 'forecast': ['sunny', 'windy']}\n```\n\n#### OpenAIFunctions `Metadata` invocation\n\n```py\nimport json\n\nimport openai\nfrom hypertion.types import Metadata\n\ndef get_openaifunctions_response(prompt: str, functions: list[dict]):\n    openai.api_key = \"<OPENAI-API-KEY>\"\n    try:\n        completion = openai.ChatCompletion.create(\n            model=\"gpt-4\",\n            temperature=0.0,\n            messages=[{\"role\": \"user\", \"content\": prompt}],\n            functions=functions,\n        )\n        function_ = completion.choices[0].message.function_call\n        return function_.name, json.loads(function_.arguments)\n\n    except Exception as e:\n        print(e)\n\nname, arguments = get_openaifunctions_response(\n    prompt=\"What is the current weather in Kolkata in fahrenheit scale with forecasting?\", \n    functions=hyperfunction.as_openai_functions\n)\n\noutput = hyperfunction.invoke(Metadata(name=name, arguments=arguments))\nprint(output)\n```\n```\n{'location': 'Kolkata', 'temperature': '72', 'unit': 'fahrenheit', 'forecast': ['sunny', 'windy']}\n```\n\n> Important: The `hypertion` library 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.\n\n\n#### Attach new `HyperFunction` instance\n\n> 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.\n\n```py\nnew_hyperfunction = HyperFunction()\n\n@new_hyperfunction.takeover(\n    description=\"<Function's Description>\"\n)\ndef new_function(\n    param1: str = HyperFunction.criteria(\n        description=\"<Description of the parameter>\"),\n    param2: int = HyperFunction.criteria(\n        100, description=\"<Description of the parameter>\")\n):\n    ...\n\n```\n<details>    \n<summary>\nMerged Schema\n\n```py\nimport json\n\nhyperfunction.attach_hyperfunction(new_hyperfunction)\nprint(json.dumps(hyperfunction.as_open_functions, indent=4))\n```\n</summary>\n\n```json\n[\n    {\n        \"api_call\": \"get_current_weather\",\n        \"name\": \"get_current_weather\",\n        \"description\": \"Get the current weather for a given location\",\n        \"parameters\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"location\": {\n                    \"type\": \"string\",\n                    \"description\": \"The city and state, e.g. San Francisco, CA\"\n                },\n                \"settings\": {\n                    \"type\": \"object\",\n                    \"description\": \"Settings to use for getting current weather.\",\n                    \"properties\": {\n                        \"unit\": {\n                            \"type\": \"string\",\n                            \"description\": \"The unit scale to represent temperature\",\n                            \"enum\": [\n                                \"celsius\",\n                                \"fahrenheit\"\n                            ]\n                        },\n                        \"forecast\": {\n                            \"type\": \"boolean\",\n                            \"description\": \"If set to True, returns the forecasting.\"\n                        }\n                    },\n                    \"required\": [\n                        \"unit\"\n                    ]\n                }\n            },\n            \"required\": [\n                \"location\",\n                \"settings\"\n            ]\n        }\n    },\n    {\n        \"api_call\": \"new_function\",\n        \"name\": \"new_function\",\n        \"description\": \"<Function's Description>\",\n        \"parameters\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"param1\": {\n                    \"type\": \"string\",\n                    \"description\": \"<Description of the parameter>\"\n                },\n                \"param2\": {\n                    \"type\": \"integer\",\n                    \"description\": \"<Description of the parameter>\"\n                }\n            },\n            \"required\": [\n                \"param1\"\n            ]\n        }\n    }\n]\n```\n</details>\n\n### Conclusion\n\nThe 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.",
    "bugtrack_url": null,
    "license": "GPL-3.0-only",
    "summary": "Streamlined and Efficient LLM function calling.",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/synacktraa/hypertion",
        "Repository": "https://github.com/synacktraa/hypertion"
    },
    "split_keywords": [
        "function-calling",
        "openfunctions",
        "openai",
        "gorilla",
        "gpt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1fa29525f289aaaae822ee3b1e597508b0528abd2636c08bc6a46f907a6c58da",
                "md5": "64dd9f8702d67a27b1a18ee2bdd3f9a5",
                "sha256": "3cb4333ffc55b282fa0c6d4f54f9cf7f0418400c5c312d3390c75dd25c05a924"
            },
            "downloads": -1,
            "filename": "hypertion-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "64dd9f8702d67a27b1a18ee2bdd3f9a5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 32461,
            "upload_time": "2023-12-06T18:53:40",
            "upload_time_iso_8601": "2023-12-06T18:53:40.406817Z",
            "url": "https://files.pythonhosted.org/packages/1f/a2/9525f289aaaae822ee3b1e597508b0528abd2636c08bc6a46f907a6c58da/hypertion-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b44b352c52d866c5d58508c9bef7fbb8c5dad2b766b09db8931ca60b0ba0e392",
                "md5": "22c520270560219838df83dd0b4e63c3",
                "sha256": "91c3ff4df4574c01bf169d5aa9f62a48d75a4908aed422f5173347493e603fbf"
            },
            "downloads": -1,
            "filename": "hypertion-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "22c520270560219838df83dd0b4e63c3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 19203,
            "upload_time": "2023-12-06T18:53:42",
            "upload_time_iso_8601": "2023-12-06T18:53:42.154158Z",
            "url": "https://files.pythonhosted.org/packages/b4/4b/352c52d866c5d58508c9bef7fbb8c5dad2b766b09db8931ca60b0ba0e392/hypertion-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-06 18:53:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "synacktraa",
    "github_project": "hypertion",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "hypertion"
}
        
Elapsed time: 0.23152s