-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
anthropic[patch]: handle lists in function calling #18609
Merged
efriis
merged 3 commits into
master
from
erick/anthropic-patch-handle-lists-in-function-calling
Mar 5, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,16 @@ | |
</parameter>""" | ||
|
||
|
||
def _get_type(parameter: Dict[str, Any]) -> str: | ||
if "type" in parameter: | ||
return parameter["type"] | ||
if "anyOf" in parameter: | ||
return json.dumps({"anyOf": parameter["anyOf"]}) | ||
if "allOf" in parameter: | ||
return json.dumps({"allOf": parameter["allOf"]}) | ||
return json.dumps(parameter) | ||
|
||
|
||
def get_system_message(tools: List[Dict]) -> str: | ||
tools_data: List[Dict] = [ | ||
{ | ||
|
@@ -78,7 +88,7 @@ def get_system_message(tools: List[Dict]) -> str: | |
[ | ||
TOOL_PARAMETER_FORMAT.format( | ||
parameter_name=name, | ||
parameter_type=parameter["type"], | ||
parameter_type=_get_type(parameter), | ||
parameter_description=parameter.get("description"), | ||
) | ||
for name, parameter in tool["parameters"]["properties"].items() | ||
|
@@ -118,21 +128,44 @@ def _xml_to_dict(t: Any) -> Union[str, Dict[str, Any]]: | |
return d | ||
|
||
|
||
def _xml_to_tool_calls(elem: Any) -> List[Dict[str, Any]]: | ||
def _xml_to_function_call(invoke: Any, tools: List[Dict]) -> Dict[str, Any]: | ||
name = invoke.find("tool_name").text | ||
arguments = _xml_to_dict(invoke.find("parameters")) | ||
|
||
# make list elements in arguments actually lists | ||
filtered_tools = [tool for tool in tools if tool["name"] == name] | ||
if len(filtered_tools) > 0 and not isinstance(arguments, str): | ||
tool = filtered_tools[0] | ||
for key, value in arguments.items(): | ||
if key in tool["parameters"]["properties"]: | ||
if "type" in tool["parameters"]["properties"][key]: | ||
if tool["parameters"]["properties"][key][ | ||
"type" | ||
] == "array" and not isinstance(value, list): | ||
arguments[key] = [value] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. crawl for list |
||
if ( | ||
tool["parameters"]["properties"][key]["type"] != "object" | ||
and isinstance(value, dict) | ||
and len(value.keys()) == 1 | ||
): | ||
arguments[key] = list(value.values())[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. crawl for objects like |
||
|
||
return { | ||
"function": { | ||
"name": name, | ||
"arguments": json.dumps(arguments), | ||
}, | ||
"type": "function", | ||
} | ||
|
||
|
||
def _xml_to_tool_calls(elem: Any, tools: List[Dict]) -> List[Dict[str, Any]]: | ||
""" | ||
Convert an XML element and its children into a dictionary of dictionaries. | ||
""" | ||
invokes = elem.findall("invoke") | ||
return [ | ||
{ | ||
"function": { | ||
"name": invoke.find("tool_name").text, | ||
"arguments": json.dumps(_xml_to_dict(invoke.find("parameters"))), | ||
}, | ||
"type": "function", | ||
} | ||
for invoke in invokes | ||
] | ||
|
||
return [_xml_to_function_call(invoke, tools) for invoke in invokes] | ||
|
||
|
||
@beta() | ||
|
@@ -262,7 +295,7 @@ def _format_output(self, data: Any, **kwargs: Any) -> ChatResult: | |
xml_text = text[start:end] | ||
|
||
xml = self._xmllib.fromstring(xml_text) | ||
additional_kwargs["tool_calls"] = _xml_to_tool_calls(xml) | ||
additional_kwargs["tool_calls"] = _xml_to_tool_calls(xml, tools) | ||
text = "" | ||
except Exception: | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dumping json schema like js