How do i dynamically create a function tool from a...
# ask-ai
c
How do i dynamically create a function tool from a json?
t
Thanks for reaching out, Rithvik. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can dynamically create a function tool from a JSON schema using the
function_tool
decorator with the
raw_schema
parameter. Here's an example:
Copy code
from livekit.agents import function_tool

# Create a function tool using raw JSON schema
@function_tool(raw_schema={
    "name": "my_function",
    "description": "Description of my function",
    "parameters": {
        # JSON schema parameters here
    }
})
async def my_function(raw_arguments: dict[str, object]):
    # Function implementation
    # raw_arguments contains the parsed JSON arguments
    pass
The
raw_schema
parameter accepts a dictionary that follows OpenAI's function definition format, which must include at least the
name
and
parameters
keys. This method is compatible with LLM providers supporting raw JSON Schema-based function definitions. Sources: Tree View: | dse-workbench
c
What would the
# JSON schema parameters
here look like?
t
Unfortunately, the sources I reviewed do not provide specific examples of what the
# JSON schema parameters
should look like in practice. However, the
raw_schema
must include a
name
and
parameters
key, and the parameters should follow the JSON schema format. If you have a specific use case in mind, I recommend defining the parameters based on the expected input and output of your function, adhering to JSON schema standards. If you need further assistance, feel free to provide more details!
c
@function_tool(_raw_schema_={ "name": func_name, "description": tool.description, "parameters": { "type": "object", "properties": { "patient_first_name": {"type": "string"}, "patient_last_name": {"type": "string"}, "patient_date_of_birth": {"type": "string"}, "patient_phone_number": {"type": "string"} } } }) Does this look correct?
t
Your JSON schema appears to be structured correctly for defining a function tool. Here's a breakdown of your schema: • `name`: Specifies the name of the function. • `description`: Provides a description of the function. • `parameters`: Defines the expected input as an object with specific properties. • `properties`: Lists the required fields, each with a type (e.g.,
string
). As long as the
parameters
key adheres to JSON schema standards, this should work. If you encounter any issues, feel free to share the error or behavior you're observing, and I can assist further!