Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Docstring parsing using docstring-parser" #81

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,20 @@ from instructor import OpenAISchema
from pydantic import Field

class UserDetails(OpenAISchema):
""""
Correctly extracted user information
:param age: age of the user
"""
"Correctly extracted user information"
name: str = Field(..., description="User's full name")
age: int
```

In this updated schema, we use the `Field` class from `pydantic` to add descriptions to the `name` field. Moreover, we use the docstring to add information for the parameter `age`.
In both cases, the description provides information about the fields, giving even more context to the language model.
Information from the docstring is extracted using [docstring-parser](https://github.com/rr-/docstring_parser) which supports different docstring styles.
Note that if the `Field` contains a description for a parameter as well as the docstring, the `Field`'s description is used.
In this updated schema, we use the `Field` class from `pydantic` to add descriptions to the `name` field. The description provides information about the field, giving even more context to the language model.


!!! note "Code, schema, and prompt"
We can run `openai_schema` to see exactly what the API will see, notice how the docstrings, attributes, types, and parameter descriptions are now part of the schema. This describes on this library's core philosophies.
We can run `openai_schema` to see exactly what the API will see, notice how the docstrings, attributes, types, and field descriptions are now part of the schema. This describes on this library's core philosophies.

```python hl_lines="2 3"
class UserDetails(OpenAISchema):
"""
Correctly extracted user information
:param name: the user's full name
:param age: age of the user
"""
"Correctly extracted user information"
name: str = Field(..., description="User's full name")
age: int

Expand All @@ -167,12 +158,11 @@ We can run `openai_schema` to see exactly what the API will see, notice how the
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "User's full name"
"description": "User's full name",
"type": "string"
},
"age": {
"type": "integer"
"description": "age of the user"
}
},
"required": [
Expand All @@ -192,11 +182,7 @@ from instructor import OpenAISchema
from pydantic import Field

class UserDetails(OpenAISchema):
"""
Correctly extracted user information
:param name: the user's full name
:param age: age of the user
"""
"Correctly extracted user information"
name: str = Field(..., description="User's full name")
age: int

Expand Down
19 changes: 5 additions & 14 deletions instructor/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# SOFTWARE.

import json
from docstring_parser import parse
from functools import wraps
from typing import Any, Callable
from pydantic import BaseModel, validate_arguments
Expand Down Expand Up @@ -66,25 +65,20 @@ def sum(a: int, b: int) -> int:
def __init__(self, func: Callable) -> None:
self.func = func
self.validate_func = validate_arguments(func)
self.docstring = parse(self.func.__doc__)

parameters = self.validate_func.model.model_json_schema()
parameters["properties"] = {
k: v
for k, v in parameters["properties"].items()
if k not in ("v__duplicate_kwargs", "args", "kwargs")
}
for param in self.docstring.params:
if (name := param.arg_name) in parameters["properties"] and (description := param.description):
parameters["properties"][name]["description"] = description
parameters["required"] = sorted(
k for k, v in parameters["properties"].items() if not "default" in v
)
_remove_a_key(parameters, "additionalProperties")
_remove_a_key(parameters, "title")
self.openai_schema = {
"name": self.func.__name__,
"description": self.docstring.short_description,
"description": self.func.__doc__,
"parameters": parameters,
}
self.model = self.validate_func.model
Expand Down Expand Up @@ -134,20 +128,17 @@ def openai_schema(cls):
model_json_schema (dict): A dictionary in the format of OpenAI's schema as jsonschema
"""
schema = cls.model_json_schema()
docstring = parse(cls.__doc__)
parameters = {
k: v for k, v in schema.items() if k not in ("title", "description")
}
for param in docstring.params:
if (name := param.arg_name) in parameters["properties"] and (description := param.description):
if "description" not in parameters["properties"][name]:
parameters["properties"][name]["description"] = description

parameters["required"] = sorted(
k for k, v in parameters["properties"].items() if not "default" in v
)

schema["description"] = docstring.short_description
if "description" not in schema:
schema[
"description"
] = f"Correctly extracted `{cls.__name__}` with all the required parameters with correct types"

_remove_a_key(parameters, "additionalProperties")
_remove_a_key(parameters, "title")
Expand Down
Loading
Loading