06. Dynamic attribute assignment (configurable_fields, configurable_alternatives)
Configuring inside the chain at runtime
This tutorial will show you how you can dynamically set various options when calling Chain.
Dynamic configuration can be done in the following two ways.
first,
configurable_fieldsThis is a method. This method allows you to configure specific fields of executable objects.second,
configurable_alternativesThis is a method. This method allows you to list alternatives to specific executable objects that can be set during runtime.
configurable_fields
configurable_fields means the field that defines the set value of the system.
Dynamic attribute assignment
ChatOpenAI When using, we model_name You can adjust the same settings as.
model_name Is a property used to specify the version of GPT. For example, gpt-4o , gpt-4o-mini You can select a model by setting the back.
If fixed model_name When you want to dynamically designate a model rather than: ConfigurableField You can use to convert to property values that can be set dynamically.
Copy
# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv
# Load API key information
load_dotenv()Copy
configurable_fields Using methods model_name Specifies properties as dynamic configurable fields.
Copy
Copy
Copy
model.invoke() On call config={"configurable": {"키": "값"}} You can dynamic it in format.
Copy
Copy
this time gpt-4o-mini I will try using the model. Check out the model that changed to the output.
Copy
Copy
model Object with_config() Using methods configurable You can also set parameters. The way it works with the previous one is the same.
Copy
Copy
You can also use this function in the same way when using it as part of a chain.
Copy
Copy
Copy
Copy
Copy
HubRunnable: Change the settings of LangChain Hub
HubRunnable Using facilitates the conversion of prompts registered in Hub.
Copy
Copy
Copy
Separately with_config Without designation prompt.invoke() When I call the method, I set it up first "rlm/rag-prompt" Pull the registered prompt on the hub.
Copy
Copy
Copy
Copy
Configurable Alternatives: Alternative setting of the Runnable object itself
It constitutes an alternative to Runnable that can be set at runtime.
Configurable alternatives
ChatAnthropic The configurable language model of gives you the flexibility to apply to a variety of tasks and contexts.
Set the parameters you set for the model to a ConfigurableField object to change the Config value dynamically.
model: Specifies the default language model to use.temperature: Values between 0 and 1, controlling the randomness of sampling. The lower the value, the more decisive and repetitive the output, and the higher the value, the more diverse and creative the output.
How to set up alternatives for LLM objects
Let's take a look at how to do this using the Large Language Model (LLM).
[Note]
ChatAnthropicAPI KEY must be issued and set to use the model.Link: https://console.anthropic.com/dashboard
Uncomment below and set API KEY,
.envSet to file.
ANTHROPIC_API_KEY Set environmental variables.
Copy
Copy
chain.invoke() The method is the default LLM ChatAnthropic Call the chain using.
Copy
Copy
chain.with_config(configurable={"llm": "model"}) Use llm You can specify different models.
Copy
Copy
Change the settings of the chain to use the language model gpt4o Specify as.
Copy
Copy
Change the settings of the chain to use the language model anthropic Specify as.
Copy
Copy
How to set an alternative for prompts
Prompts can also do something similar to the previous LLM alternative setup method.
Copy
If there are no settings changes, the default prompt is entered.
Copy
Copy
with_config Call another prompt.
Copy
Copy
Copy
Copy
this time eng Use prompts to request translation. The input variable to pass at this time input is.
Copy
Copy
Change all prompt & LLM
You can configure several things using prompts and LLMs.
Here is an example of doing this using both prompt and LLM.
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Save settings
You can easily save the configured chain as a separate object. For example, after configuring a customized chain for a specific task, you can easily utilize it in similar tasks in the future by saving it as a reusable object.
Copy
Copy
Copy
Last updated