In this lesson, you'll create your first haystack components. Custom components are a huge part of the haystack experience. We have lots of components, but there's no way we have everything you need. Let's dive in and build a component that can fetch the top Hacker News posts. All right, let's go. In this lab, we're going to be building our first custom haystack component. And we're going to be creating a new summarizer. Specifically, we're going to create a hack, a new summarizer. So far, you've used readymade components with haystack, but this time you're going to create your own. Let's remind ourselves what components are some readymade components in haystack are embeds, retrievers, generators, and so on. Each of them expect a certain number of inputs, and they can also produce a certain number of outputs. But for something to be a component in haystack, we only really have a few requirements. For example, we need to have the component decorator for a class. That class needs a run method and it has to return a dictionary. Finally, we also have to provide component output types. We'll see why in a bit. For example here we have a translator component. And this is a very valid component of the haystack. We've decorated it with components and we've provided the output types that this component is producing. In this case we're telling haystack world that this is going to be producing documents of type list document. Next we have a run method. So we're also saying that this component expects three inputs from lang to lang and documents. And some of them even have default values. Finally we're returning a dictionary. So in this case we're telling the haystack world that this is returning documents. And if we've done our job correctly, we should be returning translated documents. In this case. In this lab you'll build two separate pipelines. You'll start with a very, very simple one. This pipeline is going to be a dialog builder. And you'll be using your very own greeter components. That's going to start with a greeting for the dialog. Next you'll build a. How can you summarize a pipeline? For this pipeline, you'll create your very own. How can use such a component? By the end of the lab, you'll have a pipeline that's able to provide summaries of the top K trending posts on Hacker News. Again let's start with the usual warning suppression. And also let's load our environment variables. Next let's import all of the dependencies that we're going to be using in this lab. And once we're done with that we can start coding. Let's start by building our very own custom components. Custom components are pretty useful, not only because sometimes you might notice that haystack doesn't provide exactly what you need, but you may also want to have your own component that accesses a specific API. In this case, let's start with a toy example where we need to have a greeter component. Let's start by creating a class called greeter and providing it the component decorator. Next, we need a run method. In this case, I've decided that my components should be expecting username, and it should return a greeting that says hello username. The only thing we have left to add here is the output type's decorator. We have to tell the haystack pipelines and the haystack world that this is outputting something called greeting, and it's of type string. This is used later on with pipeline validation. Well, basically telling the Hastag pipeline that this greeter component can only connect to something that is expecting string as input. Now let's see how we might use this component. Let's start by initializing our greeter. And for this case, I've decided to build a pipeline that's basically a dialog builder. So let's start and create our prompt template. In this case, I've created the prompt template that starts with the following statement. You'll be given the beginning of a dialog. Create a short play script using this as a start of the play start of dialog dialog. So I know that my prompt builder is going to be expecting something called dialog. Let's add this template to a prompt builder. And then finally, again, I'll be using the OpenAI generator with the default model, which is GPT three. Now we can start creating the dialog builder. First you'll initialize your pipeline. And again, as usual, you'll start adding your components. Now, the one thing that's going to be slightly different in this pipeline to the previous ones is how you connect your greeter to the prompt builder. Your greeter is outputting something called greeting, which is a string, and your prompt builder is expecting something called dialog. So we can start with a greeting for the dialog. Next, let's simply connect the prompt to the large language model. And there we have it. Again, let's show our pipeline just to make sure that we've got the correct pipeline that we expect. Great. Now we see that the greeter is the first component and we are expecting username. So if all goes well, we should be able to run this pipeline with a username like Tijuana. And hopefully we get a dialog that starts starts with hello Tijuana. Let's run our dialog builder and assign the outputs to dialog. And let's also print out the dialog itself. And there we are. We have a short play with two characters, Mary and Tijuana. And Mary starts with the statement hello, Tijuana. You can try changing the username here, and hopefully you'll get some dialogs that start with your name or someone else's name as well. Now that you've seen how to create your very own haystack component and also how to add that to full pipelines. Let's start building a more complex component that maybe is a bit more similar to what you'll see in the real world. For this use case, we're going to be building a component for how can you summarize. So we're going to be building a component called the Hacker News feature. We're able to access current trending Hacker News posts with an API as. So if you like, you can also change from asking for the trending posts to the newest posts. There are various things on Hacker News such as new posts, Ask Hacker News, and so on. This is asking for the top trending posts, but if you like, for example, you can change this to news stories instead. News stories change a lot more frequently. Top stories less I'll be using top stories. This request gets various top stories, and for example, we can look at the current top story right now. The important thing to note is you'll be seeing different contents than what I'm seeing, because you'll be running this on a different day. All right. You'll notice that this story has a URL. Mostly Hacker News has URLs for stories, but in some cases, you'll notice that this is actually text. And that's text instead of a URL. You should pay attention to what kind of news stories are currently trending when you run this, which may require you to make some edits or change your component if you wish. All right, so let's start with a bare minimum. How can you set a component that's basically returns nothing? In this case we've created a how can you set a class. And it's expecting top k integer as inputs. And now it's returning an empty list and it's returning something called articles. This is a very valid hastag component. But as you can see it's not returning anything yet. So let's start filling this in. We're going to start by populating the articles list. And we're going to use the same exact request that you saw before. We now have a Hacker News searcher, but we're doing something a bit different because most articles have URLs. We're going to add, a HTML conversion pipeline, which is something that you saw in the second lab in the in it's of this class. Next, let's start building the run method. Again, let's start with an empty list of articles. But now we're going to have to populate this empty list of articles. Let's get the trending list from our API. And then this asks the top k articles to be added to our articles list. For this I'm going to be adding a for loop. And then let's look at what the for loop has in it. For top k posts we loop through the trending list. And if there is a URL we actually ask our HTML pipeline to run. And we add the contents to our articles list. I've also added an extra statement here. As I mentioned, some Hacker News posts do not have URLs. Instead they have text. In this case, we are simply adding the contents of that text into our articles list. Finally, we can return our articles. All right. Now we have our. How can you set a. Now let's try running this Hacker News feature on its own. For this, I've decided to ask for the top three trending Hacker News posts. I'm assigning them to results. And since we know that the results are in articles, let's print this out. As you can see, there are three documents and each of them have URLs, and each of them also have the contents of those URLs. Well, now able to use this component in a full haystack pipeline to ask for summaries of the top trending Hacker News posts. Now that we have our hacker newsletter and we're able to get the trending posts, let's start creating our Summarizer pipeline. I'll start by creating my prompt templates. In this prompt template, I've indicated that we will be provided a few of the top posts in Hacker News for each post. Please provide a brief summary if possible. And then finally, we are looping through articles and adding the contents of those articles specifically into our prompt. now that we have this prompt template, we can create our prompt builder. We also want our fetcher. And again we want to Netherland. Again. I'll be using GPT three, but feel free to change this if you like. Let's create our pipeline. I've called it the Summarizer pipeline, and again I'm adding all of the components that I need to my pipeline. Finally, let's start connecting the components. We know that our fetcher that we created outputs articles. And I want that to go into prompt articles. The last connection is the same as usual. We're connecting the prompt to our large language model. And there we have it. We can also show our pipeline again, just to be sure, we've created the pipeline that we expect to see. So this pipeline starts with our fetcher. And the first thing we're expecting is an integer that many articles will be fetched. And then we'll be hopefully creating summaries for them. Let's run this pipeline and ask for the summaries of the top three Hacker News post right now. you'll be seeing something totally different to what I'm seeing. Probably. And this is expected you'll be seeing summaries of the top three posts on the day that you run this lab yourself. Before we end this lab, let's start customizing this prompt to do something a bit different as well. You may remember the Link Content Fetcher, which we also use in our custom component, adds URLs to the metadata. So why not not only ask for summaries, but also asks for the URL that the post is coming from. So let's start by creating our custom prompt again. The only thing we'll be changing here is simply adding the URLs of the articles into the prompt, as well. For each post, provide a brief summary followed by the URL of the full post, we're adding URL article meta URL into our prompt. Now we simply create the same exact pipeline as you saw before, using the same exact components. The only thing that's different is the prompt build in this case is using our new prompt template. Now that we have our custom prompt, let's ask for the summaries. In this case, the top two articles on Hacker News. Once you run this pipeline, if all goes well you should be seeing as many summaries as you requested in top k followed by the URL. If this doesn't go exactly how you expect, you can start looking into whether you should edit your prompt to be a bit more precise. And even in some cases, you may want to implement self reflection where we're asking the NLM to improve on its results. You'll be seeing an example of this in the following labs. In this lab, you not only saw how to create your very own custom haystack component, but also how to use that in a pipeline. This is not only useful if you want to access a different API like we did here with how can use. But maybe if you want to access a model provider that haystack doesn't yet provide access to, or maybe simply implements your very own behavior in a component. This is really nice because we also get community members adding their own custom components to haystack integrations, so you can scroll through those and see if someone else has already created one for you. You can also add your own custom components to Haystack Integrations if you like. I'll include a link to Haystack Integrations in the notebook for you to scroll through the integrations that the haystack community or Deep Set have created. In the next lab, we're going to be looking at your first haystack pipeline that implements branching, and we're going to be using branching to implement a fallback mechanism. I'll see you there.