Skip to content

Commit

Permalink
fix: clean up imports for anthropic (#517)
Browse files Browse the repository at this point in the history
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
  • Loading branch information
jxnl and ellipsis-dev[bot] authored Mar 21, 2024
1 parent b9eda42 commit bd1e3b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
12 changes: 10 additions & 2 deletions instructor/anthropic_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# type: ignore
import re
import xmltodict
from pydantic import BaseModel
import xml.etree.ElementTree as ET
from typing import Type, Any, Dict, TypeVar


try:
import xmltodict
import xml.etree.ElementTree as ET
except ImportError:
import warnings
warnings.warn("xmltodict and xml.etree.ElementTree modules not found. Please install them to proceed. `pip install xmltodict`", ImportWarning)


T = TypeVar("T", bound=BaseModel)


Expand Down
10 changes: 3 additions & 7 deletions instructor/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from docstring_parser import parse
from functools import wraps
from pydantic import BaseModel, create_model
from instructor.exceptions import IncompleteOutputException
from openai.types.chat import ChatCompletion
from instructor.exceptions import IncompleteOutputException
from instructor.mode import Mode
from instructor.utils import extract_json_from_codeblock
import logging
import importlib

from .anthropic_utils import json_to_xml, extract_xml, xml_to_model

T = TypeVar("T")

Expand Down Expand Up @@ -63,6 +61,7 @@ def openai_schema(cls) -> Dict[str, Any]:
@classmethod
@property
def anthropic_schema(cls) -> str:
from instructor.anthropic_utils import json_to_xml, extract_xml, xml_to_model
return json_to_xml(cls)

@classmethod
Expand All @@ -87,10 +86,7 @@ def from_response(
"""
if mode == Mode.ANTHROPIC_TOOLS:
try:
assert isinstance(
completion,
importlib.import_module("anthropic.types.message").Message,
)
from instructor.anthropic_utils import extract_xml, xml_to_model
except ImportError as err:
raise ImportError("Please 'pip install anthropic' package to proceed.") from err
assert hasattr(completion, "content")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "instructor"
version = "0.6.5"
version = "0.6.6"
description = "structured outputs for llm"
authors = ["Jason Liu <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit bd1e3b9

Please sign in to comment.