add doc code

This commit is contained in:
Jason
2023-07-08 22:30:29 +08:00
parent 4fc6d71277
commit 35f1ebbb07
4 changed files with 60 additions and 6 deletions
+4
View File
@@ -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
+3 -3
View File
@@ -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
::: openai_function_call
+2
View File
@@ -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
+51 -3
View File
@@ -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 @@ class openai_function:
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 @@ class OpenAISchema(BaseModel):
@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: