Machine Learning Spot

What is Langchain Hub? How to Use Langchain Hub

Feature Image of LangChain Hub

Prompt engineering is the future? Where can I find the community? Langchain Hub is a platform in Langchain within Langchain Smith to share prompts, learn from others’ prompts, and use other’s prompts.

It is really simple to use and helps us save time. It’s integrated with Langsmith having 5K active teams and more than 80K signups, 40 M traces logged in as of Feb 2024 Today we will see how to use Langchain Hub for both coding and live testing and how to find the prompts that we are looking for.

Let’s explore the platform of discovery, sharing, colaboration, version control!

Exploring Langchain Hub

Let’s visit the Langsmith of Langchain. Scroll to the bottom of the page, find the Langchain Hub, and click on it. Then, sign up.

After signing up, you will see multiple filtering options, which you can see like Top Favorite, Top Viewed, Top Downloaded, Recently Updated, Use Cases, etc. filter them out to find the prompt that might be best for you.

Suppose you have found the prompt in Langchain Hub that you were looking for. Now there is also an option to test it out directly right on Langchain Hub in Langchain Playground, as you can see in the video below.

To test and run the prompt in Langchain Hub, you will be required to provide the API key of the LLM you wish to use.

You can also share your prompt as a contribution to the community by clicking on the new prompt. The below video shows you a glimpse of how you can share your prompt with prefect tags and you can also keep your prompts private to save them for your own future use.

Exploring Langchain Hub Programmatically

To use a prompt from Langchain Hub, you just need a link for that prompt to pull, but to do that, you need to install Langchain Hub so everything else remains the same make a Langchain chain, connect your LLM to the prompt, and voila! ready to go!

So first, start with installing the essentials.

%pip install langchain
%pip install langchainhub
%pip install fireworks-ai   # To use as LLM. Import OpenAI if you are using OpenAI

Importing Every Essential Modules

I will keep repeating the below lines of code in which we import the modules to show why, and when a person feels the need to import them, I am importing each module in a single block to help you avoid missing any of them.

from langchain import hub
from langchain_community.llms import Fireworks
from langchain.chains import LLMChain
import os

This is how a prompt is pulled from Langchain Hub anyone can pull it if the person has the link; it doesn’t require any login.

from langchain import hub
prompt = hub.pull("hardkothari/prompt-maker")

We have pulled a prompt, but to use it, we need an LLM. We are now using fireworks as LLM I have chosen Fireworks since it’s a good alternative to OpenAI and its usage is almost similar, so get your API from here and follow the below code.

from langchain_community.llms import Fireworks

import os
os.environ["FIREWORKS_API_KEY"] = "My API" #replace My API with your API

llm = Fireworks()

Time to connect the prompt that we have pulled and saved into the variable without LLM and invoke the chain.

from langchain.chains import LLMChain

chain = LLMChain(llm=llm, prompt=prompt)

result = chain.invoke(({"lazy_prompt": "you are a granny expert in story telling", "task": "a story about animals"}))
# Print the improved prompt
print(result)

Output:

{'lazy_prompt': 'you are a granny expert in story telling',
 'task': 'a story about animals',
 'text': '\n\nAs a veteran storyteller, craft a captivating narrative that immerses readers in a world of fascinating creatures and their habitats.\n\nYour tale should unfold with vivid descriptions of diverse ecosystems, from the lush rainforests of the Amazon to the arid deserts of the Sahara.\n\nWeave together a rich tapestry of characters, each with their unique personalities and motivations, and illustrate how they interact and depend on one another.\n\nThe narrative should culminate in a climactic event that highlights the delicate balance of nature and the consequences of disrupting it.\n\nExample:\n\n"In the heart of the Amazon, where the sun\'s rays pierce through the dense canopy, a world of magic awaits.\n\n\n

Conclusion:

I hope you now understand what the Langchain Hub is and how to use it. If you have any suggestions or feedback, feel free to reach out. Also, be sure to read my other blogs on Langchain to become a champion in using Langchain.

Liked the Post?  You can share as well

Facebook
Twitter
LinkedIn

More From Machine Learning Spot

Get The Latest AI News and Insights

Directly To Your Inbox
Subscribe

Signup ML Spot Newsletter

What Will You Get?

Bonus

Get A Free Workshop on
AI Development