The D3 notebooks will cover the essential topics of prompt engineering, beginning with inference in general and an introduction to LangChain. We will then cover the topics of prompt templates and parsing and will then go on to the concept of creating chains and connecting these in different ways to build more sophisticated constructs to make the most of LLMs.
The D3 notebooks will cover the essential topics of prompt engineering, beginning with inference in general and an introduction to LangChain. We will then cover the topics of prompt templates and parsing and will then go on to the concept of creating chains and connecting these in different ways to build more sophisticated constructs to make the most of LLMs.
Using the an API-hosted LLM (e.g. OpenAI) is like renting a powerful car — it’s ready to go, but you mustn't tinker with the inner workings of the engine and you pay each time you drive.
Using the an API-hosted LLM (e.g. OpenAI) is like renting a powerful car — it’s ready to go, but you mustn't tinker with the inner workings of the engine and you pay each time you drive.
Using a locally hosted model is like buying your own vehicle — more upfront work and maintenance, but full control, privacy, and no cost per use, apart from footing the energy bill.
Using a locally hosted model is like buying your own vehicle — more upfront work and maintenance, but full control, privacy, and no cost per use, apart from footing the energy bill.
| **Setup time** | Minimal – just an API key | Requires downloading and managing the model |
| **Setup time** | Minimal – just an API key | Requires downloading and managing the model |
| **Hardware requirement** | None (runs in the cloud) | Requires a GPU (sometimes large memory) |
| **Hardware requirement** | None (runs in the cloud) | Requires a GPU (sometimes large memory) |
| **Latency** | Network-dependent | Faster inference (once model is loaded) |
| **Latency** | Network-dependent | Faster inference (once model is loaded) |
| **Privacy / Data control**| Data sent to external servers | Data stays on your infrastructure |
| **Privacy / Data control**| Data sent to external servers | Data stays on your infrastructure |
| **Cost** | Pay-per-use (based on tokens) | Free at inference (after download), but uses your compute |
| **Cost** | Pay-per-use (based on tokens) | Free at inference (after download), but uses your compute |
| **Scalability** | Handled by provider | You manage and scale infrastructure |
| **Scalability** | Handled by provider | You manage and scale infrastructure |
| **Flexibility** | Limited to provider's models and settings | Full control: quantization, fine-tuning, prompt handling |
| **Flexibility** | Limited to provider's models and settings | Full control: quantization, fine-tuning, prompt handling |
| **Offline use** | Not possible | Yes, after initial download |
| **Offline use** | Not possible | Yes, after initial download |
| **Customizability** | No access to internals | You can modify and extend anything |
| **Customizability** | No access to internals | You can modify and extend anything |
**Using an API (e.g. OpenAI)**<br>
**Using an API (e.g. OpenAI)**<br>
- You use OpenAI or ChatOpenAI class from LangChain
- You use OpenAI or ChatOpenAI class from LangChain
- LangChain sends your prompt to api.openai.com
- LangChain sends your prompt to api.openai.com
- You don’t manage the model, only the request and response
- You don’t manage the model, only the request and response
```
```
from langchain.chat_models import ChatOpenAI
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(api_key="...", model="gpt-4")
llm = ChatOpenAI(api_key="...", model="gpt-4")
response = llm.invoke("Summarize this legal clause...")
response = llm.invoke("Summarize this legal clause...")
```
```
📝 You can store your API key in different ways, it is common, however, to set it as an **environment variable**.
📝 You can store your API key in different ways, it is common, however, to set it as an **environment variable**.
Note, that LangChain automatically looks up any environment variable with the name **`OPENAI_API_KEY`** automatically when making a connection to OpenAI.
Note, that LangChain automatically looks up any environment variable with the name **`OPENAI_API_KEY`** automatically when making a connection to OpenAI.
Alternatively, you could just pass in the openai key via a string (not very secure, you should NEVER hard-code your API keys), or even just save it somewhere on your computer in a text file and then read it in:
Alternatively, you could just pass in the openai key via a string (not very secure, you should NEVER hard-code your API keys), or even just save it somewhere on your computer in a text file and then read it in:
```
```
f = open('C:\\Users\\Simeon\\Desktop\\openai.txt')
f = open('C:\\Users\\Simeon\\Desktop\\openai.txt')
api_key = f.read()
api_key = f.read()
llm = OpenAI(openai_api_key=api_key)
llm = OpenAI(openai_api_key=api_key)
```
```
**Using a Local Model (e.g. Mistral, LLaMA)**<br>
**Using a Local Model (e.g. Mistral, LLaMA)**<br>
- You load the model and tokenizer using Hugging Face Transformers
- You load the model and tokenizer using Hugging Face Transformers
- You wrap the pipeline using HuggingFacePipeline or similar in LangChain
- You wrap the pipeline using HuggingFacePipeline or similar in LangChain
- You manage memory, GPU allocation, quantization, etc.
- You manage memory, GPU allocation, quantization, etc.
```
```
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import AutoModelForCausalLM, AutoTokenizer
Apart from the usual suspects of Pytorch and Huggingface libraries, we get our first imports of the LangChain library and some of its classes.
Apart from the usual suspects of Pytorch and Huggingface libraries, we get our first imports of the LangChain library and some of its classes.
Since we want to show you how to how to work with LLMs that are not part of the closed OpenAI and Anthropic world, we are going to show you how to work with open and downloadable models. As it makes no sense for all of us to download the models and store them in our home directory, we've done that for your before the start of the course. You can find the path to the models down below.
Since we want to show you how to how to work with LLMs that are not part of the closed OpenAI and Anthropic world, we are going to show you how to work with open and downloadable models. As it makes no sense for all of us to download the models and store them in our home directory, we've done that for your before the start of the course. You can find the path to the models down below.
If you choose to work with a model such as `meta-llama/Llama-3.3-70B-Instruct`, you will have to use quantization in order to get the model into the memory of one GPU. It is advisable to utilise BitsAndBytes for qantization and write a short config for that, e.g.:
If you choose to work with a model such as `meta-llama/Llama-3.3-70B-Instruct`, you will have to use quantization in order to get the model into the memory of one GPU. It is advisable to utilise BitsAndBytes for qantization and write a short config for that, e.g.:
```
```
# Define quantization config
# Define quantization config
quantization_config = BitsAndBytesConfig(
quantization_config = BitsAndBytesConfig(
load_in_4bit=True, # Enable 4-bit quantization
load_in_4bit=True, # Enable 4-bit quantization
bnb_4bit_compute_dtype=torch.float16, # Use float16 for computation
bnb_4bit_compute_dtype=torch.float16, # Use float16 for computation
bnb_4bit_use_double_quant=True # Double quantization for efficiency
bnb_4bit_use_double_quant=True # Double quantization for efficiency
)
)
```
```
However, beware, a model of that size takes roughly 30 minutes to load...
However, beware, a model of that size takes roughly 30 minutes to load...
In this course we do not want to wait around for that long, so we will use a smaller model called [Nous-Hermes-2-Mistral-7B-DPO](https://huggingface.co/NousResearch/Nous-Hermes-2-Mistral-7B-DPO).
In this course we do not want to wait around for that long, so we will use a smaller model called [Nous-Hermes-2-Mistral-7B-DPO](https://huggingface.co/NousResearch/Nous-Hermes-2-Mistral-7B-DPO).
Setting `pad_token_id` to `eos_token_id`:32000 for open-end generation.
Setting `pad_token_id` to `eos_token_id`:32000 for open-end generation.
What is the capital of France? Can you give me some facts about it?
What is the capital of France? Can you give me some facts about it?
The capital of France is Paris. Paris is the largest city in France and is located in the northern part of the country. It is situated on the Seine River and is known for its beautiful architecture, art, and culture.
The capital of France is Paris. Paris is the largest city in France and is located in the northern part of the country. It is situated on the Seine River and is known for its beautiful architecture, art, and culture.
Here are some interesting facts about Paris:
Here are some interesting facts about Paris:
1. Paris is home to some of the world’s most famous landmarks, including the Eiffel Tower, the Louvre Museum, and the Notre-Dame Cathedral.
1. Paris is home to some of the world’s most famous landmarks, including the Eiffel Tower, the Louvre Museum, and the Notre-Dame Cathedral.
2. The city is often referred to as the “City of Light” due to its role in the Age of Enlightenment and its status as a major center of education and ideas.
2. The city is often referred to as the “City of Light” due to its role in the Age of Enlightenment and its status as a major center of education and ideas.
3. Paris is known for its fashion industry and is home to some of the world’s most famous designers and fashion houses.
3. Paris is known for its fashion industry and is home to some of the world’s most famous designers and fashion houses.
4. The city is also famous for its cuisine, with dishes such as croissants, escargot, and macarons originating in France.
4. The city is also famous for its cuisine, with dishes such as croissants, escargot, and macarons originating in France.
5. Paris is a major transportation hub, with an extensive network of buses, trains, and subways that connect the city to the rest of France and Europe.
5. Paris is a major transportation hub, with an extensive network of buses, trains, and subways that connect the city to the rest of France and Europe.
[LangChain](https://www.langchain.com/) is a powerful open-source framework designed to help developers build applications using LLMs. It abstracts and simplifies common LLM tasks like prompt engineering, chaining multiple steps, retrieving documents, parsing structured output, and building conversational agents.
[LangChain](https://www.langchain.com/) is a powerful open-source framework designed to help developers build applications using LLMs. It abstracts and simplifies common LLM tasks like prompt engineering, chaining multiple steps, retrieving documents, parsing structured output, and building conversational agents.
LangChain supports a wide range of models (OpenAI, Hugging Face, Cohere, Anthropic, etc.) and integrates seamlessly with tools like vector databases, APIs, file loaders, and output parsers.
LangChain supports a wide range of models (OpenAI, Hugging Face, Cohere, Anthropic, etc.) and integrates seamlessly with tools like vector databases, APIs, file loaders, and output parsers.
| `invoke()` | Handles a **single input**, returns one response | `str` or `Message(s)` | `str` / `AIMessage` |
| `invoke()` | Handles a **single input**, returns one response | `str` or `Message(s)` | `str` / `AIMessage` |
| `generate()` | Handles a **batch of inputs**, returns multiple outputs | `list[str]` | `LLMResult` |
| `generate()` | Handles a **batch of inputs**, returns multiple outputs | `list[str]` | `LLMResult` |
| `batch()` | Batched input, returns a flat list of outputs | `list[str]` | `list[str]` / Messages |
| `batch()` | Batched input, returns a flat list of outputs | `list[str]` | `list[str]` / Messages |
| `stream()` | Streams the output as tokens are generated | `str` / `Message(s)` | Generator (streamed text)|
| `stream()` | Streams the output as tokens are generated | `str` / `Message(s)` | Generator (streamed text)|
| `ainvoke()` | Async version of `invoke()` | `str` / `Message(s)` | Awaitable result |
| `ainvoke()` | Async version of `invoke()` | `str` / `Message(s)` | Awaitable result |
| `agenerate()` | Async version of `generate()` | `list[str]` | Awaitable result |
| `agenerate()` | Async version of `generate()` | `list[str]` | Awaitable result |
Before we use one of these methods, we need to create a pipeline and apply the LangChain wrapper to the pipeline, so we create a format that LangChain can call with .invoke() or .generate() etc. If we use an remotly hosted LLM, which we access through an API, we do not need the pipeline.
Before we use one of these methods, we need to create a pipeline and apply the LangChain wrapper to the pipeline, so we create a format that LangChain can call with .invoke() or .generate() etc. If we use an remotly hosted LLM, which we access through an API, we do not need the pipeline.
print(llm.invoke('Here is a fun fact about Mars:'))
print(llm.invoke('Here is a fun fact about Mars:'))
```
```
%% Output
%% Output
Here is a fun fact about Mars: it has the largest volcano in our solar system. It’s called Olympus Mons, and it’s so big that if it were on Earth, it would stretch from New York City to Denver.
Here is a fun fact about Mars: it has the largest volcano in our solar system. It’s called Olympus Mons, and it’s so big that if it were on Earth, it would stretch from New York City to Denver.
But what’s even more amazing is that Olympus Mons is a shield volcano, which means it was formed by slow-moving lava flows. And those lava flows were so thick that they built up over time, creating a mountain that’s three times taller than Mount Everest.
But what’s even more amazing is that Olympus Mons is a shield volcano, which means it was formed by slow-moving lava flows. And those lava flows were so thick that they built up over time, creating a mountain that’s three times taller than Mount Everest.
So how did Olympus Mons get so big? Well, Mars has a thin atmosphere, which means that the pressure inside its volcanoes is much lower than on Earth. This allows the l
So how did Olympus Mons get so big? Well, Mars has a thin atmosphere, which means that the pressure inside its volcanoes is much lower than on Earth. This allows the l
results=llm.batch(["Tell me a joke","Translate this to German: It has been raining non-stop today."])
results=llm.batch(["Tell me a joke","Translate this to German: It has been raining non-stop today."])
print(results)
print(results)
```
```
%% Output
%% Output
['Tell me a joke.\n\nI’ll tell you a joke. Why don’t scientists trust atoms? Because they make up everything.\n\nWhat’s the difference between a well-dressed man on a tricycle and a poorly dressed man on a bicycle? Attire.\n\nWhat did the fish say when he hit the wall? Dam.\n\nWhat do you call a fake noodle? An impasta.\n\nWhat do you call a bear with no teeth? A gummy bear.\n\nWhat do you call a boomerang that doesn’t come back? A stick.\n\nWhat do you call a fake noodle that doesn’t work? An impasta-fail.\n\n', 'Translate this to German: It has been raining non-stop today.\n\n# German Translation\n\nheute hat es ununterbrochen geregnet.\n\nLearn German with us! Additionally, our language school in Berlin can help you improve your German skills.\n\nGerman (Deutsch) is a West Germanic language that is mainly spoken in Central Europe. It is the most widely spoken and official or co-official language in Germany, Austria, Switzerland, South Tyrol in Italy, the German-speaking Community of Belgium, and Liechtenstein. It is one of the three official languages of Luxembourg and a co-official language in the Opole Voivodeship in Poland. The languages which are most similar to German are the other members of the']
['Tell me a joke.\n\nI’ll tell you a joke. Why don’t scientists trust atoms? Because they make up everything.\n\nWhat’s the difference between a well-dressed man on a tricycle and a poorly dressed man on a bicycle? Attire.\n\nWhat did the fish say when he hit the wall? Dam.\n\nWhat do you call a fake noodle? An impasta.\n\nWhat do you call a bear with no teeth? A gummy bear.\n\nWhat do you call a boomerang that doesn’t come back? A stick.\n\nWhat do you call a fake noodle that doesn’t work? An impasta-fail.\n\n', 'Translate this to German: It has been raining non-stop today.\n\n# German Translation\n\nheute hat es ununterbrochen geregnet.\n\nLearn German with us! Additionally, our language school in Berlin can help you improve your German skills.\n\nGerman (Deutsch) is a West Germanic language that is mainly spoken in Central Europe. It is the most widely spoken and official or co-official language in Germany, Austria, Switzerland, South Tyrol in Italy, the German-speaking Community of Belgium, and Liechtenstein. It is one of the three official languages of Luxembourg and a co-official language in the Opole Voivodeship in Poland. The languages which are most similar to German are the other members of the']
I’ll tell you a joke. Why don’t scientists trust atoms? Because they make up everything.
I’ll tell you a joke. Why don’t scientists trust atoms? Because they make up everything.
What’s the difference between a well-dressed man on a tricycle and a poorly dressed man on a bicycle? Attire.
What’s the difference between a well-dressed man on a tricycle and a poorly dressed man on a bicycle? Attire.
What did the fish say when he hit the wall? Dam.
What did the fish say when he hit the wall? Dam.
What do you call a fake noodle? An impasta.
What do you call a fake noodle? An impasta.
What do you call a bear with no teeth? A gummy bear.
What do you call a bear with no teeth? A gummy bear.
What do you call a boomerang that doesn’t come back? A stick.
What do you call a boomerang that doesn’t come back? A stick.
What do you call a fake noodle that doesn’t work? An impasta-fail.
What do you call a fake noodle that doesn’t work? An impasta-fail.
Prompt 2: Translate this to German: 'It has been raining non-stop today.'
Prompt 2: Translate this to German: 'It has been raining non-stop today.'
Response:
Response:
Translate this to German: 'It has been raining non-stop today.'
Translate this to German: 'It has been raining non-stop today.'
The German translation for 'It has been raining non-stop today' is 'Es hat heute ununterbrochen geregnet.'
The German translation for 'It has been raining non-stop today' is 'Es hat heute ununterbrochen geregnet.'
In this sentence, 'Es' means 'it', 'hat' means 'has', 'heute' means 'today', 'ununterbrochen' means 'non-stop' or 'uninterrupted', and 'geregnet' means 'rained'.
In this sentence, 'Es' means 'it', 'hat' means 'has', 'heute' means 'today', 'ununterbrochen' means 'non-stop' or 'uninterrupted', and 'geregnet' means 'rained'.
So, the sentence structure in German is similar to English, with the subject 'it' followed by the verb 'has' and the adverb 'today', and then the main verb 'rained' with the adverb 'non-stop' placed before it.
So, the sentence structure in German is similar to English, with the subject 'it' followed by the verb 'has' and the adverb 'today', and then the main verb 'rained' with the adverb 'non-stop' placed before it.
results=llm.generate(["Where should my customer go for a luxurious Safari?",
results=llm.generate(["Where should my customer go for a luxurious Safari?",
"What are your top three suggestions for backpacking destinations?"])
"What are your top three suggestions for backpacking destinations?"])
print(results)
print(results)
```
```
%% Output
%% Output
generations=[[Generation(text='Where should my customer go for a luxurious Safari?\n\nIf your customer is looking for a luxurious safari experience, they should consider going to Africa. Africa is home to some of the most luxurious safari lodges and camps, offering guests an unforgettable experience in the heart of the wilderness.\n\nSome of the top destinations for a luxurious safari in Africa include:\n\n1. Singita Grumeti in Tanzania: This luxury safari camp offers guests the chance to experience the wonders of the Serengeti in style. The camp features spacious suites with private plunge pools, outdoor showers, and stunning views of the surrounding wilderness.\n\n2. &Beyond Sandibe Okavango')], [Generation(text='What are your top three suggestions for backpacking destinations?\n\n1. The Pacific Crest Trail: This 2,650-mile trail stretches from Mexico to Canada and offers some of the most stunning views in the world. From the desert landscapes of Southern California to the snow-capped peaks of the Sierra Nevada, this trail has something for everyone.\n\n2. The Appalachian Trail: This 2,200-mile trail runs from Georgia to Maine and is one of the most popular backpacking destinations in the United States. With its diverse terrain and abundant wildlife, the Appalachian Trail offers a unique and unforgettable backpacking experience.\n\n3. The Inca Trail: This 26-mile trail')]] llm_output=None run=[RunInfo(run_id=UUID('ffcd266d-4c4f-4229-b404-b3445dd89d6f')), RunInfo(run_id=UUID('b15527e8-b2b0-409b-a554-16b7d649e241'))] type='LLMResult'
generations=[[Generation(text='Where should my customer go for a luxurious Safari?\n\nIf your customer is looking for a luxurious safari experience, they should consider going to Africa. Africa is home to some of the most luxurious safari lodges and camps, offering guests an unforgettable experience in the heart of the wilderness.\n\nSome of the top destinations for a luxurious safari in Africa include:\n\n1. Singita Grumeti in Tanzania: This luxury safari camp offers guests the chance to experience the wonders of the Serengeti in style. The camp features spacious suites with private plunge pools, outdoor showers, and stunning views of the surrounding wilderness.\n\n2. &Beyond Sandibe Okavango')], [Generation(text='What are your top three suggestions for backpacking destinations?\n\n1. The Pacific Crest Trail: This 2,650-mile trail stretches from Mexico to Canada and offers some of the most stunning views in the world. From the desert landscapes of Southern California to the snow-capped peaks of the Sierra Nevada, this trail has something for everyone.\n\n2. The Appalachian Trail: This 2,200-mile trail runs from Georgia to Maine and is one of the most popular backpacking destinations in the United States. With its diverse terrain and abundant wildlife, the Appalachian Trail offers a unique and unforgettable backpacking experience.\n\n3. The Inca Trail: This 26-mile trail')]] llm_output=None run=[RunInfo(run_id=UUID('ffcd266d-4c4f-4229-b404-b3445dd89d6f')), RunInfo(run_id=UUID('b15527e8-b2b0-409b-a554-16b7d649e241'))] type='LLMResult'
Where should my customer go for a luxurious Safari?
Where should my customer go for a luxurious Safari?
If your customer is looking for a luxurious safari experience, they should consider going to Africa. Africa is home to some of the most luxurious safari lodges and camps, offering guests an unforgettable experience in the heart of the wilderness.
If your customer is looking for a luxurious safari experience, they should consider going to Africa. Africa is home to some of the most luxurious safari lodges and camps, offering guests an unforgettable experience in the heart of the wilderness.
Some of the top destinations for a luxurious safari in Africa include:
Some of the top destinations for a luxurious safari in Africa include:
1. Singita Grumeti in Tanzania: This luxury safari camp offers guests the chance to experience the wonders of the Serengeti in style. The camp features spacious suites with private plunge pools, outdoor showers, and stunning views of the surrounding wilderness.
1. Singita Grumeti in Tanzania: This luxury safari camp offers guests the chance to experience the wonders of the Serengeti in style. The camp features spacious suites with private plunge pools, outdoor showers, and stunning views of the surrounding wilderness.
2. &Beyond Sandibe Okavango
2. &Beyond Sandibe Okavango
What are your top three suggestions for backpacking destinations?
What are your top three suggestions for backpacking destinations?
1. The Pacific Crest Trail: This 2,650-mile trail stretches from Mexico to Canada and offers some of the most stunning views in the world. From the desert landscapes of Southern California to the snow-capped peaks of the Sierra Nevada, this trail has something for everyone.
1. The Pacific Crest Trail: This 2,650-mile trail stretches from Mexico to Canada and offers some of the most stunning views in the world. From the desert landscapes of Southern California to the snow-capped peaks of the Sierra Nevada, this trail has something for everyone.
2. The Appalachian Trail: This 2,200-mile trail runs from Georgia to Maine and is one of the most popular backpacking destinations in the United States. With its diverse terrain and abundant wildlife, the Appalachian Trail offers a unique and unforgettable backpacking experience.
2. The Appalachian Trail: This 2,200-mile trail runs from Georgia to Maine and is one of the most popular backpacking destinations in the United States. With its diverse terrain and abundant wildlife, the Appalachian Trail offers a unique and unforgettable backpacking experience.
forchunkinllm.stream("Tell me a story about a cat."):
forchunkinllm.stream("Tell me a story about a cat."):
print(chunk,end="")
print(chunk,end="")
```
```
%% Output
%% Output
Once upon a time, there was a cat named Whiskers. Whiskers was a beautiful black and white cat with bright green eyes. She lived in a small house with her owner, Mrs. Johnson. Mrs. Johnson was an old lady who lived alone, and Whiskers was her only companion.
Once upon a time, there was a cat named Whiskers. Whiskers was a beautiful black and white cat with bright green eyes. She lived in a small house with her owner, Mrs. Johnson. Mrs. Johnson was an old lady who lived alone, and Whiskers was her only companion.
One day, Mrs. Johnson went out to run some errands, and when she returned, she found that Whiskers was missing. She searched the entire house, but couldn't find her anywhere. She put up posters around the neighborhood and asked everyone she met if they had seen her cat.
One day, Mrs. Johnson went out to run some errands, and when she returned, she found that Whiskers was missing. She searched the entire house, but couldn't find her anywhere. She put up posters around the neighborhood and asked everyone she met if they had seen her cat.
Days went by, and Mrs. Johnson was starting to lose hope of ever seeing her beloved
Days went by, and Mrs. Johnson was starting to lose hope of ever seeing her beloved
| **LLMs** | Models that take a plain text string as input and return generated text | GPT-2, Falcon, LLaMA, Mistral (raw) |
| **LLMs** | Models that take a plain text string as input and return generated text | GPT-2, Falcon, LLaMA, Mistral (raw) |
| **Chat Models**| Models that work with structured chat messages (system, user, assistant) | GPT-4, Claude, LLaMA-Instruct, Mistral-Instruct|
| **Chat Models**| Models that work with structured chat messages (system, user, assistant) | GPT-4, Claude, LLaMA-Instruct, Mistral-Instruct|
---
---
**Why the distinction?**
**Why the distinction?**
Chat models are designed to understand multi-turn conversation and role-based prompting. Their input format includes a structured message history, making them ideal for:
Chat models are designed to understand multi-turn conversation and role-based prompting. Their input format includes a structured message history, making them ideal for:
- Instruction following
- Instruction following
- Contextual reasoning
- Contextual reasoning
- Assistant-like behavior
- Assistant-like behavior
LLMs, on the other hand, expect a single flat prompt string. They still power many applications and are worth understanding, especially when using older models, doing fine-tuning, or debugging at the token level.
LLMs, on the other hand, expect a single flat prompt string. They still power many applications and are worth understanding, especially when using older models, doing fine-tuning, or debugging at the token level.
---
---
**Do Chat Models matter more now?**
**Do Chat Models matter more now?**
Yes — most modern instruction-tuned models (like GPT-4, Claude, Mistral-Instruct, or LLaMA-3-Instruct) are designed as chat models, and LangChain's agent and memory systems are built around them.
Yes — most modern instruction-tuned models (like GPT-4, Claude, Mistral-Instruct, or LLaMA-3-Instruct) are designed as chat models, and LangChain's agent and memory systems are built around them.
However, LLMs are still important:
However, LLMs are still important:
- Some models only support the LLM interface
- Some models only support the LLM interface
- LLMs are useful in batch processing and structured generation
- LLMs are useful in batch processing and structured generation
- Understanding their behavior helps you build better prompts
- Understanding their behavior helps you build better prompts
print(llm.invoke("Explain LangChain in one sentence."))
print(llm.invoke("Explain LangChain in one sentence."))
# Use as a ChatModel (structured messages)
# Use as a ChatModel (structured messages)
chat_llm=ChatHuggingFace(llm=llm)
chat_llm=ChatHuggingFace(llm=llm)
messages=[
messages=[
SystemMessage(content="You are a helpful AI assistant."),
SystemMessage(content="You are a helpful AI assistant."),
HumanMessage(content="Explain LangChain in one sentence.")
HumanMessage(content="Explain LangChain in one sentence.")
]
]
print("\n--- Chat-style output ---\n")
print("\n--- Chat-style output ---\n")
print(chat_llm.invoke(messages).content)
print(chat_llm.invoke(messages).content)
```
```
%% Output
%% Output
--- LLM-style output ---
--- LLM-style output ---
Explain LangChain in one sentence.
Explain LangChain in one sentence.
LangChain is a framework for building AI language models that can understand and generate human-like text.
LangChain is a framework for building AI language models that can understand and generate human-like text.
What is the purpose of LangChain?
What is the purpose of LangChain?
The purpose of LangChain is to provide developers with a powerful toolkit for building AI language models that can be used in a variety of applications, such as chatbots, language translation, text summarization, and more.
The purpose of LangChain is to provide developers with a powerful toolkit for building AI language models that can be used in a variety of applications, such as chatbots, language translation, text summarization, and more.
What are the key features of LangChain?
What are the key features of LangChain?
LangChain has several key features that make it a powerful tool for building AI language models. These include:
LangChain has several key features that make it a powerful tool for building AI language models. These include:
1. Modular architecture: LangChain is designed to be modular, allowing developers to easily add new components and customize existing ones to suit their specific needs.
1. Modular architecture: LangChain is designed to be modular, allowing developers to easily add new components and customize existing ones to suit their specific needs.
--- Chat-style output ---
--- Chat-style output ---
<s><|im_start|>system
<s><|im_start|>system
You are a helpful AI assistant.<|im_end|>
You are a helpful AI assistant.<|im_end|>
<|im_start|>user
<|im_start|>user
Explain LangChain in one sentence.<|im_end|>
Explain LangChain in one sentence.<|im_end|>
<|im_start|>assistant
<|im_start|>assistant
LangChain is a framework that enables developers to build and integrate natural language processing (NLP) and conversational AI models into their applications, allowing for more efficient and effective communication between humans and machines.
LangChain is a framework that enables developers to build and integrate natural language processing (NLP) and conversational AI models into their applications, allowing for more efficient and effective communication between humans and machines.
The raw output you're seeing includes special chat formatting tokens (like <|im_start|>, <|im_end|>, etc.) which are used internally by the model (e.g., Mistral, LLaMA, GPT-J-style models) to distinguish between roles in a chat.
The raw output you're seeing includes special chat formatting tokens (like <|im_start|>, <|im_end|>, etc.) which are used internally by the model (e.g., Mistral, LLaMA, GPT-J-style models) to distinguish between roles in a chat.
These tokens help the model understand who is speaking, but they're not intended for humans to see. <br>
These tokens help the model understand who is speaking, but they're not intended for humans to see. <br>
<br>
<br>
So, to prettyfy the ouput we will define a function:
So, to prettyfy the ouput we will define a function:
LangChain is a framework that enables developers to build and integrate natural language processing (NLP) and conversational AI models into their applications, allowing for more efficient and effective communication between humans and machines.
LangChain is a framework that enables developers to build and integrate natural language processing (NLP) and conversational AI models into their applications, allowing for more efficient and effective communication between humans and machines.
You are not alone. Until recently, LangChain had a different wrapper for LLMs and Chat Models, but in recent versions of LangChain, the HuggingFacePipeline class implements the ChatModel interface under the hood — it can accept structured chat messages (SystemMessage, HumanMessage, etc.) even though it wasn't originally designed to.
You are not alone. Until recently, LangChain had a different wrapper for LLMs and Chat Models, but in recent versions of LangChain, the HuggingFacePipeline class implements the ChatModel interface under the hood — it can accept structured chat messages (SystemMessage, HumanMessage, etc.) even though it wasn't originally designed to.
So yes:
So yes:
You can now do:
You can now do:
```
```
llm = HuggingFacePipeline(pipeline=text_pipe)
llm = HuggingFacePipeline(pipeline=text_pipe)
response = llm.invoke([
response = llm.invoke([
SystemMessage(content="You are a helpful legal assistant."),
SystemMessage(content="You are a helpful legal assistant."),
HumanMessage(content="Simplify this clause: ...")
HumanMessage(content="Simplify this clause: ...")
])
])
```
```
Even though you're not explicitly using ChatHuggingFace, LangChain detects the message types and processes them correctly using the underlying text-generation model.
Even though you're not explicitly using ChatHuggingFace, LangChain detects the message types and processes them correctly using the underlying text-generation model.
<br>
<br>
<br>
<br>
The same would apply if you used a remotly hosted LLM/Chat Model through an API:
The same would apply if you used a remotly hosted LLM/Chat Model through an API:
```
```
from langchain_openai import ChatOpenAI
from langchain_openai import ChatOpenAI
chat = ChatOpenAI(openai_api_key=api_key)
chat = ChatOpenAI(openai_api_key=api_key)
result = chat.invoke([HumanMessage(content="Can you tell me a fact about Dolphins?")])
result = chat.invoke([HumanMessage(content="Can you tell me a fact about Dolphins?")])
AIMessage(content='<s><|im_start|>user\nCan you tell me a fact about dolphins?<|im_end|>\n<|im_start|>assistant\nDolphins are highly intelligent marine mammals and are known for their playful and social behavior. They are part of the family Delphinidae, which includes around 40 species. Dolphins are air-breathing, have a streamlined body shape, two limbs modified into flippers, and a dorsal fin. They use echolocation to navigate and find prey, and they communicate with each other using a variety of clicks, whistles, and body movements.', additional_kwargs={}, response_metadata={}, id='run-9455ac84-a668-43aa-8569-077637312649-0')
AIMessage(content='<s><|im_start|>user\nCan you tell me a fact about dolphins?<|im_end|>\n<|im_start|>assistant\nDolphins are highly intelligent marine mammals and are known for their playful and social behavior. They are part of the family Delphinidae, which includes around 40 species. Dolphins are air-breathing, have a streamlined body shape, two limbs modified into flippers, and a dorsal fin. They use echolocation to navigate and find prey, and they communicate with each other using a variety of clicks, whistles, and body movements.', additional_kwargs={}, response_metadata={}, id='run-9455ac84-a668-43aa-8569-077637312649-0')
Dolphins are highly intelligent marine mammals and are known for their playful and social behavior. They are part of the family Delphinidae, which includes around 40 species. Dolphins are air-breathing, have a streamlined body shape, two limbs modified into flippers, and a dorsal fin. They use echolocation to navigate and find prey, and they communicate with each other using a variety of clicks, whistles, and body movements.
Dolphins are highly intelligent marine mammals and are known for their playful and social behavior. They are part of the family Delphinidae, which includes around 40 species. Dolphins are air-breathing, have a streamlined body shape, two limbs modified into flippers, and a dorsal fin. They use echolocation to navigate and find prey, and they communicate with each other using a variety of clicks, whistles, and body movements.
Dolphins are highly intelligent marine mammals that are known for their playful and social behavior. Did you know that dolphins have a complex communication system using a series of clicks, whistles, and body movements to communicate with each other? They can also recognize themselves in a mirror, which is a sign of self-awareness.
Dolphins are highly intelligent marine mammals that are known for their playful and social behavior. Did you know that dolphins have a complex communication system using a series of clicks, whistles, and body movements to communicate with each other? They can also recognize themselves in a mirror, which is a sign of self-awareness.
Dolphins are highly intelligent marine mammals that are known for their playful and social behavior. Did you know that dolphins have a complex communication system using a series of clicks, whistles, and body movements to communicate with each other? They can also recognize themselves in a mirror, which is a sign of self-awareness.
Dolphins are highly intelligent marine mammals that are known for their playful and social behavior. Did you know that dolphins have a complex communication system using a series of clicks, whistles, and body movements to communicate with each other? They can also recognize themselves in a mirror, which is a sign of self-awareness.
Prompt 2:
Prompt 2:
Whales and dolphins are both marine mammals, but they belong to different families. Whales are part of the family Cetacea, which includes toothed whales and baleen whales, while dolphins are part of the family Delphinidae. Here are some key differences between whales and dolphins:
Whales and dolphins are both marine mammals, but they belong to different families. Whales are part of the family Cetacea, which includes toothed whales and baleen whales, while dolphins are part of the family Delphinidae. Here are some key differences between whales and dolphins:
1. Physical characteristics: Whales are generally larger than dolphins, with some species of whales reaching lengths of over 100 feet. Dolphins, on the other hand, are smaller, with the largest species, the killer whale, reaching lengths of up to 32 feet. Whales also have a more streamlined body shape, while dol
1. Physical characteristics: Whales are generally larger than dolphins, with some species of whales reaching lengths of over 100 feet. Dolphins, on the other hand, are smaller, with the largest species, the killer whale, reaching lengths of up to 32 feet. Whales also have a more streamlined body shape, while dol
Dolphins are highly intelligent marine mammals that are known for their playful and social behavior. Did you know that dolphins have a complex communication system using a series of clicks, whistles, and body movements to communicate with each other? They can also recognize themselves in a mirror, which is a sign of self-awareness.
Dolphins are highly intelligent marine mammals that are known for their playful and social behavior. Did you know that dolphins have a complex communication system using a series of clicks, whistles, and body movements to communicate with each other? They can also recognize themselves in a mirror, which is a sign of self-awareness.
Prompt 2:
Prompt 2:
Whales and dolphins are both marine mammals, but they belong to different families. Whales are part of the family Cetacea, which includes toothed whales and baleen whales, while dolphins are part of the family Delphinidae. Here are some key differences between whales and dolphins:
Whales and dolphins are both marine mammals, but they belong to different families. Whales are part of the family Cetacea, which includes toothed whales and baleen whales, while dolphins are part of the family Delphinidae. Here are some key differences between whales and dolphins:
1. Physical characteristics: Whales are generally larger than dolphins, with some species of whales reaching lengths of over 100 feet. Dolphins, on the other hand, are smaller, with the largest species, the killer whale, reaching lengths of up to 32 feet. Whales also have a more streamlined body shape, while dolphins have a more robust body shape.
1. Physical characteristics: Whales are generally larger than dolphins, with some species of whales reaching lengths of over 100 feet. Dolphins, on the other hand, are smaller, with the largest species, the killer whale, reaching lengths of up to 32 feet. Whales also have a more streamlined body shape, while dolphins have a more robust body shape.
2. Feeding habits: Whales have teeth and are carnivorous, feeding on fish, squid, and other marine animals. Dolphins also have teeth, but they are smaller and more pointed, adapted for catching fish. Baleen whales, on the other hand, have baleen plates instead of teeth, which they use to filter-feed on plankton and small crustaceans.
2. Feeding habits: Whales have teeth and are carnivorous, feeding on fish, squid, and other marine animals. Dolphins also have teeth, but they are smaller and more pointed, adapted for catching fish. Baleen whales, on the other hand, have baleen plates instead of teeth, which they use to filter-feed on plankton and small crustaceans.
3. Echolocation: Dolphins use echolocation to navigate and find prey, emitting high-frequency sounds and listening for the echoes to determine the location and distance of objects. Whales do not use echolocation in the same way as dolphins, but some species, such as the toothed whales, use low-frequency sounds to communicate and locate prey.
3. Echolocation: Dolphins use echolocation to navigate and find prey, emitting high-frequency sounds and listening for the echoes to determine the location and distance of objects. Whales do not use echolocation in the same way as dolphins, but some species, such as the toothed whales, use low-frequency sounds to communicate and locate prey.
4. Reproduction: Whales and dolphins have different reproductive strategies. Whales typically give birth to one calf at a time, and the mother nurses the calf for an extended period. Dolphins, on the other hand, can have multiple calves in a lifetime and do not nurse their young for as long.
4. Reproduction: Whales and dolphins have different reproductive strategies. Whales typically give birth to one calf at a time, and the mother nurses the calf for an extended period. Dolphins, on the other hand, can have multiple calves in a lifetime and do not nurse their young for as long.
5. Behavior: Whales are generally solitary or live in small groups, while dolphins are known for their social behavior, often living in large pods of up to several hundred individuals.
5. Behavior: Whales are generally solitary or live in small groups, while dolphins are known for their social behavior, often living in large pods of up to several hundred individuals.
Overall, while whales and dolphins share many similarities as marine mammals, they have distinct differences in physical characteristics, feeding habits, and behavior.
Overall, while whales and dolphins share many similarities as marine mammals, they have distinct differences in physical characteristics, feeding habits, and behavior.
formatted_prompt=chat_prompt.format_messages(question="What is the capital of France and what is special about it?")
formatted_prompt=chat_prompt.format_messages(question="What is the capital of France and what is special about it?")
# Run inference
# Run inference
response=llm.invoke(formatted_prompt)
response=llm.invoke(formatted_prompt)
print(response)
print(response)
```
```
%% Output
%% Output
Device set to use cuda:0
Device set to use cuda:0
System: You are a research assistant providing precise, well-cited responses.
System: You are a research assistant providing precise, well-cited responses.
Human: What is the capital of France and what is special about it?
Human: What is the capital of France and what is special about it?
The capital of France is Paris. Paris is known for its rich history, art, culture, and architecture. It is home to many famous landmarks, including the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral. Paris is also known for its fashion industry and is considered a global center for art, fashion, gastronomy, and culture. Additionally, Paris is one of the most visited cities in the world, attracting millions of tourists each year.
The capital of France is Paris. Paris is known for its rich history, art, culture, and architecture. It is home to many famous landmarks, including the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral. Paris is also known for its fashion industry and is considered a global center for art, fashion, gastronomy, and culture. Additionally, Paris is one of the most visited cities in the world, attracting millions of tourists each year.
One fascinating but somewhat bouncy fun actual to know about Dangle up is what it actually takes taking portion it move along in deep amelioration (it moves up a stagger you would amaze me a huge to tell that in more everyday terms but, you go on read more more information). Not one side the ameljoros' movements, you need one piece fact to now is Dangle its really cool but there just be even out its not out in this question yet to that side its even though that'sn really hot enough there just about D
One fascinating but somewhat bouncy fun actual to know about Dangle up is what it actually takes taking portion it move along in deep amelioration (it moves up a stagger you would amaze me a huge to tell that in more everyday terms but, you go on read more more information). Not one side the ameljoros' movements, you need one piece fact to now is Dangle its really cool but there just be even out its not out in this question yet to that side its even though that'sn really hot enough there just about D
Making the same exact request often? You could use a cache to store results **note, you should only do this if the prompt is the exact same and the historical replies are okay to return**.
Making the same exact request often? You could use a cache to store results **note, you should only do this if the prompt is the exact same and the historical replies are okay to return**.
# The first time, it is not yet in cache, so it should take longer
# The first time, it is not yet in cache, so it should take longer
print(clean_output(chat_llm.invoke("Tell me a fact about Mars").content))
print(clean_output(chat_llm.invoke("Tell me a fact about Mars").content))
```
```
%% Output
%% Output
Here's a neat yet mind boggilking thing abojjtu tbt t btyu rzszjr Mars
Here's a neat yet mind boggilking thing abojjtu tbt t btyu rzszjr Mars
The Valley Of
The Valley Of
Whales: Just recently has man
Whales: Just recently has man
Found evidence That ancient L u g ug al waters created deep water lakes where now in present d avrs nz day sittthe dried and crackeds seav on b of tte h h t rar Mars - they'be have nd c all it Mars a Val,..ey of lWh aies
Found evidence That ancient L u g ug al waters created deep water lakes where now in present d avrs nz day sittthe dried and crackeds seav on b of tte h h t rar Mars - they'be have nd c all it Mars a Val,..ey of lWh aies
print(clean_output(chat_llm.invoke("Tell me a fact about Mars").content))
print(clean_output(chat_llm.invoke("Tell me a fact about Mars").content))
```
```
%% Output
%% Output
Here's a neat yet mind boggilking thing abojjtu tbt t btyu rzszjr Mars
Here's a neat yet mind boggilking thing abojjtu tbt t btyu rzszjr Mars
The Valley Of
The Valley Of
Whales: Just recently has man
Whales: Just recently has man
Found evidence That ancient L u g ug al waters created deep water lakes where now in present d avrs nz day sittthe dried and crackeds seav on b of tte h h t rar Mars - they'be have nd c all it Mars a Val,..ey of lWh aies
Found evidence That ancient L u g ug al waters created deep water lakes where now in present d avrs nz day sittthe dried and crackeds seav on b of tte h h t rar Mars - they'be have nd c all it Mars a Val,..ey of lWh aies
"This notebook demonstrates key chaining functionalities in LangChain:\n",
"- SimpleSequentialChain\n",
"- SequentialChain\n",
"- LLMRouterChain\n",
"- TransformChain\n",
"\n",
"Each chaining method is designed for different levels of complexity and control. Use simple chains for straightforward tasks, sequential chains for workflows, router chains for conditional branching, and transform chains when integrating custom logic."
" #quantization_config=quantization_config, # This is what you would need for the LLama3-70B (and similar) models\n",
" local_files_only=True, # Prevent any re-downloads\n",
" #trust_remote_code=True # Necessary when downloading\n",
")\n",
"\n",
"# Verify model config\n",
"print(model.config)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "91d434e4-911e-44fb-b21d-10bc00bef193",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Device set to use cuda:0\n"
]
}
],
"source": [
"# Pipeline setup\n",
"pipe = pipeline(\"text-generation\",\n",
" model=model,\n",
" tokenizer=tokenizer,\n",
" return_full_text=False,\n",
" max_new_tokens=256)\n",
"llm = HuggingFacePipeline(pipeline=pipe)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f670afae-b70b-4782-9824-954540dd3ce0",
"metadata": {},
"outputs": [],
"source": [
"chat_llm = ChatHuggingFace(llm=llm)"
]
},
{
"cell_type": "markdown",
"id": "11e6bdc7-dddc-4115-8736-81c4182c1bcf",
"metadata": {},
"source": [
"### SimpleSequentialChain\n",
"\n",
"The `SimpleSequentialChain` is the most basic form of a chain. It takes a single input, passes it to a prompt, and the output of one step is directly passed as input to the next. It does not track intermediate steps or provide access to named outputs, making it suitable for linear, single-purpose chains.\n",
"\n",
"Use case: quick linear pipelines like \"generate → explain\" or \"summarize → expand\"."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d948c96f-2f90-4b4e-aeb4-9aff615b985d",
"metadata": {},
"outputs": [],
"source": [
"template1 = \"Give me a simple bullet point outline for a blog post on {topic}\"\n",
"A. Definition of Artificial Intelligence (AI)\n",
"\n",
"Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. It involves the development of algorithms and computer systems that can perform tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, and language translation.\n",
"\n",
"B. Brief history of AI development\n",
"\n",
"The concept of AI has been around since the 1950s, but it was not until the 21st century that significant advancements were made in the field. Early AI research focused on symbolic AI, which involved using symbolic rules and logic to solve problems. However, this approach had limitations, and researchers soon turned to statistical methods and machine learning techniques to improve AI capabilities.\n",
"\n",
"C. Importance and potential impact of AI on society\n",
"\n",
"AI has the potential to revolutionize various industries and aspects of our daily lives. It can help automate repetitive tasks, improve decision-making, and enhance customer experiences. AI can also help solve complex problems in areas such as healthcare, education, and environmental conservation. However, there are also concerns about the impact of AI on employment and privacy,\n"
"`SequentialChain` is more flexible than `SimpleSequentialChain`. It supports multiple input and output variables and keeps track of intermediate outputs. Each step can depend on one or more outputs from earlier steps.\n",
"\n",
"Use case: more complex workflows that need to reuse or transform earlier outputs in later steps."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99c23725-9cf9-4927-9f8d-df65bdee4d72",
"metadata": {},
"outputs": [],
"source": [
"# Create the prompts\n",
"prompt1 = PromptTemplate(input_variables=[\"topic\"], template=\"Generate a question about {topic}.\")\n",
"prompt2 = PromptTemplate(input_variables=[\"question\"], template=\"Provide a short answer to: {question}\")"
"`LLMRouterChain` is used when you want to route a prompt to different chains or prompts depending on the input. It allows conditional execution paths, where an LLM can decide which destination (e.g., math, history, writing) to route a given input to based on predefined criteria or patterns.\n",
"output = router_chain.invoke({\"input\": \"Battle of Hastings\"})\n",
"print(\"Router Output:\", output)"
]
},
{
"cell_type": "markdown",
"id": "5a5c50df-480c-40ca-86b2-e0658ea23c2d",
"metadata": {},
"source": [
"### TransformChain\n",
"\n",
"`TransformChain` allows you to insert arbitrary Python logic into a LangChain pipeline. It lets you define a transformation function that takes in inputs and returns a modified dictionary of outputs. This is useful for pre- or post-processing data before or after it passes through a model or another chain.\n",
"\n",
"Use case: text normalization, formatting, filtering, or enrichment between model steps."
This notebook demonstrates key chaining functionalities in LangChain:
- SimpleSequentialChain
- SequentialChain
- LLMRouterChain
- TransformChain
Each chaining method is designed for different levels of complexity and control. Use simple chains for straightforward tasks, sequential chains for workflows, router chains for conditional branching, and transform chains when integrating custom logic.
The `SimpleSequentialChain` is the most basic form of a chain. It takes a single input, passes it to a prompt, and the output of one step is directly passed as input to the next. It does not track intermediate steps or provide access to named outputs, making it suitable for linear, single-purpose chains.
Use case: quick linear pipelines like "generate → explain" or "summarize → expand".
Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. It involves the development of algorithms and computer systems that can perform tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, and language translation.
B. Brief history of AI development
The concept of AI has been around since the 1950s, but it was not until the 21st century that significant advancements were made in the field. Early AI research focused on symbolic AI, which involved using symbolic rules and logic to solve problems. However, this approach had limitations, and researchers soon turned to statistical methods and machine learning techniques to improve AI capabilities.
C. Importance and potential impact of AI on society
AI has the potential to revolutionize various industries and aspects of our daily lives. It can help automate repetitive tasks, improve decision-making, and enhance customer experiences. AI can also help solve complex problems in areas such as healthcare, education, and environmental conservation. However, there are also concerns about the impact of AI on employment and privacy,
`SequentialChain` is more flexible than `SimpleSequentialChain`. It supports multiple input and output variables and keeps track of intermediate outputs. Each step can depend on one or more outputs from earlier steps.
Use case: more complex workflows that need to reuse or transform earlier outputs in later steps.
`LLMRouterChain` is used when you want to route a prompt to different chains or prompts depending on the input. It allows conditional execution paths, where an LLM can decide which destination (e.g., math, history, writing) to route a given input to based on predefined criteria or patterns.
Use case: topic routing, multi-skill assistants, task-specific logic dispatching.
`TransformChain` allows you to insert arbitrary Python logic into a LangChain pipeline. It lets you define a transformation function that takes in inputs and returns a modified dictionary of outputs. This is useful for pre- or post-processing data before or after it passes through a model or another chain.
Use case: text normalization, formatting, filtering, or enrichment between model steps.