Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support anthropic tools #569

Merged
merged 9 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/partial_streaming/run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Part of this code is adapted from the following examples from OpenAI Cookbook:
# https://cookbook.openai.com/examples/how_to_stream_completions
# https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
import time
import tiktoken
import instructor
from openai import OpenAI
from pydantic import BaseModel
Expand Down
148 changes: 0 additions & 148 deletions instructor/anthropic_utils.py

This file was deleted.

9 changes: 7 additions & 2 deletions instructor/client_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ def from_anthropic(
client, (anthropic.Anthropic, anthropic.AsyncAnthropic)
), "Client must be an instance of anthropic.Anthropic or anthropic.AsyncAnthropic"

if mode == instructor.Mode.ANTHROPIC_TOOLS:
create = client.beta.tools.messages.create
else:
create = client.messages.create

if isinstance(client, anthropic.Anthropic):
return instructor.Instructor(
client=client,
create=instructor.patch(create=client.messages.create, mode=mode),
create=instructor.patch(create=create, mode=mode),
provider=instructor.Provider.ANTHROPIC,
mode=mode,
**kwargs,
Expand All @@ -49,7 +54,7 @@ def from_anthropic(
else:
return instructor.AsyncInstructor(
client=client,
create=instructor.patch(create=client.messages.create, mode=mode),
create=instructor.patch(create=create, mode=mode),
provider=instructor.Provider.ANTHROPIC,
mode=mode,
**kwargs,
Expand Down
60 changes: 30 additions & 30 deletions instructor/function_calls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Any, Dict, Optional, Type, TypeVar
from xml.dom.minidom import parseString
from docstring_parser import parse
from functools import wraps
from pydantic import BaseModel, create_model
Expand All @@ -18,18 +17,8 @@


class OpenAISchema(BaseModel): # type: ignore[misc]
@classmethod # type: ignore[misc]
@property
def openai_schema(cls) -> Dict[str, Any]:
"""
Return the schema in the format of OpenAI's schema as jsonschema

Note:
Its important to add a docstring to describe how to best use this class, it will be included in the description attribute and be part of the prompt.

Returns:
model_json_schema (dict): A dictionary in the format of OpenAI's schema as jsonschema
"""
@classmethod
def model_schema(cls) -> Dict[str, Any]:
schema = cls.model_json_schema()
docstring = parse(cls.__doc__ or "")
parameters = {
Expand All @@ -54,22 +43,36 @@ def openai_schema(cls) -> Dict[str, Any]:
f"Correctly extracted `{cls.__name__}` with all "
f"the required parameters with correct types"
)
return schema

@classmethod # type: ignore[misc]
@property
def openai_schema(cls) -> Dict[str, Any]:
"""
Return the schema in the format of OpenAI's schema as jsonschema

Note:
Its important to add a docstring to describe how to best use this class, it will be included in the description attribute and be part of the prompt.

Returns:
model_json_schema (dict): A dictionary in the format of OpenAI's schema as jsonschema
"""
schema = cls.model_schema()
return {
"name": schema["title"],
"description": schema["description"],
"parameters": parameters,
"parameters": schema["properties"],
}

@classmethod
@property
def anthropic_schema(cls) -> str:
from instructor.anthropic_utils import json_to_xml

return "\n".join(
line.lstrip()
for line in parseString(json_to_xml(cls)).toprettyxml().splitlines()[1:]
)
def anthropic_schema(cls) -> Dict[str, Any]:
schema = cls.model_schema()
return {
"name": schema["title"],
"description": schema["description"],
"input_schema": cls.model_json_schema(),
}

@classmethod
def from_response(
Expand All @@ -92,7 +95,7 @@ def from_response(
cls (OpenAISchema): An instance of the class
"""
if mode == Mode.ANTHROPIC_TOOLS:
return cls.parse_anthropic_tools(completion)
return cls.parse_anthropic_tools(completion, validation_context, strict)

if mode == Mode.ANTHROPIC_JSON:
return cls.parse_anthropic_json(completion, validation_context, strict)
Expand All @@ -115,15 +118,12 @@ def from_response(
def parse_anthropic_tools(
cls: Type[BaseModel],
completion: ChatCompletion,
validation_context: Optional[Dict[str, Any]] = None,
strict: Optional[bool] = None,
) -> BaseModel:
try:
from instructor.anthropic_utils import extract_xml, xml_to_model
except ImportError as err:
raise ImportError(
"Please 'pip install anthropic xmltodict' package to proceed."
) from err
assert hasattr(completion, "content")
return xml_to_model(cls, extract_xml(completion.content[0].text)) # type:ignore
tool_call = [c.input for c in completion.content if c.type == "tool_use"][0]

return cls.model_validate(tool_call, context=validation_context, strict=strict) # type:ignore

@classmethod
def parse_anthropic_json(
Expand Down
31 changes: 9 additions & 22 deletions instructor/process_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,29 +282,16 @@ def handle_response_model(
new_kwargs["messages"][0]["content"] += f"\n\n{message}"
elif mode == Mode.ANTHROPIC_TOOLS:
tool_descriptions = response_model.anthropic_schema
system_prompt = (
dedent(
f"""In this environment you have access to a set of tools you can use to answer the user's question.
You may call them like this:
<function_calls>
<invoke>
<tool_name>$TOOL_NAME</tool_name>
<parameters>
<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
...
</parameters>
</invoke>
</function_calls>

Here are the tools available:"""
)
+ tool_descriptions
)
new_kwargs["tools"] = [tool_descriptions]

system_messages = [
m["content"] for m in new_kwargs["messages"] if m["role"] == "system"
]
new_kwargs["system"] = "\n\n".join(system_messages)
new_kwargs["messages"] = [
m for m in new_kwargs["messages"] if m["role"] != "system"
]

if "system" in new_kwargs:
new_kwargs["system"] = f"{system_prompt}\n{new_kwargs['system']}"
else:
new_kwargs["system"] = system_prompt
elif mode == Mode.ANTHROPIC_JSON:
# anthropic wants system message to be a string so we first extract out any system message
openai_system_messages = [
Expand Down
4 changes: 2 additions & 2 deletions instructor/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def reask_messages(response: ChatCompletion, mode: Mode, exception: Exception):
if mode == Mode.ANTHROPIC_TOOLS:
# TODO: we need to include the original response
yield {
"role": "assistant",
"content": f"Validation Errors found:\n{exception}\nRecall the function correctly, fix the errors",
"role": "user",
"content": f"Validation Errors found:\n{exception}\nReca ll the function correctly, fix the errors from\n{response.content}",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo: Rec all

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to use the following format from official documentation to indicate an error?

{
  "role": "user",
  "content": [
    {
      "type": "tool_result",
      "tool_use_id": "toolu_01A09q90qw90lq917835lq9",
      "content": "ConnectionError: the weather service API is not available (HTTP 500)",
      "is_error": true
    }
  ]
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this is great are you able to kick of a PR

Copy link
Contributor

@lazyhope lazyhope Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will take a look at it tomorrow

}
return
if mode == Mode.ANTHROPIC_JSON:
Expand Down
17 changes: 17 additions & 0 deletions instructor/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pydantic


class User(pydantic.BaseModel):
name: str
age: int


from pprint import pprint

pprint(
{
"name": User.model_json_schema()["title"],
"description": User.__doc__,
"input_schema": User.model_json_schema(),
}
)
11 changes: 5 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading