Skip to content

Commit

Permalink
add doc code
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl committed Jul 8, 2023
1 parent 4fc6d71 commit 35f1ebb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions docs/openai_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -53,6 +53,6 @@ class UserDetails(BaseModel):
age: int
```

## OpenAISchema
## Code Reference

::: openai_function_call.OpenAISchema
::: openai_function_call
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 51 additions & 3 deletions openai_function_call/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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 = {
Expand All @@ -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:
Expand Down

0 comments on commit 35f1ebb

Please sign in to comment.