Helper functions that allow us to improve openai's function_call ergonomics
Project description
OpenAI Function Call and Pydantic Integration Module
This Python module provides a powerful and efficient approach to output parsing when interacting with OpenAI's Function Call API. It leverages the data validation capabilities of the Pydantic library to handle output parsing in a more structured and reliable manner. This README will guide you through the installation, usage, and contribution processes of this module. If you have any feedback, leave an issue or hit me up on twitter.
Installation
To get started, clone the repository
git clone https://github.com/jxnl/openai_function_call.git
Next, install the necessary Python packages from the requirements.txt file:
pip install -r requirements.txt
Contributing
Your contributions are welcome! If you have great examples or find neat patterns, clone the repo and add another example. The goal is to find great patterns and cool examples to highlight.
If you encounter any issues or want to provide feedback, you can create an issue in this repository. You can also reach out to me on Twitter at @jxnlco.
Poetry
We also use poetry if you'd like
poetry build
Note that there's no separate pip install command for this module. Simply copy and paste the module's code into your application.
Usage
This module simplifies the interaction with the OpenAI API, enabling a more structured and predictable conversation with the AI. Below are examples showcasing the use of function calls and schemas with OpenAI and Pydantic.
Example 1: Function Calls
import openai
from openai_function_call import openai_function
@openai_function
def sum(a:int, b:int) -> int:
"""Sum description adds a + b"""
return a + b
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
temperature=0,
functions=[sum.openai_schema],
messages=[
{
"role": "system",
"content": "You must use the `sum` function instead of adding yourself.",
},
{
"role": "user",
"content": "What is 6+3 use the `sum` function",
},
],
)
result = sum.from_response(completion)
print(result) # 9
Example 2: Schema Extraction
import openai
from openai_function_call import OpenAISchema
class UserDetails(OpenAISchema):
"""User Details"""
name: str = Field(..., description="User's name")
age: int = Field(..., description="User's age")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
functions=[UserDetails.openai_schema]
messages=[
{"role": "system", "content": "I'm going to ask for user details. Use UserDetails to parse this data."},
{"role": "user", "content": "My name is John Doe and I'm 30 years old."},
],
)
user_details = UserDetails.from_response(completion)
print(user_details) # UserDetails(name="John Doe", age=30)
Advanced Usage
If you want to see more examples checkout the examples folder!
License
This project is licensed under the terms of the MIT license.
For more details, refer to the LICENSE file in the repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for openai_function_call-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | eda379cd1a4623a141b0fbdceebab4c6bf00577eb66735f1247f786bc682d3c5 |
|
MD5 | b8f5b0f38d8fc0c13808639fe2cc5f0e |
|
BLAKE2b-256 | 3598f3b7c518c2c8283c9e98de8243f64249a17f711e08152754f12bf218978a |
Hashes for openai_function_call-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60c53d986aeb60072f46bf86bd0e20295046982d9e0e9a9d057516a8fc2b5f40 |
|
MD5 | 6c9bef5d813746b9e30d18b2a4dd3d23 |
|
BLAKE2b-256 | 93aa043340c62d9bfef612c181e4ea93e464850128efb45cd74343b8ad106eb1 |