https://www.boundaryml.com logo
runtime defind types
# general
p
If we're trying to have a runtime defined 'Toolbox' provided to a model, how would that work? Can we have an @@dynamic Type? e.g.:
Copy code
baml
type Toolbox @@dynamic // chosen tools get injected here

class AgentResponse {
  tool_calls Toolbox[]?
  final_answer string?
}

function StructuredAgent(query: string, tool_schemas: string) -> AgentResponse {
  client openai/gpt-4o
  prompt #"You are a ...
Here is the user query: {{ query }}
...Based on the query, call a tool if necessary...
Available tools:
{{ tool_schemas }}

{{ ctx.output_format }}"#    }
Does this work? Or can we only have an enum/class have a dynamic attribute - would Toolbox have to be a class or enum then?
b
TypeBuilder allows add_enum() and add_class()!
So what you’d do is instead of defining tool_calls and a placeholder Toolbox, just have AgentResponse defined statically
and then add tool_calls with a new type dynamically
p
I am realizing that a template_string may also work well (better?) for this - but need to look at how to make this a smooth helper function from the Python side
oh great - what would this look like?
er, sorry, wrong section
clipboard is being wonky…
“Creating new dynamic classes or enums not in BAML” is the section on that page you’re looking for
p
BAML-wise, as well as from the Python side? e.g. say we have a
Copy code
python
@tool
def use_symbolic_solver
  ...

@tool
def google_search_tool
  ...
How would this then be injected on the BAML side?
b
We don’t have any ergonomics around building TypeBuilder from pydantic types / fn signatures yet (we’ve talked about it!) so you’d be building the types yourself
p
aka would it be a new class we add?
b
alternatively if the tools individually are static, you could define them in baml
and then just build Toolbox as a union dynamically
p
Yes that's fine - I'm assuming we have some custom introspection logic to do the fn signature generation
b
Copy code
from baml_client.type_builder import TypeBuilder
from baml_client.async_client import b

async def run():
  tb = TypeBuilder()
  hobbies_enum = tb.add_enum("Hobbies")
  hobbies_enum.add_value("Soccer")
  hobbies_enum.add_value("Reading")

  address_class = tb.add_class("Address")
  address_class.add_property("street", tb.string()).description("The user's street address")

  tb.User.add_property("hobby", hobbies_enum.type().optional())
  tb.User.add_property("address", address_class.type().optional())
  res = await b.DynamicUserCreator("some user info", {"tb": tb})
  # Now res might have the hobby property, which can be Soccer or Reading
  print(res)
p
But we specifically need to be able to deal with dynamic tools from the program side
b
Copy code
class User {
  name string
  age int
  @@dynamic
}

function DynamicUserCreator(user_info: string) -> User {
  client GPT4
  prompt #"
    Extract the information from this chunk of text:
    "{{ user_info }}"

    {{ ctx.output_format }}
  "#
}
p
Yes - I've been reading the docs on this section, would the BAML code look something like this then:
Copy code
baml
class AgentResponse {
  final_answer string?
@@dynamic
}

function StructuredAgent(query: string, tool_schemas: string) -> AgentResponse {
  client openai/gpt-4o
  prompt #"You are a ...
Here is the user query: {{ query }}
...Based on the query, call a tool if necessary...
Available tools:
{{ tool_schemas }}

{{ ctx.output_format }}"#    }
and on the Python side (assuming we have these defined tools):
Copy code
python
@tool
def use_symbolic_solver
  ...

@tool
def google_search_tool
  ...
The dynamic code would be something like:
b
Yep, exactly edit: I’ll wait for the example python code 😅
p
Copy code
python
from baml_client.type_builder import TypeBuilder
from baml_client.async_client import b

async def run():
  tb = TypeBuilder()
tb.AgentResponse.add_property("symbolic solver tool", tb.string())
tb.AgentResponse.add_property("google search tool", tb.sting())

...
Yeah sorry that took a second to make sure I was getting it right
b
Do things make sense to you now? I could hop on a call in 15-20 min if you’d like
p
Yeah that would great actually
In around 10 mins or so?
b
I'm on office hours!
summary: we hopped on for an ~hour, got set up with collector and dynamic types in probably about 10-15 min live, and just spent the rest of the time talking thru PrimeTime’s specific technical/organizational/product use case
p
Also, @baml-sam I think the .with_options setup is perfect for the default values thing we were talking about
b
Ah, I forgot we added that one!
p
Though I’m not sure it’s able to be a persistent ‘setting’ that can be set somewhere in a module and then active whenever the module is imported - will have to test and find out
b
I'd do it as
from our_baml_client import b
and then in
our_baml_client.py
you'd do
Copy code
b = baml_client.b.with_options(...)
It still doesn't buy you the decorator wrapping functionality - we still need something around callback support