Skip to content
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
2 changes: 1 addition & 1 deletion docs/api_reference/async_guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- "parse"
- "validate"
- "error_spans_in_output"
- "add_json_function_calling_tool"
- "json_function_calling_tool"
- "to_dict"
- "from_dict"
- "to_runnable"
2 changes: 1 addition & 1 deletion docs/api_reference/guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- "parse"
- "validate"
- "error_spans_in_output"
- "add_json_function_calling_tool"
- "json_function_calling_tool"
- "to_dict"
- "from_dict"
- "to_runnable"
6 changes: 3 additions & 3 deletions docs/examples/json_function_calling_tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
"pydantic_guard = Guard.from_pydantic(Schedule)\n",
"\n",
"# Generate the function calling tool and add it to the list\n",
"pydantic_guard_tools = pydantic_guard.add_json_function_calling_tool([])\n",
"pydantic_guard_tools = pydantic_guard.json_function_calling_tool([])\n",
"\n",
"print(pydantic_guard_tools)"
]
Expand Down Expand Up @@ -346,7 +346,7 @@
"\n",
"\n",
"# Generate the function calling tool and add it to the list\n",
"rail_guard_tools = rail_guard.add_json_function_calling_tool([])\n",
"rail_guard_tools = rail_guard.json_function_calling_tool([])\n",
"\n",
"print(rail_guard_tools)"
]
Expand Down Expand Up @@ -981,7 +981,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions docs/migration_guides/0-5-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ print(response)

Guardrails has supported function calling for OpenAI Chat models for a while and previously would auto-insert a function to specify the schema when a Guard was created via a Pydantic model.

In Guardrails 0.5.0, you can use this same pattern regardless of how the Guard was initialized. We also made the process more transparent by allowing you to generate the tool first and decide when to pass it as a keyword argument. For models that support openai tool/function calling (`gpt-4o`, `gpt-4-turbo`, or `gpt-3.5-turbo`), you can extend your existing `tools` with `Guard.add_json_function_calling_tool()`
In Guardrails 0.5.0, you can use this same pattern regardless of how the Guard was initialized. We also made the process more transparent by allowing you to generate the tool first and decide when to pass it as a keyword argument. For models that support openai tool/function calling (`gpt-4o`, `gpt-4-turbo`, or `gpt-3.5-turbo`), you can extend your existing `tools` with `Guard.json_function_calling_tool()`

Example:
```py
Expand Down Expand Up @@ -164,7 +164,7 @@ response = guard(
instructions="You are a helpful assistant.",
prompt=prompt,
prompt_params={"chat_history": chat_history},
tools=guard.add_json_function_calling_tool(tools),
tools=guard.json_function_calling_tool(tools),
tool_choice="required",
)
```
Expand Down
4 changes: 2 additions & 2 deletions docs/pydocs/api_reference/guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"parse",
"validate",
"error_spans_in_output",
"add_json_function_calling_tool",
"json_function_calling_tool",
"to_dict",
"from_dict",
"to_runnable",
Expand All @@ -43,7 +43,7 @@
"parse",
"validate",
"error_spans_in_output",
"add_json_function_calling_tool",
"json_function_calling_tool",
"to_dict",
"from_dict",
"to_runnable",
Expand Down
8 changes: 4 additions & 4 deletions guardrails/guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

from guardrails.utils.tools_utils import (
# Prevent duplicate declaration in the docs
add_json_function_calling_tool as add_json_function_calling_tool_util,
json_function_calling_tool as json_function_calling_tool_util,
)


Expand Down Expand Up @@ -1219,13 +1219,13 @@ def to_dict(self) -> Dict[str, Any]:

return i_guard.to_dict()

def add_json_function_calling_tool(
def json_function_calling_tool(
self,
tools: list,
tools: Optional[list] = None,
) -> List[Dict[str, Any]]:
"""Appends an OpenAI tool that specifies the output structure using
JSON Schema for chat models."""
tools = add_json_function_calling_tool_util(
tools = json_function_calling_tool_util(
tools=tools,
# todo to_dict has a slight bug workaround here
# but should fix in the long run dont have to
Expand Down
7 changes: 4 additions & 3 deletions guardrails/utils/tools_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Optional

from guardrails.classes.schema.processed_schema import ProcessedSchema

Expand All @@ -18,9 +18,10 @@ def schema_to_tool(schema) -> dict:
return tool


def add_json_function_calling_tool(
def json_function_calling_tool(
schema: ProcessedSchema,
tools: List = [],
tools: Optional[List] = None,
) -> List:
tools = tools or []
tools.append(schema_to_tool(schema)) # type: ignore
return tools
4 changes: 2 additions & 2 deletions tests/integration_tests/test_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ def test_string_output(mocker):
mock_invoke_llm = None


def test_add_json_function_calling_tool(mocker):
def test_json_function_calling_tool(mocker):
mock_invoke_llm = mocker.patch(
"guardrails.llm_providers.OpenAIChatCallable._invoke_llm"
)
Expand Down Expand Up @@ -1080,7 +1080,7 @@ class Tasks(BaseModel):
" some email blah blah blah.",
}
],
tools=guard.add_json_function_calling_tool(tools),
tools=guard.json_function_calling_tool(tools),
tool_choice="required",
)

Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/utils/test_tools_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from guardrails.schema.pydantic_schema import pydantic_model_to_schema

from guardrails.utils.tools_utils import add_json_function_calling_tool, schema_to_tool
from guardrails.utils.tools_utils import json_function_calling_tool, schema_to_tool


class Delivery(BaseModel):
Expand Down Expand Up @@ -117,9 +117,9 @@ def test_pydantic_model_to_schema():
}


def test_add_json_function_calling_tool():
def test_json_function_calling_tool():
schema = pydantic_model_to_schema(Person)
tools = add_json_function_calling_tool(schema.json_schema)
tools = json_function_calling_tool(schema.json_schema)
assert tools == [
{
"type": "function",
Expand Down