mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
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:
@@ -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"]
|
||||
|
||||
Reference in New Issue
Block a user