Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl committed Jul 8, 2023
1 parent 311dd36 commit f9b1c33
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
19 changes: 19 additions & 0 deletions docs/openai_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ user_details = UserDetails.from_response(completion)
print(user_details) # name="John Doe", age=30
```

## Using the decorator

You can also use a decorator but i recommend the class since you get nice autocompletes with VSCode

```python
import openai
from openai_function_call import openai_schema

from pydantic import Field, BaseModel

@openai_schema
class UserDetails(BaseModel):
"""Details of a user"""
name: str = Field(..., description="users's full name")
age: int
```

## OpenAISchema

::: openai_function_call.OpenAISchema
5 changes: 5 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ plugins:
members_order: alphabetical
repo_url: https://github.com/jxnl/openai_function_call
markdown_extensions:
- pymdownx.critic
- pymdownx.caret
- pymdownx.keys
- pymdownx.mark
- pymdownx.tilde
- pymdownx.highlight:
anchor_linenums: true
line_spans: __span
Expand Down
6 changes: 5 additions & 1 deletion openai_function_call/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class OpenAISchema(BaseModel):
@classmethod
@property
def openai_schema(cls):
"""
Return the schema of the class in the format of OpenAI's schema
"""
schema = cls.schema()
parameters = {
k: v for k, v in schema.items() if k not in ("title", "description")
Expand All @@ -98,6 +101,7 @@ def openai_schema(cls):

@classmethod
def from_response(cls, completion, throw_error=True):
"""Execute the function from the response of an openai chat completion"""
message = completion.choices[0].message

if throw_error:
Expand All @@ -116,7 +120,7 @@ def openai_schema(cls):
raise TypeError("Class must be a subclass of pydantic.BaseModel")

@wraps(cls, updated=())
class Wrapper(cls, OpenAISchema):
class Wrapper(cls, OpenAISchema): # type: ignore
pass

return Wrapper

0 comments on commit f9b1c33

Please sign in to comment.