What is an example selector, and why do we need it? | Langchain
Tags: programming, ai, chatgpt
Published: 2023-09-23 07:20:29

Teaching an LLM (language model) to perform tasks effectively often feels like providing a recipe to a chef who's still experimenting with their culinary skills. You need that perfect blend of ingredients, just like you need precise instructions to get the output you desire. In the realm of machine learning and natural language processing, clarity and precision are paramount. And what better way to communicate your desires to an LLM than through examples? Yet, if you've ever attempted to provide multiple examples, you know it can feel a bit like concocting a potion—mixing various ingredients, hoping for the best, and sometimes ending up with unexpected results. It's a journey full of "hallucinations," where the magic of language modeling unfolds. So, in this article, we'll embark on a quest to unravel how Langchain, the wizards of AI, helps you select just the right examples from a sea of possibilities, making the enchanting process of instructing your LLM a whole lot easier.

In Langchain, we have a handy tool called the "BaseExampleSelector" class. Think of it as a tool belt for selecting examples to use in your prompts. It comes with a function called "select_examples." This function takes input variables and does the heavy lifting, giving you a list of examples that fit the bill.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31te7d9nt4lojk8j4r4q.png)

Now, the first option we have is "Custom Example Selector". It's like your personal assistant for picking examples. You can redirect it based on your own needs. Imagine you have a bunch of examples, and you want to choose some randomly. Well, this tool lets you do just that. It uses a function called "np.random.choice" to make those random picks, and you can even specify how many examples you want to select. But here's the thing: It works best when all your examples are closely related. If they're not, you might end up confusing your LLM. So, choose wisely and keep things simple and clear to get the best results.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzjemphwdh93oiske7u9.png)

Now, let's explore the "Select by Length" method in Langchain. This method is all about making choices based on the length of the examples. Imagine you're trying to construct a prompt, but you're worried it might end up being too long for the LLM's comfort. You see, LLMs have a context window, which is like the number of tokens they can handle as input. When dealing with longer prompts, it's wise to pick fewer examples, while for shorter prompts, you can choose more.

Here's how it works: Langchain lets you configure the "max_length" parameter with a specific number. This number tells Langchain the maximum length you want for your prompt. Then, like a diligent assistant, Langchain will select examples that fit within that length limit.

So, if you want your prompt to stay within a certain token count, this method has your back. It's like telling your LLM, "Hey, let's keep it short and sweet today!"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0r292phj6cdqb910mywj.png)

Now, let's introduce you to the "MaxMarginalRelevanceExampleSelector" in Langchain. This method takes a unique approach. It wants to find examples that are both similar to your inputs and diverse at the same time. Imagine you're at an ice cream parlour, and you want to try a bit of every flavour without having the same one twice. That's what this method is all about!

Here's how it works: Langchain looks at the examples and checks which ones are most similar to your inputs using something called "cosine similarity." Don't worry; it's not about trigonometry. Cosine similarity simply measures how much two things are alike, like checking if two arrows point in roughly the same direction. The closer they are, the more similar they are.

But there's a twist! Langchain doesn't stop at finding similar examples. It also looks for diversity. So, it picks examples that are similar but not too close to the ones it's already chosen. It's like assembling a team of superheroes, making sure each one brings something unique to the table.

So, with this method, you get the best of both worlds—examples that match your inputs and a dash of variety to keep things interesting. It's like having an ice cream cone with all the flavours, but none of them tastes the same!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbuiplogmblu5lw8bcfg.png)

Now, let's dive into the "NGramOverlapExampleSelector" method in Langchain. This method is all about finding examples that are similar to your input, but it has a twist: it uses something called an "n-gram overlap score" to make its selections.

Don't worry, n-grams are just fancy terms for overlapping sequences of words in text. Imagine you have two texts, and you want to know how much they share in common. N-grams help with that. They look for sequences of words that both texts have in common, like finding the same set of words in two books.

So, Langchain's method assigns an n-gram overlap score to each example. This score is a number between 0.0 and 1.0, where 0.0 means there's no overlap and 1.0 means they are identical. You can also set a threshold score. If an example's score is less than or equal to the threshold, it's left out. By default, the threshold is set at -1.0, meaning it won't exclude any examples; just reorder them. But if you set it to 0.0, examples that have no n-gram overlap with your input will be excluded.

So, in a nutshell, this method helps you find examples that share common words with your input, ensuring that your LLM's responses are on the same page with your content. It's like finding sentences in two different books that have the same words—a handy way to make sure your LLM gets the right idea.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0j6dauf9vf49kymzueec.png)

Now, let's uncover the "Select by Similarity" method in Langchain. This one is all about choosing examples that are most similar to your inputs. It's like picking friends who share your interests—you want to be on the same wavelength.

Here's how it works: Langchain looks at the examples and checks which ones have embeddings (basically, representations of the text) that are most like your input's embeddings. It uses something called "cosine similarity" to make this judgment. If two examples point in roughly the same direction in the vast space of text, they're considered similar.

But there's an interesting twist! Langchain also offers another method called "Select by MMR." This method aims for diversity. So, even if some examples aren't as similar to your input as the top ones, it will still select them. It's like inviting a mix of friends to your party—some who share your interests and some who bring a fresh perspective.

In a nutshell, "Select by Similarity" focuses on choosing the most similar examples, while "Select by MMR" goes for a more varied selection. It's like deciding whether you want a playlist of all your favorite songs or a mix of different genres to keep things interesting.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fe98i6rj7vyjdt8nwya.png)

In the grand journey of instructing your LLM, the power of examples becomes your trusted ally. These example selectors in Langchain are like your toolkit, helping you pick the perfect pieces to craft your instructions. There's no absolute right or wrong here; it's all about what suits your needs and use case. Whether you choose to go for similarity, diversity, or randomness, the aim remains the same: using your examples in the best possible way to guide your LLM's understanding and behavior. So, embrace the magic of example selectors, and may your language model's responses always hit the mark!