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

fix(prompts): normalized tools #6220

Merged
merged 11 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all 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

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

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
ImageContentPart,
ImageContentValue,
PromptChatTemplateV1,
PromptFunctionToolV1,
PromptMessage,
PromptToolDefinition,
PromptToolsV1,
PromptVersion,
TextContentPart,
Expand Down Expand Up @@ -131,36 +131,30 @@ def _to_model_kwargs(
def _to_tools(
obj: PromptToolsV1,
) -> Iterator[ToolParam]:
for tool_definition in obj.tool_definitions:
definition = tool_definition.definition
if "name" in definition and "input_schema" in definition:
tool: ToolParam = {
"name": definition["name"],
"input_schema": definition["input_schema"],
}
if "description" in definition:
tool["description"] = definition["description"]
yield tool
for t in obj.tools:
tool: ToolParam = {
"name": t.name,
"input_schema": dict(t.schema_ or {}),
}
if t.description:
tool["description"] = t.description
yield tool


def _from_tools(
tools: Iterable[ToolParam],
) -> PromptToolsV1:
return PromptToolsV1(
tool_definitions=[
PromptToolDefinition(
definition={
"name": tool["name"],
"input_schema": tool["input_schema"],
**(
{"description": description}
if (description := tool.get("description"))
else {}
),
}
type="tools-v1",
tools=[
PromptFunctionToolV1(
type="function-tool-v1",
name=tool["name"],
schema=tool["input_schema"], # type: ignore[call-arg]
description=tool.get("description"),
)
for tool in tools
]
],
)


Expand Down Expand Up @@ -209,6 +203,7 @@ def _from_text_block_param(
obj: TextBlockParam,
) -> TextContentPart:
return TextContentPart(
type="text",
text=TextContentValue(
text=obj["text"],
),
Expand Down Expand Up @@ -244,6 +239,7 @@ def _from_image_block_param(
else:
assert_never(source["data"])
return ImageContentPart(
type="image",
image=ImageContentValue(
url=url,
),
Expand Down Expand Up @@ -274,13 +270,15 @@ def _from_tool_use_block(
arguments = str(obj.input)
assert isinstance(arguments, str)
return ToolCallContentPart(
type="tool_call",
tool_call=ToolCallContentValue(
tool_call_id=obj.id,
tool_call=ToolCallFunction(
type="function",
name=obj.name,
arguments=arguments,
),
)
),
)


Expand All @@ -293,13 +291,15 @@ def _from_tool_use_block_param(
arguments = str(obj["input"])
assert isinstance(arguments, str)
return ToolCallContentPart(
type="tool_call",
tool_call=ToolCallContentValue(
tool_call_id=obj["id"],
tool_call=ToolCallFunction(
type="function",
name=obj["name"],
arguments=arguments,
),
)
),
)


Expand All @@ -322,10 +322,11 @@ def _from_tool_result_block_param(
) -> ToolResultContentPart:
result = str(obj["content"]) if "content" in obj else None # TODO: relax this
return ToolResultContentPart(
type="tool_result",
tool_result=ToolResultContentValue(
tool_call_id=obj["tool_use_id"],
result=result,
)
),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
ImageContentPart,
ImageContentValue,
PromptChatTemplateV1,
PromptFunctionToolV1,
PromptMessage,
PromptToolDefinition,
PromptToolsV1,
PromptVersion,
TextContentPart,
Expand Down Expand Up @@ -117,29 +117,33 @@ def _to_model_kwargs(
def _to_tools(
obj: PromptToolsV1,
) -> Iterable[ChatCompletionToolParam]:
for tool_definition in obj.tool_definitions:
if "function" in tool_definition.definition:
definition = tool_definition.definition["function"]
if "name" in definition:
function: FunctionDefinition = {"name": definition["name"]}
if "parameters" in definition:
function["parameters"] = definition["parameters"]
if "description" in definition:
function["description"] = definition["description"]
if "strict" in definition:
function["strict"] = definition["strict"]
yield {"type": "function", "function": function}
for t in obj.tools:
function: FunctionDefinition = {"name": t.name}
if t.description:
function["description"] = t.description
if t.schema_:
function["parameters"] = dict(t.schema_)
if t.strict is not None:
function["strict"] = t.strict
yield {"type": "function", "function": function}


def _from_tools(
tools: Iterable[ChatCompletionToolParam],
) -> PromptToolsV1:
return PromptToolsV1(
tool_definitions=[
PromptToolDefinition(definition={"function": dict(tool["function"])})
type="tools-v1",
tools=[
PromptFunctionToolV1(
type="function-tool-v1",
name=tool["function"]["name"],
description=tool["function"].get("description"),
schema=tool["function"].get("parameters"), # type: ignore[call-arg]
strict=tool["function"].get("strict"),
)
for tool in tools
if tool["type"] == "function"
]
],
)


Expand Down Expand Up @@ -349,6 +353,7 @@ def _from_tool_call(
tool_call=ToolCallContentValue(
tool_call_id=obj["id"],
tool_call=ToolCallFunction(
type="function",
name=obj["function"]["name"],
arguments=obj["function"]["arguments"],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_round_trip(self) -> None:
assert not DeepDiff(block, new_block)

def test_formatter(self) -> None:
obj = TextContentPart(text=TextContentValue(text=token_hex(8)))
obj = TextContentPart(type="text", text=TextContentValue(text=token_hex(8)))
variables = Faker().pydict(value_types=(str, int, float, bool))
block: TextBlockParam = _to_text_block_param(obj, variables, _MockFormatter())
assert block["text"] == json.dumps(variables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_round_trip(self) -> None:
assert not DeepDiff(part, new_part)

def test_formatter(self) -> None:
obj = TextContentPart(text=TextContentValue(text=_str()))
obj = TextContentPart(type="text", text=TextContentValue(text=_str()))
variables = Faker().pydict(value_types=(str, int, float, bool))
part: ChatCompletionContentPartTextParam = _to_text_param(obj, variables, _MockFormatter())
assert part["text"] == json.dumps(variables)
Expand Down
Loading
Loading