Jan 20, 2025

Structured outputs with Deepseek R1

By Vaibhav Gupta

Deepseek has recently released Deepseek R1, a reasoning model that can match the performance of OpenAI's o1 model at some tasks, at 1/50th of the cost (as of January 2025).

Our prompting framework, BAML, lets you do function-calling / tool-use with DeepseekR1 even when it is not officially supported.

Let's look at an interactive example!

Classication with Deepseek R1 and BAML

First we write some BAML code to classify messages. Here we write a function that takes a message and returns a category, that will be executed by an LLM. BAML will parse the result for you into the right enum type.

Loading preview...
No tests running

You can run this in python like this:

from baml_client import b
response = b.ClassifySupport(message="The app keeps crashing when I try to upload files")
print(f"Category: {response.category}")
print(f"Priority: {response.priority}")

Tool calling with Deepseek R1 and BAML

BAML lets you write tools like normal Unions of types. Here's another example where we have two tools, one that searches products, and one that schedules appointments.

Loading preview...
No tests running

You can call this in python like this (we also support other languages!):

from baml_client import b
from baml_client.types import ProductSearch, ScheduleAppointment
response = b.ChooseOneTool(user_message="Find me running shoes under $100 in the sports category")
print(response)

if isinstance(response, ProductSearch):
    print(f"Product Search called:")
    print(f"Query: {response.query}")
    print(f"Max Price: ${response.maxPrice}")
    print(f"Category: {response.category}")
elif isinstance(response, ScheduleAppointment):
    print(f"Schedule Appointment called:")
    print(f"Customer: {response.customerName}")
    print(f"Service: {response.serviceType}")
    print(f"Date: {response.preferredDate}")
    print(f"Duration: {response.duration} minutes")

Thanks for reading!