WEBVTT

00:00.960 --> 00:02.070
-: In this video, we're gonna cover

00:02.070 --> 00:04.170
something called ConfigurableFields

00:04.170 --> 00:06.150
inside of LangChain Expression Language.

00:06.150 --> 00:08.610
Basically, configuration fields are a way for you

00:08.610 --> 00:11.790
to actually define specific bits of configuration

00:11.790 --> 00:15.360
that will be invoked or ran at runtime.

00:15.360 --> 00:17.340
Let's have a look at a couple of different examples

00:17.340 --> 00:18.780
so you can see how you can use this

00:18.780 --> 00:20.760
within LangChain Expression Language.

00:20.760 --> 00:23.010
We're firstly importing a couple of different packages,

00:23.010 --> 00:25.590
so langchain and langchain_openai.

00:25.590 --> 00:28.590
After doing that, we set up a very simple PromptTemplate

00:28.590 --> 00:29.640
and we've got our first thing,

00:29.640 --> 00:31.530
which is a ConfigurableField.

00:31.530 --> 00:33.480
Now, ConfigurableFields can be set

00:33.480 --> 00:36.330
on multiple different things such as the prompt.

00:36.330 --> 00:38.130
It can be done at the LLM.

00:38.130 --> 00:40.200
It can be done at lots of different types of things.

00:40.200 --> 00:42.780
These are the main two, though, prompts and LLMs.

00:42.780 --> 00:45.270
If we have a look here, we've got a ConfigurableField.

00:45.270 --> 00:48.690
We set up our chat model, which is a ChatOpenAI model,

00:48.690 --> 00:51.210
and we have this .configurable_fields.

00:51.210 --> 00:53.100
Notice how we set the temperature,

00:53.100 --> 00:55.320
which we know is a ConfigurableField.

00:55.320 --> 00:58.410
We know it's a field that we can access on ChatOpenAI.

00:58.410 --> 01:01.050
We create a ConfigurableField and we give it a name

01:01.050 --> 01:03.450
and a description and an id.

01:03.450 --> 01:05.490
Now, the id is the most important point

01:05.490 --> 01:08.190
because that's actually how you specifically change

01:08.190 --> 01:10.440
that field on whatever it is.

01:10.440 --> 01:12.630
So let's have an example and and see that.

01:12.630 --> 01:15.270
So we've got this model, and if we have a look now,

01:15.270 --> 01:16.620
if we just do the .invoke

01:16.620 --> 01:20.490
and we say pick a random number, it'll pick a random number,

01:20.490 --> 01:23.520
but it will be using the temperature as you can see here,

01:23.520 --> 01:25.050
which has been set to zero.

01:25.050 --> 01:28.740
Now, if we want to then modify that temperature at runtime,

01:28.740 --> 01:31.380
because we've set a ConfigurableField for the temperature,

01:31.380 --> 01:35.340
we can just do model.config, so it's model.with_config,

01:35.340 --> 01:37.830
and then you put a configurable object in there.

01:37.830 --> 01:41.220
Now, this is the same as or type of a RunnableConfig,

01:41.220 --> 01:42.330
and there's lots of different things

01:42.330 --> 01:44.400
you can add into a RunnableConfig.

01:44.400 --> 01:45.960
Now, in this scenario,

01:45.960 --> 01:49.770
notice we're referencing the llm_temperature,

01:49.770 --> 01:52.770
which is the same as we have for this id field

01:52.770 --> 01:54.690
when we create the ConfigurableField.

01:54.690 --> 01:57.510
And we're setting the temperature here to be 0.9,

01:57.510 --> 01:59.160
and then we are running the .invoke.

01:59.160 --> 02:02.910
So again, it's that model.with_config function

02:02.910 --> 02:05.160
that's then gonna allow you to change

02:05.160 --> 02:07.860
any ConfigurableField at runtime.

02:07.860 --> 02:09.480
You could also do this on a prompt.

02:09.480 --> 02:11.730
So for example, we can take a chat model,

02:11.730 --> 02:13.770
we can then specify a PromptTemplate,

02:13.770 --> 02:15.540
and after we're doing the PromptTemplate,

02:15.540 --> 02:18.750
we then have .configurable_alternatives.

02:18.750 --> 02:21.720
And this shows us that we could then have a different field.

02:21.720 --> 02:23.970
So for example, the ConfigurableField here

