Getting Started with LangChain - Part 1: A Beginner's Guide to Building LLMs-Powered Applications
Tags: aiops, nlp, chatgpt, ai
Published: 2023-05-24 08:34:32

Today AI has evolved in ways people would never have perceived. More so in generative AI, companies like Openai and Google have changed the way we humans interact with machines by building ChatGPT and Google Bard respectively. Language models, moreso Large Language Models(LLMs) have today become the topic of the day in most tech industries. LLMs is playing a great role in making machines understand, interpret and generate human language with high accuracy through AI. The LLMs models are trained on large amounts of text data from which they get to grasp and understand different linguistic patterns semantically. They power applications that perform tasks like text generation, language translation and conversational interface chatbots.

LangChain is a framework that has been created to help developers design cutting edge applications using LLMs. It is like a magical tool that help developers talk with talking robots! It lets you control how these robots talk and play together. You can make them say funny things, ask questions, and even make them find information from special places called APIs and databases. It's like having a bunch of talking friends who can do all sorts of cool stuff!

This cool article is all about LangChain! It tells you everything you need to know about it. LangChain is like a magic tool that can do lots of amazing things with words. It helps you talk and write better. The article also shares some great ideas on how to use LangChain and how to make it work really well in your projects. Whether you're a super smart developer or just starting to learn about this cool stuff, this guide is here to help you get started. 

## What is LangChain and Why is it Awesome?

LangChain is like a super cool toolbox for developers. It helps them create amazing apps that can understand and talk in different languages. Imagine having a magical box filled with special tools, buttons, and switches that make it super easy to build these language-powered apps.

With LangChain, developers can play around with all these tools and build incredible apps without much trouble. They can make these apps talk, understand what people are saying, and even connect with other cool stuff like websites and databases. It's like putting all the puzzle pieces together effortlessly.

The best part is that LangChain also gives developers some special codes that they can add to their apps. These special codes help the apps process language and understand it better, without developers having to start everything from scratch. It's like having a head start on creating something awesome.

So, whether you're a beginner or a pro developer, LangChain is here to make your life easier. It takes away the complicated parts and lets you focus on the fun and creative stuff. Imagine a world where robots talk to you like your best friend, your phone understands and translates any language, and computers can even tell how you feel just by reading your words. Chatbots, virtual assistants, language translation tools, and sentiment analysis tools are some super cool examples of these apps. But what's the secret behind them? Developers use LangChain to create customized language models that can do all these amazing things! As we keep improving the way computers understand and use human language, the possibilities for this technology are like a never-ending adventure. Who knows what other incredible things we'll be able to do with it in the future? The sky's the limit!

## Unveiling the Secrets of LangChain: How it Works and What's Inside

LangChain stands out with its remarkable focus on flexibility and modularity, which greatly benefits developers seeking tailored solutions. By breaking down the natural language processing pipeline into individual components, LangChain empowers developers to mix and match these building blocks effortlessly. This versatility allows for the creation of custom workflows that precisely cater to specific requirements. Consequently, LangChain emerges as an exceptionally adaptable framework capable of constructing conversational AI applications across diverse industries and use cases.

Within LangChain, a rich array of key features awaits developers, including components and chains, prompt templates and values, example selectors, output parsers, indexes and retrievers, chat message history, document loaders, text splitters, agents, and toolkits. These comprehensive features empower developers to build end-to-end conversational AI applications that deliver personalized and captivating user experiences. With LangChain, the possibilities are endless, enabling developers to unlock the full potential of conversational AI and create exceptional applications.

**1. Components and chains:** LangChain, the powerhouse of natural language processing, operates through the clever arrangement of "components" and "chains." Each "component" represents a distinct block of code or module dedicated to fulfilling a specific function within the language processing pipeline. These components can be ingeniously linked together in "chains," forming customized workflows tailored to specific purposes. For instance, envision a chain designed for a customer service chatbot, comprising components such as sentiment analysis, intent recognition, and response generation. By skillfully integrating these components, LangChain empowers applications to engage in seamless and effective communication.

**2. Prompt templates and values:** Prompt templates are a fantastic feature that allows you to use predefined prompts across various chains without any hassle. By inserting values into these templates, you can make them even more flexible and customized for specific situations. Let's say you have a prompt template asking for the user's name; you can easily insert a value into that template to create a personalized response. This comes in handy when you want to generate prompts dynamically based on varying resources. With prompt templates, you have the power to create engaging and tailored interactions effortlessly.

