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

Adds support for json structured return through tools #840

Merged
merged 15 commits into from
Jun 20, 2024

Conversation

dtam
Copy link
Contributor

@dtam dtam commented Jun 18, 2024

adds support for tools based function calling as a strategy for from_pydantic guards to achieve structured output see usage below. this is only supported on openAI models that support function calling and

class Task(BaseModel):
    status: str
    priority: int
    description: str

guard = Guard.from_pydantic(Tasks)
tools = [] # openai function calling tool array
output = guard(
      openai.chat.completions.create,
      model='gpt-4o',
      msg_history=[
          {
              "role": "user",
              "content": "You are a helpful assistant"
              "read this email and return the tasks from it."
              " some email blah blah blah.",
          }
      ],
      tools=guard.add_json_function_calling_tool(tools),
      tool_choice="required",
)

note List[BaseModel] as a base_model is not supported as its technically not a pydantic object. It can be achieved with two models see example below

    class Task(BaseModel):
        status: str
        priority: int
        description: str

    class Tasks(BaseModel):
        list: List[Task]

    guard = Guard.from_pydantic(Tasks)

fixes #846

@dtam dtam changed the title adds support for json structured return through tools Adds support for json structured return through tools Jun 19, 2024
@dtam dtam mentioned this pull request Jun 19, 2024
24 tasks
@dtam dtam linked an issue Jun 20, 2024 that may be closed by this pull request
@@ -199,7 +199,11 @@ def _invoke_llm(
# TODO: Update this to tools
# Configure function calling if applicable (only for non-streaming)
fn_kwargs = {}
if base_model and not kwargs.get("stream", False):
if (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this preserves the legacy support for the old default support for old openai functions

@@ -1259,6 +1261,19 @@ def to_dict(self) -> Dict[str, Any]:

return i_guard_dict

def augment_tools_with_schema(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a blocker bc we can always deprecate in favor of something else once we make a decision, but since this is intended to facility function calling can we make the name reflect that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated to add_json_function_calling_tool

@CalebCourier
Copy link
Collaborator

CalebCourier commented Jun 20, 2024

Question about this:

note List[BaseModel] as a base_model is not supported as its technically not a pydantic object

How does this lack of support manifest? We were previously supporting function calling for root level lists here: https://github.com/guardrails-ai/guardrails/blob/main/guardrails/utils/pydantic_utils/v2.py#L130

The output_schema should also support top level lists.

[Edit]: I'm guessing this is what you're talking about: https://github.com/guardrails-ai/guardrails/pull/835/files#r1646518102

@dtam
Copy link
Contributor Author

dtam commented Jun 20, 2024

Question about this:

note List[BaseModel] as a base_model is not supported as its technically not a pydantic object

How does this lack of support manifest? We were previously supporting function calling for root level lists here: https://github.com/guardrails-ai/guardrails/blob/main/guardrails/utils/pydantic_utils/v2.py#L130

The output_schema should also support top level lists.

[Edit]: I'm guessing this is what you're talking about: https://github.com/guardrails-ai/guardrails/pull/835/files#r1646518102

neither the openai tools or functions api does supports array as the base type of the parameter
tool generated by convert_pydantic_model_to_openai_fn( with List[Delivery]
Error code: 400 - {'error': {'message': 'Invalid schema for function \'Delivery\': schema must be a JSON Schema of \'type: "object"\', got \'type: "array"\'.', 'type': 'invalid_request_error', 'param': 'functions[0].parameters', 'code': 'invalid_function_parameters'}}
tool generated with my code with List[Delivery]
Error code: 400 - {'error': {'message': 'Invalid schema for function \'gd_response_tool\': schema must be a JSON Schema of \'type: "object"\', got \'type: "array"\'.', 'type': 'invalid_request_error', 'param': 'tools[0].function.parameters', 'code': 'invalid_function_parameters'}}.

@CalebCourier CalebCourier merged commit 4a7b99f into 0.5.0-dev Jun 20, 2024
12 checks passed
@CalebCourier CalebCourier deleted the dtam/050_tools_json_support branch June 20, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feat] add support for tools augmentation for structured json response
2 participants