02:23.970 --> 02:25.530
is the idea of prompt.

02:25.530 --> 02:27.660
The default key will be a joke.

02:27.660 --> 02:31.053
So that basically means that will be the default LLM.

02:31.053 --> 02:33.480
This is the default key that will be used.

02:33.480 --> 02:36.090
However, you could also add a new option.

02:36.090 --> 02:39.000
So you can see here, for example, we have a poem

02:39.000 --> 02:42.240
and this says PromptTemplate.from_template,

02:42.240 --> 02:43.680
write a short poem.

02:43.680 --> 02:45.630
So what's really happening here

02:45.630 --> 02:47.550
is you've got this default prompt

02:47.550 --> 02:50.370
that's gonna be tell me a joke about topic,

02:50.370 --> 02:51.840
but we've also got the ability

02:51.840 --> 02:55.350
to have a different PromptTemplate here if we use the poem.

02:55.350 --> 02:56.310
So let's have a look at that.

02:56.310 --> 02:58.470
So we create our runnable chain.

02:58.470 --> 03:01.860
So you can see the prompt gets piped into the LLM,

03:01.860 --> 03:04.020
and then after that, then we can invoke that.

03:04.020 --> 03:07.290
So what's happening here is on this section above,

03:07.290 --> 03:09.300
you've got tell me a joke about topic

03:09.300 --> 03:10.800
and then we'll get the topic

03:10.800 --> 03:12.270
which will be injected with bears.

03:12.270 --> 03:13.590
So tell me a joke about bears

03:13.590 --> 03:14.760
and it'll say, why did the joke

03:14.760 --> 03:16.140
break up with his girlfriend?

03:16.140 --> 03:19.020
Because he couldn't bear the relationship any longer.

03:19.020 --> 03:21.900
But if we have a look now, we could also then say

03:21.900 --> 03:26.400
this chain.with_config, we'll pass in a configurable object,

03:26.400 --> 03:28.350
which is a prompt of poem,

03:28.350 --> 03:32.400
it's gonna change this to be write a short poem about topic.

03:32.400 --> 03:35.400
And what's happening here is then when we do the invoke,

03:35.400 --> 03:37.470
the topic is then going to be associated

03:37.470 --> 03:40.653
with write a short poem about bears.

03:41.820 --> 03:44.790
So you could easily change the different PromptTemplates

03:44.790 --> 03:49.233
based on having these extra keys that you put in here.

03:50.760 --> 03:53.370
Cool, you can also save your configurations.

03:53.370 --> 03:55.530
So if you do the chain.with_config,

03:55.530 --> 03:59.130
you can see you get a serializable JSON string,

03:59.130 --> 04:02.310
which you could also load up into an object as well.

04:02.310 --> 04:03.930
Just to have a look here as well,

04:03.930 --> 04:06.480
you can see that on LangChain's page,

04:06.480 --> 04:07.770
there's a couple of different other ways

04:07.770 --> 04:09.780
that you could use ConfigurableFields.

04:09.780 --> 04:12.360
So if you were using hubs, HubRunnables,

04:12.360 --> 04:13.770
you could use it for that.

04:13.770 --> 04:15.750
And also, there's a lot of different ways

04:15.750 --> 04:17.280
that you can optimize the LLM.

04:17.280 --> 04:18.750
So you could even say,

04:18.750 --> 04:22.050
I'm gonna make the LLM a RunnableConfigurableField.

04:22.050 --> 04:24.150
So the default one would be Anthropic,

04:24.150 --> 04:25.890
but then we'd also add on extra ones

04:25.890 --> 04:27.990
like OpenAI or GPT-4.

04:27.990 --> 04:30.900
And then you could specifically pick a chain,

04:30.900 --> 04:33.480
but then associate the LLM with different models.

04:33.480 --> 04:35.010
So your chain is actually able

04:35.010 --> 04:37.140
to run different models at runtime

04:37.140 --> 04:41.520
because you've made the model a ConfigurableField

04:41.520 --> 04:44.400
using that .configurable_alternatives method,

04:44.400 --> 04:47.520
and then adding on your ConfigurableFields.

04:47.520 --> 04:49.590
Cool, hopefully this gives you a good intro

04:49.590 --> 04:53.100
in how you can start using ConfigurableFields to optimize

04:53.100 --> 04:55.263
and do dynamic chains at runtime.
