# OpenAI functions
The `openai-functions` Python project simplifies the usage of OpenAI's ChatGPT [function calling](https://platform.openai.com/docs/guides/gpt/function-calling) feature. It abstracts away the complexity of parsing function signatures and docstrings by providing developers with a clean and intuitive interface.
![Tests](https://github.com/rizerphe/openai-functions/actions/workflows/tests.yml/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/rizerphe/openai-functions/badge.svg?branch=main)](https://coveralls.io/github/rizerphe/openai-functions?branch=main) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![PyPI version](https://badge.fury.io/py/openai-functions.svg)](https://badge.fury.io/py/openai-functions) [![Documentation Status](https://readthedocs.org/projects/openai-functions/badge/?version=latest)](https://openai-functions.readthedocs.io/en/latest/?badge=latest)
## Installation
You can install `openai-functions` from PyPI using pip:
```
pip install openai-functions
```
## Usage
1. Import the necessary modules and provide your API key:
```python
import enum
import openai
from openai_functions import Conversation
openai.api_key = "<YOUR_API_KEY>"
```
2. Create a `Conversation` instance:
```python
conversation = Conversation()
```
3. Define your functions using the `@conversation.add_function` decorator:
```python
class Unit(enum.Enum):
FAHRENHEIT = "fahrenheit"
CELSIUS = "celsius"
@conversation.add_function()
def get_current_weather(location: str, unit: Unit = Unit.FAHRENHEIT) -> dict:
"""Get the current weather in a given location.
Args:
location (str): The city and state, e.g., San Francisco, CA
unit (Unit): The unit to use, e.g., fahrenheit or celsius
"""
return {
"location": location,
"temperature": "72",
"unit": unit.value,
"forecast": ["sunny", "windy"],
}
```
4. Ask the AI a question:
```python
response = conversation.ask("What's the weather in San Francisco?")
# Should return something like:
# The current weather in San Francisco is 72 degrees Fahrenheit and it is sunny and windy.
```
You can read more about how to use `Conversation` [here](https://openai-functions.readthedocs.io/en/latest/conversation.html).
## More barebones use - just schema generation and result parsing:
```python
from openai_functions import FunctionWrapper
wrapper = FunctionWrapper(get_current_weather)
schema = wrapper.schema
result = wrapper({"location": "San Francisco, CA"})
```
Or you could use [skills](https://openai-functions.readthedocs.io/en/latest/skills.html).
## Another use case: data extraction
1. Import the necessary modules and provide your API key:
```python
from dataclasses import dataclass
import openai
from openai_functions import nlp
openai.api_key = "<YOUR_API_KEY>"
```
3. Define your data container using the `@nlp` decorator:
```python
@nlp
@dataclass
class Person:
"""Extract personal info"""
name: str
age: int
```
4. Ask the AI for the extracted data:
```python
person = Person.from_natural_language("I'm Jack and I'm 20 years old.")
```
You can read more about `@nlp` [here](https://openai-functions.readthedocs.io/en/latest/nlp_interface.html).
Note: mypy does not parse class decorators ([#3135](https://github.com/python/mypy/issues/3135)), so you might have trouble getting type checking when using it like this. Consider using something like `nlp(Person).from_natural_language` to get proper type support.
## How it Works
`openai-functions` takes care of the following tasks:
- Parsing the function signatures (with type annotations) and docstrings.
- Sending the conversation and function descriptions to the OpenAI model.
- Deciding whether to call a function based on the model's response.
- Calling the appropriate function with the provided arguments.
- Updating the conversation with the function response.
- Repeating the process until the model generates a user-facing message.
This abstraction allows developers to focus on defining their functions and adding user messages without worrying about the details of function calling.
## Note
Please note that `openai-functions` is an unofficial project not maintained by OpenAI. Use it at your discretion.
Raw data
{
"_id": null,
"home_page": "https://github.com/rizerphe/openai-functions",
"name": "openai-functions",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "nlp,openai,openai-api,chatgpt,chatgpt-api,wrapper,functions,chatgpt-api,openai-functions,chatgpt-functions,typing,docstring,docstrings,decorators,signatures,parsing",
"author": "rizerphe",
"author_email": "44440399+rizerphe@users.noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/98/17/d83a63e5944ebcbb0d8c5dd87d1621ef99111e4a1188d90022cd352d4d82/openai_functions-1.0.0.tar.gz",
"platform": null,
"description": "# OpenAI functions\n\nThe `openai-functions` Python project simplifies the usage of OpenAI's ChatGPT [function calling](https://platform.openai.com/docs/guides/gpt/function-calling) feature. It abstracts away the complexity of parsing function signatures and docstrings by providing developers with a clean and intuitive interface.\n\n![Tests](https://github.com/rizerphe/openai-functions/actions/workflows/tests.yml/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/rizerphe/openai-functions/badge.svg?branch=main)](https://coveralls.io/github/rizerphe/openai-functions?branch=main) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![PyPI version](https://badge.fury.io/py/openai-functions.svg)](https://badge.fury.io/py/openai-functions) [![Documentation Status](https://readthedocs.org/projects/openai-functions/badge/?version=latest)](https://openai-functions.readthedocs.io/en/latest/?badge=latest)\n\n## Installation\n\nYou can install `openai-functions` from PyPI using pip:\n\n```\npip install openai-functions\n```\n\n## Usage\n\n1. Import the necessary modules and provide your API key:\n\n```python\nimport enum\nimport openai\nfrom openai_functions import Conversation\n\nopenai.api_key = \"<YOUR_API_KEY>\"\n```\n\n2. Create a `Conversation` instance:\n\n```python\nconversation = Conversation()\n```\n\n3. Define your functions using the `@conversation.add_function` decorator:\n\n```python\nclass Unit(enum.Enum):\n FAHRENHEIT = \"fahrenheit\"\n CELSIUS = \"celsius\"\n\n@conversation.add_function()\ndef get_current_weather(location: str, unit: Unit = Unit.FAHRENHEIT) -> dict:\n \"\"\"Get the current weather in a given location.\n\n Args:\n location (str): The city and state, e.g., San Francisco, CA\n unit (Unit): The unit to use, e.g., fahrenheit or celsius\n \"\"\"\n return {\n \"location\": location,\n \"temperature\": \"72\",\n \"unit\": unit.value,\n \"forecast\": [\"sunny\", \"windy\"],\n }\n```\n\n4. Ask the AI a question:\n\n```python\nresponse = conversation.ask(\"What's the weather in San Francisco?\")\n# Should return something like:\n# The current weather in San Francisco is 72 degrees Fahrenheit and it is sunny and windy.\n```\n\nYou can read more about how to use `Conversation` [here](https://openai-functions.readthedocs.io/en/latest/conversation.html).\n\n## More barebones use - just schema generation and result parsing:\n\n```python\nfrom openai_functions import FunctionWrapper\n\nwrapper = FunctionWrapper(get_current_weather)\nschema = wrapper.schema\nresult = wrapper({\"location\": \"San Francisco, CA\"})\n```\n\nOr you could use [skills](https://openai-functions.readthedocs.io/en/latest/skills.html).\n\n## Another use case: data extraction\n\n1. Import the necessary modules and provide your API key:\n\n```python\nfrom dataclasses import dataclass\nimport openai\nfrom openai_functions import nlp\n\nopenai.api_key = \"<YOUR_API_KEY>\"\n```\n\n3. Define your data container using the `@nlp` decorator:\n\n```python\n@nlp\n@dataclass\nclass Person:\n \"\"\"Extract personal info\"\"\"\n\n name: str\n age: int\n```\n\n4. Ask the AI for the extracted data:\n\n```python\nperson = Person.from_natural_language(\"I'm Jack and I'm 20 years old.\")\n```\n\nYou can read more about `@nlp` [here](https://openai-functions.readthedocs.io/en/latest/nlp_interface.html).\n\nNote: mypy does not parse class decorators ([#3135](https://github.com/python/mypy/issues/3135)), so you might have trouble getting type checking when using it like this. Consider using something like `nlp(Person).from_natural_language` to get proper type support.\n\n## How it Works\n\n`openai-functions` takes care of the following tasks:\n\n- Parsing the function signatures (with type annotations) and docstrings.\n- Sending the conversation and function descriptions to the OpenAI model.\n- Deciding whether to call a function based on the model's response.\n- Calling the appropriate function with the provided arguments.\n- Updating the conversation with the function response.\n- Repeating the process until the model generates a user-facing message.\n\nThis abstraction allows developers to focus on defining their functions and adding user messages without worrying about the details of function calling.\n\n## Note\n\nPlease note that `openai-functions` is an unofficial project not maintained by OpenAI. Use it at your discretion.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Simplifies the usage of OpenAI ChatGPT's function calling by generating the schemas and parsing OpenAI's responses for you.",
"version": "1.0.0",
"project_urls": {
"Documentation": "https://openai-functions.readthedocs.io/",
"Homepage": "https://github.com/rizerphe/openai-functions"
},
"split_keywords": [
"nlp",
"openai",
"openai-api",
"chatgpt",
"chatgpt-api",
"wrapper",
"functions",
"chatgpt-api",
"openai-functions",
"chatgpt-functions",
"typing",
"docstring",
"docstrings",
"decorators",
"signatures",
"parsing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2173b0e9d3edd4a0ad99bb619de87626921bc6a224cba97a5d97322d161ce084",
"md5": "7b8f439d440fe04a9f6a7b79729f9289",
"sha256": "5e998f618d2cdcb653671e3de27871d38d87c8ebcb8618e6308cb3c504dcc160"
},
"downloads": -1,
"filename": "openai_functions-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7b8f439d440fe04a9f6a7b79729f9289",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 29456,
"upload_time": "2023-07-03T19:33:40",
"upload_time_iso_8601": "2023-07-03T19:33:40.607179Z",
"url": "https://files.pythonhosted.org/packages/21/73/b0e9d3edd4a0ad99bb619de87626921bc6a224cba97a5d97322d161ce084/openai_functions-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9817d83a63e5944ebcbb0d8c5dd87d1621ef99111e4a1188d90022cd352d4d82",
"md5": "ed0b752261e31e05b0b93671a7f726b7",
"sha256": "8abb82faa63997b23356a7a4abb0b561773165ce9f005108224f8d2c7bba2907"
},
"downloads": -1,
"filename": "openai_functions-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "ed0b752261e31e05b0b93671a7f726b7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 19549,
"upload_time": "2023-07-03T19:33:42",
"upload_time_iso_8601": "2023-07-03T19:33:42.283601Z",
"url": "https://files.pythonhosted.org/packages/98/17/d83a63e5944ebcbb0d8c5dd87d1621ef99111e4a1188d90022cd352d4d82/openai_functions-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-03 19:33:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rizerphe",
"github_project": "openai-functions",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "openai-functions"
}