https://www.boundaryml.com logo
🟑 Using BAML as a templating library and seeking ...
# questions
r
Hi there; two questions: 1. is it possible to only use BAML as a templating library to start with (keeping my own invocations until you have OpenTelemetry support)?, 2. do you have a simple chat example; where each consecutive round actually appends a message (and we therefore can use caching for the previous messages) β€” to clarify, I'm not looking for creating a brand new context every request as the iteration over messages here does https://docs.boundaryml.com/ref/prompt-syntax/role
g
Hello! You can use the [Modular API](https://docs.boundaryml.com/guide/baml-advanced/modular-api) to generate an HTTP request from your BAML function invocation, then modify the request manually and send it to the provider yourself, then use Modular API again to parse the response if needed. Would that work for your first case?
We have a chatbot example here: https://github.com/BoundaryML/baml-examples/tree/main/python-chatbot It does append messages to a list, but it doesn’t try to cache the prior conversations, although with a little reorganization you certainly could! Would you like to chat about how to accomplish that in BAML?
r
Yes it works, but it's a level above what I want. Perhaps what I would like is to use template_string with jinja2/templating? I statically declare my agents, so I want the instructions statically (without much runtime, i.e. string constants, and perhaps some interpolation) As for the chatbot in https://github.com/BoundaryML/baml-examples/blob/main/python-chatbot/server/baml_src/agent.baml it would seem it builds a new initial message every time; while this reduces the tokens, it also means I can't use cached tokens with any of the providers and I'm now in the object model of BAML rather than the api I'm interacting with.
g
Ah, you want to be able to just evaluate a BAML template_string from client code, you don't want to use BAML functions?
r
Sometimes yes
Any idea about the caching question?
g
πŸ‘‹ Coming back to this. Can you help me understand the LLM call pattern you want to achieve? You mentioned that you don't wan to rebuild your context every time, and I wasn't sure what you meant. (AFAIK, you always need to send the full context in the LLM request, but by marking some of the messages with a cache instruction, you are allowing the model provider to avoid having to shuffle those messages around in its own network).
r
I'd simply like to have a 100+ message conversation properly cached. It would seem to be from the examples that the iteration is inside the system message, thereby rewriting the initial message very time the function is called.
g
You can set things up in the BAML function to generate the sequence of messages that works for the caching strategy you have in mind. If many conversations have the same 100+ message prefix, then you can put the entire session into one cached block:
Copy code
function Talk(history: Message[], query: string) -> string {
  client ClaudeSonnet
  prompt #"
    {{ _.role("user", cache_control={"type": "ephemeral"}) }}
    Conversation history: {{ history }}
    {{ _.role("user") }}
    {{ query }}
  "#
}
If the history changes, you will get a cache miss, so this will only be helpful if the message history is a stable thing you reuse over and over. If the conversations are more dynamic, or you need to preserve the roles of the senders of the messages, you can include the caching instruction in each message. Then you'll have cache hits for all the previously seen messages and misses for new messages:
Copy code
function Talk(history: Message[], query: string) -> string {
  client ClaudeSonnet
  prompt #"
    {% for message in history %}
      {{ _.role(message.role, cache_control={"type": "ephemeral"}) }}
      {{ message.content }}
    {% endfor %}
    {{ query }}
  "#
}
You're right that the query must be recomputed each time we send a request. I've been working under the assumption that the caching you're talking about is LLM provider prompt caching (an optimization on the LLM provider's side), and not caching of the creation of the prompt on the client side. To help me understand, could you say what would proper caching look like for you? You mentioned not wanting to rebuild your context every time, and I didn't quite follow what you mean there.