Docstring parsing using docstring-parser (#76)

* adds description for params

* add test for missing param description

* adds docstring parsing to OpenAISchema

* docs

---------

Co-authored-by: Jason Liu <jxnl@users.noreply.github.com>
This commit is contained in:
Alexander Dicke
2023-08-20 08:40:59 +02:00
committed by GitHub
parent 1ebb7b95c1
commit 76ac9b4d12
6 changed files with 658 additions and 573 deletions
+22 -8
View File
@@ -130,20 +130,29 @@ from instructor import OpenAISchema
from pydantic import Field
class UserDetails(OpenAISchema):
"Correctly extracted user information"
""""
Correctly extracted user information
:param age: age of the user
"""
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. The description provides information about the field, giving even more context to the language model.
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.
!!! 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 field 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 parameter 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"
"""
Correctly extracted user information
:param name: the user's full name
:param age: age of the user
"""
name: str = Field(..., description="User's full name")
age: int
@@ -158,11 +167,12 @@ In this updated schema, we use the `Field` class from `pydantic` to add descript
"type": "object",
"properties": {
"name": {
"description": "User's full name",
"type": "string"
"type": "string",
"description": "User's full name"
},
"age": {
"type": "integer"
"description": "age of the user"
}
},
"required": [
@@ -182,7 +192,11 @@ from instructor import OpenAISchema
from pydantic import Field
class UserDetails(OpenAISchema):
"Correctly extracted user information"
"""
Correctly extracted user information
:param name: the user's full name
:param age: age of the user
"""
name: str = Field(..., description="User's full name")
age: int