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
+48 -1
View File
@@ -1,7 +1,9 @@
from typing import Literal
import pytest
from pydantic import BaseModel
from instructor import openai_schema, OpenAISchema
from instructor import openai_schema, OpenAISchema, openai_function
def test_openai_schema():
@@ -40,3 +42,48 @@ def test_no_docstring():
Dummy.openai_schema["description"]
== "Correctly extracted `Dummy` with all the required parameters with correct types"
)
def test_openai_function():
@openai_function
def get_current_weather(
location: str, format: Literal["celsius", "fahrenheit"] = "celsius"
):
"""
Gets the current weather in a given location, use this function for any questions related to the weather
Parameters
----------
location
The city to get the weather, e.g. San Francisco. Guess the location from user messages
format
A string with the full content of what the given role said
"""
@openai_function
def get_current_weather_no_format_docstring(
location: str, format: Literal["celsius", "fahrenheit"] = "celsius"
):
"""
Gets the current weather in a given location, use this function for any questions related to the weather
Parameters
----------
location
The city to get the weather, e.g. San Francisco. Guess the location from user messages
"""
scheme_missing_param = get_current_weather_no_format_docstring.openai_schema
assert (
scheme_missing_param["parameters"]["properties"]["location"]["description"]
==
"The city to get the weather, e.g. San Francisco. Guess the location from user messages"
)
assert (
scheme_missing_param["parameters"]["properties"]["format"]["enum"]
==
["celsius", "fahrenheit"]
)
with pytest.raises(KeyError, match="description"):
scheme_missing_param["parameters"]["properties"]["format"]["description"]