Library 12-Factor Agents

1. Natural language to tool calls

Author
HumanLayer (Dex Horthy)
Licence
CC BY-SA 4.0 (text), Apache-2.0 (code)
Source file
content/factor-01-natural-language-to-tool-calls.md
Commit
d20c728
Supports
21, 13

Share-alike: this text is licensed CC BY-SA 4.0. Anything adapted from it must credit the author and be released under the same licence. Code samples are Apache-2.0.

Vendored text from humanlayer/12-factor-agents, shown unchanged except that MDX components and import lines are removed and relative links point at GitHub. Not our writing.

1. Natural Language to Tool Calls

One of the most common patterns in agent building is to convert natural language to structured tool calls. This is a powerful pattern that allows you to build agents that can reason about tasks and execute them.

110-natural-language-tool-calls

This pattern, when applied atomically, is the simple translation of a phrase like

can you create a payment link for $750 to Terri for sponsoring the february AI tinkerers meetup?

to a structured object that describes a Stripe API call like

{
  "function": {
    "name": "create_payment_link",
    "parameters": {
      "amount": 750,
      "customer": "cust_128934ddasf9",
      "product": "prod_8675309",
      "price": "prc_09874329fds",
      "quantity": 1,
      "memo": "Hey Jeff - see below for the payment link for the february ai tinkerers meetup"
    }
  }
}

Note: in reality the stripe API is a bit more complex, a real agent that does this (video) would list customers, list products, list prices, etc to build this payload with the proper ids, or include those ids in the prompt/context window (we’ll see below how those are kinda the same thing though!)

From there, deterministic code can pick up the payload and do something with it. (More on this in factor 3)

nextStep = await llm.determineNextStep(
  """
  create a payment link for $750 to Jeff 
  for sponsoring the february AI tinkerers meetup
  """
  )

# Handle the structured output based on its function
if nextStep.function == 'create_payment_link':
    stripe.paymentlinks.create(nextStep.parameters)
    return  # or whatever you want, see below
elif nextStep.function == 'something_else':
    # ... more cases
    pass
else:  # the model didn't call a tool we know about
    # do something else
    pass

NOTE: While a full agent would then receive the API call result and loop with it, eventually returning something like

I’ve successfully created a payment link for $750 to Terri for sponsoring the february AI tinkerers meetup. Here’s the link: https://buy.stripe.com/test_1234567890

Instead, We’re actually going to skip that step here, and save it for another factor, which you may or may not want to also incorporate (up to you!)

← How We Got Here | Own Your Prompts →

esc

Narrow with repo:jm layer:eval q:naive mine · ↑↓ to move, enter to open