diff --git a/docs/index.md b/docs/index.md index e1b5cb73e..53c79b4d3 100644 --- a/docs/index.md +++ b/docs/index.md @@ -70,3 +70,7 @@ completion = openai.ChatCompletion.create( user_details = UserDetails.from_response(completion) print(user_details) # name="John Doe", age=30 ``` + +# Code + +::: openai_function_call \ No newline at end of file diff --git a/docs/openai_schema.md b/docs/openai_schema.md index e293d4f29..84975f5be 100644 --- a/docs/openai_schema.md +++ b/docs/openai_schema.md @@ -36,7 +36,7 @@ user_details = UserDetails.from_response(completion) print(user_details) # name="John Doe", age=30 ``` -## Using the decorator +## Using the decorator You can also use a decorator but i recommend the class since you get nice autocompletes with VSCode @@ -53,6 +53,6 @@ class UserDetails(BaseModel): age: int ``` -## OpenAISchema +## Code Reference -::: openai_function_call.OpenAISchema \ No newline at end of file +::: openai_function_call \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 4affeae37..c3dba31d0 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -11,6 +11,8 @@ plugins: python: options: members_order: alphabetical + allow_inspection: true + show_bases: true repo_url: https://github.com/jxnl/openai_function_call markdown_extensions: - pymdownx.critic diff --git a/openai_function_call/function_calls.py b/openai_function_call/function_calls.py index f5ebebe4d..b4155405b 100644 --- a/openai_function_call/function_calls.py +++ b/openai_function_call/function_calls.py @@ -37,6 +37,31 @@ def _remove_a_key(d, remove_key) -> None: class openai_function: + """ + Decorator to convert a function into an OpenAI function. + + This decorator will convert a function into an OpenAI function. The + function will be validated using pydantic and the schema will be + generated from the function signature. + + Example: + ```python + @openai_function + def sum(a: int, b: int) -> int: + return a + b + + completion = openai.ChatCompletion.create( + ... + messages=[{ + "content": "What is 1 + 1?", + "role": "user" + }] + ) + sum.from_response(completion) + # 2 + ``` + """ + def __init__(self, func: Callable) -> None: self.func = func self.validate_func = validate_arguments(func) @@ -66,7 +91,16 @@ def wrapper(*args, **kwargs): return wrapper(*args, **kwargs) def from_response(self, completion, throw_error=True): - """Execute the function from the response of an openai chat completion""" + """ + Parse the response from OpenAI's API and return the function call + + Parameters: + completion (openai.ChatCompletion): The response from OpenAI's API + throw_error (bool): Whether to throw an error if the response does not contain a function call + + Returns: + result (any): result of the function call + """ message = completion.choices[0].message if throw_error: @@ -85,7 +119,13 @@ class OpenAISchema(BaseModel): @property def openai_schema(cls): """ - Return the schema of the class in the format of OpenAI's schema + Return the schema in the format of OpenAI's schema as jsonschema + + Note: + Its important to add a docstring to describe how to best use this class, it will be included in the description attribute and be part of the prompt. + + Returns: + model_json_schema (dict): A dictionary in the format of OpenAI's schema as jsonschema """ schema = cls.schema() parameters = { @@ -101,7 +141,15 @@ def openai_schema(cls): @classmethod def from_response(cls, completion, throw_error=True): - """Execute the function from the response of an openai chat completion""" + """Execute the function from the response of an openai chat completion + + Parameters: + completion (openai.ChatCompletion): The response from an openai chat completion + throw_error (bool): Whether to throw an error if the function call is not detected + + Returns: + cls (OpenAISchema): An instance of the class + """ message = completion.choices[0].message if throw_error: