feat: anthropic supports enum (#524)

This commit is contained in:
Chris Ruppelt
2024-03-22 08:50:02 -04:00
committed by GitHub
parent 496a6de4ff
commit 6bfb699b8e
2 changed files with 49 additions and 8 deletions
+12 -1
View File
@@ -83,10 +83,21 @@ def _add_params(
if (
isinstance(details, dict) and "$ref" in details
): # Checking if there are nested params
reference = _resolve_reference(references, details["$ref"])
if 'enum' in reference:
type_element.text = reference['type']
enum_values = reference['enum']
values = ET.SubElement(parameter, "values")
for value in enum_values:
value_element = ET.SubElement(values, "value")
value_element.text = value
continue
nested_params = ET.SubElement(parameter, "parameters")
list_found |= _add_params(
nested_params,
_resolve_reference(references, details["$ref"]),
reference,
references,
)
elif field_type == "array": # Handling for List[] type
+37 -7
View File
@@ -1,12 +1,14 @@
import pytest
import anthropic
import instructor
from pydantic import BaseModel
from enum import Enum
from typing import List
create = instructor.patch(
create=anthropic.Anthropic().messages.create, mode=instructor.Mode.ANTHROPIC_TOOLS
)
import anthropic
import pytest
from pydantic import BaseModel
import instructor
create = instructor.patch(create=anthropic.Anthropic().messages.create, mode=instructor.Mode.ANTHROPIC_TOOLS)
@pytest.mark.skip
def test_anthropic():
@@ -33,3 +35,31 @@ def test_anthropic():
) # type: ignore
assert isinstance(resp, User)
@pytest.mark.skip
def test_anthropic_enum():
class ProgrammingLanguage(Enum):
PYTHON = "python"
JAVASCRIPT = "javascript"
TYPESCRIPT = "typescript"
UNKNOWN = "unknown"
OTHER = "other"
class SimpleEnum(BaseModel):
language: ProgrammingLanguage
resp = create(
model="claude-3-haiku-20240307",
max_tokens=1024,
max_retries=0,
messages=[
{
"role": "user",
"content": "What is your favorite programming language?",
}
],
response_model=SimpleEnum,
) # type: ignore
assert isinstance(resp, SimpleEnum)