```
from langchain.llms import OpenAI
from langchain import PromptTemplate
llm = OpenAI(model_name="text-davinci-003", openai_api_key="YourAPIKey")  # Notice "food" below, that is a placeholder for another value later
template = """ I really want to eat {food}. How much should I eat? Respond in one short sentence """
 
prompt = PromptTemplate(
    input_variables=["food"],
    template=template,
)
 
final_prompt = prompt.format(food="Chicken")
 
print(f"Final Prompt: {final_prompt}")
 
print("-----------")
 
print(f"LLM Output: {llm(final_prompt)}")


```
Let's take a look at an example where the prompt is modified to include the specific food "Chicken." The resulting response from the AI is a concise sentence that suggests not to eat more than a kilo of chicken.

```
Final Prompt:
I really want to eat Chicken. How much should I eat?
 
Respond in one short sentence
 
-----------
LLM Output:
Don't eat more than a kilo.
```
**3. Example  selectors:** Example selectors play a crucial role in enhancing the accuracy and relevance of the model's responses by identifying relevant examples from its training data. These selectors can be customized to prioritize specific types of examples and exclude irrelevant ones, allowing you to fine-tune the AI's response according to the user input. By leveraging example selectors, you can optimize the performance of the AI model and provide more precise and tailored outputs.

```
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
from langchain.llms import OpenAI
 
llm = OpenAI(model_name="text-davinci-003", openai_api_key="YourAPIKey")
 
example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Example Input: {input}\nExample Output: {output}",
)
 
# Examples of locations that nouns are found
examples = [
    {"input": "pirate", "output": "ship"},
    {"input": "pilot", "output": "plane"},
    {"input": "driver", "output": "car"},
    {"input": "tree", "output": "ground"},
    {"input": "bird", "output": "nest"},
]
 
# SemanticSimilarityExampleSelector will select examples that are similar to your input by semantic meaning
 
example_selector = SemanticSimilarityExampleSelector.from_examples(
    # This is the list of examples available to select from.
    examples,
    # This is the embedding class used to produce embeddings which are used to measure semantic similarity.
    OpenAIEmbeddings(openai_api_key=openai_api_key),
    # This is the VectorStore class that is used to store the embeddings and do a similarity search over.
    FAISS,
    # This is the number of examples to produce.
    k=2,
)
 
similar_prompt = FewShotPromptTemplate(
    # The object that will help select examples
    example_selector=example_selector,
    # Your prompt
    example_prompt=example_prompt,
    # Customizations that will be added to the top and bottom of your prompt
    prefix="Give the location an item is usually found in",
    suffix="Input: {noun}\nOutput:",
    # What inputs your prompt will receive
    input_variables=["noun"],
)
```
An example selector, when given the noun "student," can identify the most similar examples such as "driver" and "pilot."

```
# Select a noun!
my_noun = "student"
 
print(similar_prompt.format(noun=my_noun))
```
Output:

```
Give the location an item is usually found in
 
Example Input: driver
Example Output: car
 
Example Input: pilot
Example Output: plane
 
Input: student
Output:
```
**4. Output parsers:** Output parsers play a crucial role in handling and refining the responses generated by the model. These parsers are employed to effectively process and filter the outputs, ensuring that they meet specific requirements. By utilizing output parsers, undesired content can be eliminated, the output can be formatted according to desired specifications, and supplementary information can be seamlessly incorporated into the response. These parsers facilitate the extraction of structured output formats, such as JSON objects, from the language model's responses, enabling streamlined and organized data utilization.

```
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.llms import OpenAI
 
llm = OpenAI(model_name="text-davinci-003", openai_api_key="YourAPIKey")
 
# How you would like your reponse structured. This is basically a fancy prompt template
 
response_schemas = [
    ResponseSchema(name="bad_string", description="This a poorly formatted user input string"),
    ResponseSchema(name="good_string", description="This is your response, a reformatted response"),
]
 
# How you would like to parse your output
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
```
Let's create a special template that will help us ask questions and get awesome answers from the AI. This template will have some empty spots where we can put specific instructions and information from the user. We'll also leave a space for the AI to fill in its fantastic response. It's like having a magical conversation with our AI friend.

```
json
{ "bad_string": string  // This a poorly formatted user input string "good_string": string  // This is your response, a reformatted response
}
```
After you have completed the prompt template, it's time to send it to the language model and receive the desired response. Once you receive the response, you can utilize the output parser to extract structured JSON objects or Python dictionaries, making it easier to process and analyze the information.

```
template = ( """ You will be given a poorly formatted string from a user. Reformat it and make sure all the words are spelled correctly {format_instructions} % USER INPUT: {user_input} YOUR RESPONSE: """
)
 
prompt = PromptTemplate(input_variables=["user_input"], partial_variables={"format_instructions": format_instructions}, template=template)
 
promptValue = prompt.format(user_input="welcom to califonya!")
 
print(promptValue)
```
Output:

```
json\n{\n\t"bad_string": "welcom to califonya!",\n\t"good_string": "Welcome to California!"\n}\n
```