guardrails-ai / guardrails

Adding guardrails to large language models.
https://www.guardrailsai.com/docs
Apache License 2.0
3.92k stars 297 forks source link

[feat] add support for tools augmentation for structured json response #846

Closed dtam closed 2 months ago

dtam commented 3 months ago

Description adds function that allows augmenting tools guard.call argument to augment tools with a function that allows structured response from llm

Why is this needed models that support the openai tools api can generate structured response through forcing a tool choice and providing a with a signature that matches the desired outputs return format and mentioning the tool must be called last

Implementation details add Guard.augment_tools_with_schema( update callable return handling to handle function choice and arguments in response

End result

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.augment_tools_with_schema(tools),
      tool_choice="required",
)
dtam commented 2 months ago

this happened in 0.5.0. closing