You can use deployed no-code applications as tools in chat completions with Palmyra-X-004.

This approach allows you to:

  • Use no-code applications alongside other tools
  • Chain multiple no-code applications together
  • Combine application outputs with other API calls or business logic

If you don’t have a deployed no-code application, build a text generation or research assistant application in AI Studio and deploy it to get an application ID.

You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.

We recommend setting the API key as an environment variable in a .env file with the name WRITER_API_KEY.

Tool structure

To use a no-code application as a tool, define the tool in a tools array when making the request to the chat endpoint.

The tools array contains an object with the following parameters:

ParameterTypeDescription
typestringThe type of tool, which is function for a no-code application
functionobjectAn object containing the tool’s description and application ID
function.namestringThe name of the tool
function.descriptionstringA description of what the tool will be used for
function.parametersobjectAn object containing the tool’s input parameters
function.parameters.typestringThe string object
function.parameters.propertiesobjectAn object containing the tool’s parameters in the form of a JSON schema. See below for more details.
function.parameters.requiredarrayAn array of the tool’s required parameters

function.parameters.properties

The function.parameters.properties object contains the tool’s parameter definitions as a JSON schema. The object’s keys should be the names of the parameters, and the values should be objects containing the parameter’s type and description.

When the model decides you should use the tool to answer the user’s question, it will return the parameters that you should use when calling the function you’ve defined.

Below is an example of a tool definition for a no-code application that generates product descriptions.

tools = [
    {
        "type": "function",
        "function": {
            "name": "generate_product_description",
            "description": "A function that will invoke a text-generation application, specialized in generating product descriptions. Any user request asking for product descriptions should use this tool.",
            "parameters": {
                "type": "object",
                "properties": {
                    "product_name": {
                        "type": "string",
                        "description": "The name of the product"
                    }
                },
                "required": ["product_name"]
            }
        }
    }
]

To help the model understand when to use the tool, follow these best practices for the function.description parameter:

  • Indicate that the tool is a function that invokes a no-code application
  • Specify the application’s purpose and capabilities
  • Describe when the tool should be used

An example description for a tool that invokes a text-generation application:

“A function that invokes a text-generation application, specialized in generating product descriptions. Any user request asking for product descriptions should use this tool.”

Response format

When the model decides to use a custom tool, the response from the tool call is returned in the tool_calls object. The tool_calls object contains the following fields:

ParameterTypeDescription
tool_calls[0].idstringThe ID of the tool call
tool_calls[0].functionobjectAn object containing the function to call
tool_calls[0].function.typestringThe type of tool, which is function for a no-code application
tool_calls[0].function.namestringThe name of the function to call
tool_calls[0].function.argumentsstringA JSON-formatted string containing the arguments to pass to the function

Here is an example of a chat completion response that includes a tool call. In this example, the model decides to use the generate_product_description tool to answer the user’s question, and provides the arguments to pass to the function.

sample response
{
"content": null,
"refusal": null,
"role": "assistant",
"graph_data": {
"sources": null,
"status": null,
"subqueries": null
},
"llm_data": null,
"tool_calls": [
{
    "id": "chatcmpl-tool-123",
    "function": {
    "arguments": "{\"product_name\": \"Terra running shoe\"}",
    "name": "generate_product_description"
    },
    "type": "function",
    "index": null
}
],
"image_data": null
}

Usage example

The following example demonstrates how to use a no-code text generation application as a tool in a chat completion. The example uses a hypothetical product description application, but you can use any no-code application in this way.

Create a function that calls your deployed application

First, create a function that calls your deployed application. You will use this function if the LLM you’re chatting with decides to use the tool.

The function below takes a product_name parameter, invokes a no-code text generation application with the application’s required input fields, and returns the generated product description.

If you are unfamiliar with how to invoke a no-code application with the SDK, see the no-code application guide.

def generate_product_description(product_name):
    response = client.applications.generate_content(
        application_id="your-application-id",
        inputs=[
            {
                "id": "Product name",
                "value": [product_name]
            }
        ]
    )
    return response.suggestion

