# FunctionalOAI
FunctionalOAI is a Python module // library designed to help you make the use of the GPT function calling API much easier.
## Ideas
If you have any features or ideas that could be added to this project, you could open an issue.
## Installation
- Through pip
```shell
pip3 install funcOAI
```
- Through setup.py
```shell
python3 setup.py install
```
## Usage
```python
# importing the main class
from functionalOAI import FunAI
client = FunAI()
@client.attach
def my_function(*args, **kwargs):
# provide gpt with something
# you could also use attachFunctions
client.attachFunctions([my_function, func1, func2])
# or even this cursed one
client.functions = my_function
# to see all functions
print(client.functions)
```
- After creating you're functions then you could use openai python module to create a chat with chatgpt and you could provide it with client.functions e.g ...
```python
from functionalOAI import FunAI
client = FunAI()
@client.attach
def get_current_weather(location: str, unit: str = "fahrenheit"):
"""Get the current weather in a given location"""
weather_info = {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)
def run_conversation():
messages = [{"role": "user", "content": "What's the weather like in Boston?"}]
## function info
functions = client.functions
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=messages,
functions=functions,
function_call="auto",
)
response_message = response["choices"][0]["message"]
if response_message.get("function_call"):
available_functions = {
"get_current_weather": get_current_weather,
}
function_name = response_message["function_call"]["name"]
fuction_to_call = available_functions[function_name]
function_args = json.loads(response_message["function_call"]["arguments"])
function_response = fuction_to_call(
location=function_args.get("location"),
unit=function_args.get("unit"),
)
messages.append(response_message)
messages.append(
{
"role": "function",
"name": function_name,
"content": function_response,
}
)
second_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=messages,
)
return second_response
print(run_conversation())
```
### This is an edited example of the [official OpenAI API example](https://platform.openai.com/docs/guides/gpt/function-calling)
## New Usage Feature
- You could now access all the elements inside the functions list and do all sort of list methods on them (most of them at least you could look in functions.py to see all the overridden list methods)
```python
from functionalOAI import FunAI
client = FunAI()
@client.attach
def suM(a: int, b: int):
"""return the sum of a and b"""
return a + b
def muL(a: int, b: int):
"""return the multiplication of a and b"""
return a * b
print(client.functions)
client.functions.append({"name": "muL", "description": "return the multiplication of a and b", ...})
print(client.functions)
```
### Its made to make removing / adding / deleting functions easy
```python
# see if a function inside the list by the name or the body
print("muL" in client.functions) # True
# remove function by name (you could also add the whole body of the function)
client.functions.remove("muL")
print("muL" in client.functions) # False
# remove a function using the del keyword
del client.functions["muL"] # error since muL was already removed
print(client.functions)
# get the body of a specefic function by its name or its index in the list
print(client.functions["suM"])
# multiple ways to add a function
client.functions.append({"name": "test", "description": "..."})
client.functions.insert("at the index of a specefic function name or by the index", {"name": "test2", "description": "...."})
print(client.functions)
# replace a specefic function
client.functions["test2"] = {"name": "test3", "description": "hackerman++"}
# get the index of a specific function by its name or body
client.functions.index("test3") # the index function calls another function called name_to_key so you could use that directly there is none special about index
# or get the name by the index
client.functions.key_to_name(0) # suM
# clear the whole list
client.functions.clear()
```
## NOTICE!!
To make this module work correctly with all of you're functions there are set of rules you have to go by when defining the functions
- Providing type annotations for all the arguments e.g ...
```python
def foo(arg1: int, arg2: str, arg3: list):
```
- Providing a docstring that follows [PEP-257 One liner docstrings](https://peps.python.org/pep-0257/#one-line-docstrings) (you could do it as you like as long as its a function desciption without parameters and returns)
```python
def add(x: int, y: int):
"""return the sum of x and y"""
return x + y
```
## WARNING
- This module is not fully complete as it still lacks some extra functionality
- There is no error handling as for now, so obeying the rules of function defining is necessary
## TODO
- ✅create a special type for the functions list, to make it more flexable and accessable, and much more efficient (handeling duplicates etc ...)
- create a multi-line docstring parser that gets a description for the function and a description for each of the arguments
- handle all possible errors in creating the body of the function, and create backup plans in case something was missing in the function definition
## Contribute
### You could contribute through
- Opening issues
- Forking the repo
- Creating Pull requests
- etc...
Raw data
{
"_id": null,
"home_page": "",
"name": "funcOAI",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "python,OpenAI,ChatGPT,GPT API,function calling API,functions,openai-gpt",
"author": "x5up0 aka.ryan",
"author_email": "x5up0sbu@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/60/bb/09829756b8abda5ebdb165d58ff48d79dce2fe5fd87adaef6fda630a158e/funcOAI-0.0.4.tar.gz",
"platform": null,
"description": "# FunctionalOAI\n\nFunctionalOAI is a Python module // library designed to help you make the use of the GPT function calling API much easier.\n\n## Ideas\n\nIf you have any features or ideas that could be added to this project, you could open an issue.\n\n## Installation\n\n- Through pip\n\n```shell \npip3 install funcOAI\n```\n\n- Through setup.py\n\n```shell \npython3 setup.py install\n```\n\n## Usage\n\n```python\n# importing the main class\nfrom functionalOAI import FunAI\n\nclient = FunAI()\n\n@client.attach\ndef my_function(*args, **kwargs):\n # provide gpt with something\n\n# you could also use attachFunctions\nclient.attachFunctions([my_function, func1, func2])\n\n# or even this cursed one\nclient.functions = my_function\n\n# to see all functions\nprint(client.functions)\n```\n\n- After creating you're functions then you could use openai python module to create a chat with chatgpt and you could provide it with client.functions e.g ...\n\n```python\nfrom functionalOAI import FunAI\n\nclient = FunAI()\n\n@client.attach\ndef get_current_weather(location: str, unit: str = \"fahrenheit\"):\n \"\"\"Get the current weather in a given location\"\"\"\n weather_info = {\n \"location\": location,\n \"temperature\": \"72\",\n \"unit\": unit,\n \"forecast\": [\"sunny\", \"windy\"],\n }\n return json.dumps(weather_info)\n\ndef run_conversation():\n messages = [{\"role\": \"user\", \"content\": \"What's the weather like in Boston?\"}]\n\n ## function info\n functions = client.functions\n\n response = openai.ChatCompletion.create(\n model=\"gpt-3.5-turbo-0613\",\n messages=messages,\n functions=functions,\n function_call=\"auto\",\n )\n response_message = response[\"choices\"][0][\"message\"]\n\n if response_message.get(\"function_call\"):\n available_functions = {\n \"get_current_weather\": get_current_weather,\n }\n function_name = response_message[\"function_call\"][\"name\"]\n fuction_to_call = available_functions[function_name]\n function_args = json.loads(response_message[\"function_call\"][\"arguments\"])\n function_response = fuction_to_call(\n location=function_args.get(\"location\"),\n unit=function_args.get(\"unit\"),\n )\n\n messages.append(response_message)\n messages.append(\n {\n \"role\": \"function\",\n \"name\": function_name,\n \"content\": function_response,\n }\n )\n\n second_response = openai.ChatCompletion.create(\n model=\"gpt-3.5-turbo-0613\",\n messages=messages,\n )\n return second_response\n\n\nprint(run_conversation())\n```\n\n### This is an edited example of the [official OpenAI API example](https://platform.openai.com/docs/guides/gpt/function-calling)\n\n\n## New Usage Feature\n\n- You could now access all the elements inside the functions list and do all sort of list methods on them (most of them at least you could look in functions.py to see all the overridden list methods)\n```python\nfrom functionalOAI import FunAI\n\nclient = FunAI()\n\n@client.attach\ndef suM(a: int, b: int):\n \"\"\"return the sum of a and b\"\"\"\n return a + b\n\ndef muL(a: int, b: int):\n \"\"\"return the multiplication of a and b\"\"\"\n return a * b\n\nprint(client.functions)\nclient.functions.append({\"name\": \"muL\", \"description\": \"return the multiplication of a and b\", ...})\nprint(client.functions)\n```\n### Its made to make removing / adding / deleting functions easy\n\n```python\n# see if a function inside the list by the name or the body\nprint(\"muL\" in client.functions) # True\n\n# remove function by name (you could also add the whole body of the function)\nclient.functions.remove(\"muL\")\n\nprint(\"muL\" in client.functions) # False\n\n# remove a function using the del keyword\ndel client.functions[\"muL\"] # error since muL was already removed\n\n\nprint(client.functions)\n\n# get the body of a specefic function by its name or its index in the list\nprint(client.functions[\"suM\"])\n\n# multiple ways to add a function\nclient.functions.append({\"name\": \"test\", \"description\": \"...\"})\nclient.functions.insert(\"at the index of a specefic function name or by the index\", {\"name\": \"test2\", \"description\": \"....\"})\nprint(client.functions)\n\n# replace a specefic function\nclient.functions[\"test2\"] = {\"name\": \"test3\", \"description\": \"hackerman++\"}\n\n# get the index of a specific function by its name or body\nclient.functions.index(\"test3\") # the index function calls another function called name_to_key so you could use that directly there is none special about index \n\n# or get the name by the index\nclient.functions.key_to_name(0) # suM\n\n# clear the whole list\nclient.functions.clear()\n```\n\n## NOTICE!!\n\nTo make this module work correctly with all of you're functions there are set of rules you have to go by when defining the functions\n\n- Providing type annotations for all the arguments e.g ...\n\n```python\ndef foo(arg1: int, arg2: str, arg3: list):\n```\n\n- Providing a docstring that follows [PEP-257 One liner docstrings](https://peps.python.org/pep-0257/#one-line-docstrings) (you could do it as you like as long as its a function desciption without parameters and returns)\n\n```python\ndef add(x: int, y: int):\n \"\"\"return the sum of x and y\"\"\"\n return x + y\n```\n\n## WARNING\n\n- This module is not fully complete as it still lacks some extra functionality\n- There is no error handling as for now, so obeying the rules of function defining is necessary\n\n## TODO\n\n- \u2705create a special type for the functions list, to make it more flexable and accessable, and much more efficient (handeling duplicates etc ...)\n- create a multi-line docstring parser that gets a description for the function and a description for each of the arguments\n- handle all possible errors in creating the body of the function, and create backup plans in case something was missing in the function definition\n\n## Contribute\n\n### You could contribute through\n\n- Opening issues\n- Forking the repo\n- Creating Pull requests\n- etc...\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "A package that helps users easily create functions to feed to GPT function calling API",
"version": "0.0.4",
"project_urls": null,
"split_keywords": [
"python",
"openai",
"chatgpt",
"gpt api",
"function calling api",
"functions",
"openai-gpt"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "53622e5c4a47255fd60c0b69bfd07d3b55c99fc7a4e06f986ceb847ba487fdea",
"md5": "86f3c2341ad24ba2bd5c006f36bb1437",
"sha256": "b6907641abbd5014bfd05e9e3fb038f355977238ff6837c901e36b3a307d574e"
},
"downloads": -1,
"filename": "funcOAI-0.0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "86f3c2341ad24ba2bd5c006f36bb1437",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6655,
"upload_time": "2023-07-06T19:47:06",
"upload_time_iso_8601": "2023-07-06T19:47:06.027820Z",
"url": "https://files.pythonhosted.org/packages/53/62/2e5c4a47255fd60c0b69bfd07d3b55c99fc7a4e06f986ceb847ba487fdea/funcOAI-0.0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "60bb09829756b8abda5ebdb165d58ff48d79dce2fe5fd87adaef6fda630a158e",
"md5": "84fe8f932d0cb6b4af00ea7cbfc20c55",
"sha256": "b9a892fc25c5b44e17ada088206dfcf6df04022296ac078867e5dd6eba76f70d"
},
"downloads": -1,
"filename": "funcOAI-0.0.4.tar.gz",
"has_sig": false,
"md5_digest": "84fe8f932d0cb6b4af00ea7cbfc20c55",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6250,
"upload_time": "2023-07-06T19:47:07",
"upload_time_iso_8601": "2023-07-06T19:47:07.887905Z",
"url": "https://files.pythonhosted.org/packages/60/bb/09829756b8abda5ebdb165d58ff48d79dce2fe5fd87adaef6fda630a158e/funcOAI-0.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-06 19:47:07",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "funcoai"
}