Back to Index

Faster LLM Function Calling — Dynamic Routes


Chapters

0:0 Fast LLM Function Calling
0:56 Semantic Router Setup for LLMs
2:20 Function Calling Schema
4:4 Dynamic Routes for Function Calling
5:51 How we can use Faster Agents

Transcript

Today, we're going to take a look at dynamic routes in the semantic router library. Now, dynamic routes expand what we can do with this library by quite a lot. Unlike a static route, a dynamic route is able to dynamically generate the parameters based on a particular input that can then be taken into whatever you want to do with those parameters.

So the main use case here is function calling. Now, the good thing with dynamic routes is that they can generate this output, but they're still very fast, just like our static routes. So they are fundamentally the same object. And I think what would be best is to just take a look at how they differ, which is not by a huge amount.

Okay, so we're going to start in the docs of the semantic router library. I'm going to go to dynamic routes, and I'm just going to open that notebook in Colab. It's now on version 0.015. This is actually no longer necessary, so I need to remove that. So I'm going to install the library first.

Then I'm going to come down to here, and I'm going to initialize a static route. Now, these are just basic static routes. And the reason we're loading those first is because we want to see what the difference is between these and a dynamic route. So yes, we initialize those.

And then we're going to initialize our route layer. Now, the initialization of a route layer, whether you have dynamic or static routes or both, is exactly the same. It doesn't change. And again, we can use Cohere, we can use OpenAI. There's also now a new FastEmbed encoder as well if you want to run the embedding part locally.

I'm going to use OpenAI because we will also want to use the OpenAI LLM as well. So API key, enter this. And there we go, okay? We do also support the Cohere LLM as well. And soon enough, you will also have local LLMs. But for now, I'm just going to use OpenAI.

It's the easiest. Okay, so we can test that. It's working, and this is purely static routes. Let's see how we might create a dynamic route. So here is how we would set up our dynamic route. We don't need to do it like this directly. You can actually, sorry, this is the actual definition of our dynamic route.

What I'm doing before here is creating this, the function schema that is required for our dynamic route. So the function schema, I can show you what that looks like maybe quickly. So if I run this, it looks like this. This is our function schema. Now, what I'm doing here with the get schema function here is I am taking an existing function and I'm formatting it in a particular way.

So we're using the sphinx.string format here. And we're adding a lot of description as to how exactly this function should actually be used. So finds the current time in a specific time zone. Okay, that's like the description. Okay, what does this function actually do? We need this for our dynamic route to understand what this does and how it should be used.

Then we specify, okay, we have our time zone. The type of our time zone is a string. And the description for it is this. Okay, so the time zone to find the current time in. It should be a valid time zone from the IANA time zone database. And then we get some examples.

And then we specify, do not put the place name like Rome or New York. You must provide this particular IANA format. So we do that. That is then going to, okay, we provide this format and it's gonna give us a time in that particular place. Now, we run that function, get time, that we just created here.

We put that through our get schema function here. We get our function schema output. And then this is what defines the difference between a static route and a dynamic route. We simply pass in this function schema to our route definition. So if I remove that, this is now a static route.

If I add that back in, it's a dynamic route, okay? And that's all there is to it. So we have our new dynamic route. I'm gonna add it to our route layer. And then I'm going to ask a time-related question, okay? And that should trigger the time or the get time dynamic route.

And it should hopefully get the right inputs for that route, okay? And we see that it does. So we have function call and we have these inputs for our function. So then I can connect this up to the function that we created. So say out equals this. And I want to say, get time.

And it's out and it's the function call. And then obviously there's the, this. Okay, let's see what we get. Okay, six, 16. And basically you can expect this to work with any function call that you'd expect an LLM to normally be able to handle because we're using an LLM here.

So what we're really doing is we're setting up that kind of like agentic workflow where an agent will decide what to do and then generate the input for whatever it decides to do. We're taking away the decision part on which route to take or which tool to use. And we're using semantics to make that decision, but we still rely on the LLM to generate the function call itself, which we've seen it does and it does pretty quickly.

Now, that's it for this video. I hope this has been useful and interesting. So thank you very much for watching and I will see you again in the next one. Bye. (upbeat music) (soft music) (soft music) (soft music) you you