Define your application as a tool

Next, define your application as a tool so the LLM knows when to use it. See the tool structure section for more details on the tool object.

tools = [
    {
        "type": "function",
        "function": {
            "name": "generate_product_description",
            "description": "A function that will invoke a text-generation application, specialized in generating product descriptions. Any user request asking for product descriptions should use this tool.",
            "parameters": {
                "type": "object",
                "properties": {
                    "product_name": {
                        "type": "string",
                        "description": "The name of the product"
                    }
                },
                "required": ["product_name"]
            }
        }
    }
]

Use your application as a tool in a chat completion

Add the tools array to the chat endpoint call along with your array of messages. Setting tool_choice to auto allows the model to choose when to use the no-code application tool, based on the user’s question and the description of the tool.

When the model decides to use the tool you’ve defined, it will indicate that in the tool_calls object of the response.

This example uses a non-streaming response. For a streaming implementation, and to learn more about processing custom tool call responses, see the tool calling guide.

import json
from writerai import Writer

# Initialize the Writer client. If you don't pass the `apiKey` parameter,
# the client looks for the `WRITER_API_KEY` environment variable.
client = Writer()

messages = [{"role": "user", "content": "Generate a description for the Terra running shoe"}]

# Send the request
response = client.chat.chat(
    model="palmyra-x-004",
    messages=messages,
    tools=tools,
    tool_choice="auto",
    stream=False
)

# Process the response
response_message = response.choices[0].message
# Check if the response contains tool calls,
#and if so, call the custom function
tool_calls = response_message.tool_calls
if tool_calls:
    tool_call = tool_calls[0]
    tool_call_id = tool_call.id
    function_name = tool_call.function.name
    function_args = json.loads(tool_call.function.arguments)

    if function_name == "generate_product_description":
        function_response = generate_product_description(function_args["product_name"])
        
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call_id,
            "name": function_name,
            "content": function_response
        })

final_response = client.chat.chat(
    model="palmyra-x-004",
    messages=messages,
    stream=False
)

print(final_response.choices[0].message.content)
# Here's a product description for the Terra running shoe: ...

Here is the full code example:

import json
from writerai import Writer

# Initialize the Writer client. If you don't pass the `apiKey` parameter,
# the client looks for the `WRITER_API_KEY` environment variable.
client = Writer()

def generate_product_description(product_name):
    response = client.applications.generate_content(
        application_id="your-application-id",
        inputs=[
            {
                "id": "Product name",
                "value": [product_name]
            }
        ]
    )
    return response.suggestion

tools = [
    {
        "type": "function",
        "function": {
            "name": "generate_product_description",
            "description": "A function that will invoke a text-generation application, specialized in generating product descriptions. Any user request asking for product descriptions should use this tool.",
            "parameters": {
                "type": "object",
                "properties": {
                    "product_name": {
                        "type": "string",
                        "description": "The name of the product"
                    }
                },
                "required": ["product_name"]
            }
        }
    }
]

messages = [{"role": "user", "content": "Generate a description for the Terra running shoe"}]

# Send the request
response = client.chat.chat(
    model="palmyra-x-004",
    messages=messages,
    tools=tools,
    tool_choice="auto",
    stream=False
)

# Process the response
response_message = response.choices[0].message
# Check if the response contains tool calls,
#and if so, call the custom function
tool_calls = response_message.tool_calls
if tool_calls:
    tool_call = tool_calls[0]
    tool_call_id = tool_call.id
    function_name = tool_call.function.name
    function_args = json.loads(tool_call.function.arguments)

    if function_name == "generate_product_description":
        function_response = generate_product_description(function_args["product_name"])
        
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call_id,
            "name": function_name,
            "content": function_response
        })

final_response = client.chat.chat(
    model="palmyra-x-004",
    messages=messages,
    stream=False
)

print(final_response.choices[0].message.content)
# Here's a product description for the Terra running shoe: ...

Next steps

Now that you’ve defined a custom tool, learn about prebuilt tools and how to use them in your applications: