-: In this video, we're gonna cover
something called ConfigurableFields
inside of LangChain Expression Language.
Basically, configuration fields are a way for you
to actually define specific bits of configuration
that will be invoked or ran at runtime.
Let's have a look at a couple of different examples
so you can see how you can use this
within LangChain Expression Language.
We're firstly importing a couple of different packages,
so langchain and langchain_openai.
After doing that, we set up a very simple PromptTemplate
and we've got our first thing,
which is a ConfigurableField.
Now, ConfigurableFields can be set
on multiple different things such as the prompt.
It can be done at the LLM.
It can be done at lots of different types of things.
These are the main two, though, prompts and LLMs.
If we have a look here, we've got a ConfigurableField.
We set up our chat model, which is a ChatOpenAI model,
and we have this .configurable_fields.
Notice how we set the temperature,
which we know is a ConfigurableField.
We know it's a field that we can access on ChatOpenAI.
We create a ConfigurableField and we give it a name
and a description and an id.
Now, the id is the most important point
because that's actually how you specifically change
that field on whatever it is.
So let's have an example and and see that.
So we've got this model, and if we have a look now,
if we just do the .invoke
and we say pick a random number, it'll pick a random number,
but it will be using the temperature as you can see here,
which has been set to zero.
Now, if we want to then modify that temperature at runtime,
because we've set a ConfigurableField for the temperature,
we can just do model.config, so it's model.with_config,
and then you put a configurable object in there.
Now, this is the same as or type of a RunnableConfig,
and there's lots of different things
you can add into a RunnableConfig.
Now, in this scenario,
notice we're referencing the llm_temperature,
which is the same as we have for this id field
when we create the ConfigurableField.
And we're setting the temperature here to be 0.9,
and then we are running the .invoke.
So again, it's that model.with_config function
that's then gonna allow you to change
any ConfigurableField at runtime.
You could also do this on a prompt.
So for example, we can take a chat model,
we can then specify a PromptTemplate,
and after we're doing the PromptTemplate,
we then have .configurable_alternatives.
And this shows us that we could then have a different field.
So for example, the ConfigurableField here
is the idea of prompt.
The default key will be a joke.
So that basically means that will be the default LLM.
This is the default key that will be used.
However, you could also add a new option.
So you can see here, for example, we have a poem
and this says PromptTemplate.from_template,
write a short poem.
So what's really happening here
is you've got this default prompt
that's gonna be tell me a joke about topic,
but we've also got the ability
to have a different PromptTemplate here if we use the poem.
So let's have a look at that.
So we create our runnable chain.
So you can see the prompt gets piped into the LLM,
and then after that, then we can invoke that.
So what's happening here is on this section above,
you've got tell me a joke about topic
and then we'll get the topic
which will be injected with bears.
So tell me a joke about bears
and it'll say, why did the joke
break up with his girlfriend?
Because he couldn't bear the relationship any longer.
But if we have a look now, we could also then say
this chain.with_config, we'll pass in a configurable object,
which is a prompt of poem,
it's gonna change this to be write a short poem about topic.
And what's happening here is then when we do the invoke,
the topic is then going to be associated
with write a short poem about bears.
So you could easily change the different PromptTemplates
based on having these extra keys that you put in here.
Cool, you can also save your configurations.
So if you do the chain.with_config,
you can see you get a serializable JSON string,
which you could also load up into an object as well.
Just to have a look here as well,
you can see that on LangChain's page,
there's a couple of different other ways
that you could use ConfigurableFields.
So if you were using hubs, HubRunnables,
you could use it for that.
And also, there's a lot of different ways
that you can optimize the LLM.
So you could even say,
I'm gonna make the LLM a RunnableConfigurableField.
So the default one would be Anthropic,
but then we'd also add on extra ones
like OpenAI or GPT-4.
And then you could specifically pick a chain,
but then associate the LLM with different models.
So your chain is actually able
to run different models at runtime
because you've made the model a ConfigurableField
using that .configurable_alternatives method,
and then adding on your ConfigurableFields.
Cool, hopefully this gives you a good intro
in how you can start using ConfigurableFields to optimize
and do dynamic chains at runtime.