Unleashing Python’s Potential: Generative AI and LangChain Take Center Stage

A comprehensive guide to prompt engineering and building applications with Large Language Models (LLM)

Introduction

Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on the interaction between computers and human language. It enables machines to understand, interpret, and generate human language in a meaningful and valuable way. With the rise of machine learning in the 2000s, NLP algorithms evolved to leverage techniques like deep learning, neural networks, and transformer models, leading to significant improvements in language understanding and generation. NLP is widely used in building virtual assistants, information extraction from text, sentiment analysis for social media, machine translation, etc.

In recent years, Generative AI and large pre-trained language models (LLM), such as BERT and GPT have achieved remarkable results across various NLP tasks, leveraging their ability to learn from massive amounts of text data. These rapid advancements have given rise to enabling libraries such as LangChain that lets us use built-in wrapper functions to access the LLM and customize it to our needs with prompts.

In this blog, we will cover the below topics

  • What is Generative AI?
  • What are Large Language Models?
  • What is LangChain?
    – How to build prompts and templates?
    – Agents in LangChain
    – Power of LangChain
  • Conclusion

What is Generative AI?

Generative AI is the branch of artificial intelligence that focuses on developing models capable of generating new and original content. These models learn patterns from existing data and use that knowledge to create unique content that resembles the training data. Generative AI has evolved significantly over the years from probabilistic models in 1980 to Neural networks in the last decade to recent advancements in large language models such as GPT-3 and reinforcement learning for generative tasks. These are foundational models and the most popular are LLM (text-based), Vision (computer vision), and Code (Copilot).

Source: Author

What are Large Language Models?

Large language models are sophisticated AI models that have been trained on vast amounts of text data such as books, articles, etc to understand and generate human-like language. These models, such as GPT-3, possess a remarkable ability to comprehend context, generate coherent responses, and even perform specific tasks based on prompts provided by users. Some of the popular LLMs are GPT-3, GPT-4, BERT, and T5 from Google, XLNet, RoBERTA from Meta. All these powerful models are trained on a specific set of data for a given period eg: The ChatGPT is trained on data till September 2021, so if you query for anything from 2023, the bot will either give a vague answer or state that it is not able to generate an answer.

Let’s try this on ChatGPT and ask something new.

Query:
can you list all the new features from Python version 3.11?

OUTPUT from ChatGTP:
As of my knowledge cutoff in September 2021, Python version 3.11 has not been
released yet. Python follows a release cycle, and new versions are typically
released on a yearly basis. Therefore, the specific features and changes in
Python 3.11 are not available at this time.

To stay updated with the latest Python releases and their features, I
recommend visiting the official Python website (python.org) or checking the
Python documentation once Python 3.11 is released.

The key point to remember

  • Each of the LLMs is trained, and managed by respective companies, and are foundational model.
  • They are not of much help when used in isolation meaning the models need additional data to customize and to get accurate responses.
  • The customization can be connecting the LLM to the database, documents, images, etc so that model learns and caters to specific requirements.

The LLMs are computationally too expensive for regular users or application developers to train and customize. So how does one leverage the power of the LLMs? Let us find answers in the next section with LangChain.

What is Langchain?

Langchain is a cutting-edge framework built on large language models that enables prompt engineering and empowers developers to create applications that interact seamlessly with users in natural language. It provides a structured way to incorporate prompts and generate responses from large language models, making it easier to build intelligent and interactive applications. The LangChain contains modules that are wrappers to other popular language models like Hugging Face and OpenAI.

Installing Langchain

Let’s start with a simple example. We will be using OpenAI (need an API key from OpenAI)

$ pip install langchain, openai
from langchain.llms import OpenAI

import os
os.environ["OPENAI_API_KEY"] = "YOUR openai API KEY"
llm = OpenAI(temperature=0.9)
text = "Tell me a joke"
print(llm(text))

OUTPUT:
Q: What did one ocean say to the other?
A: Nothing, they just waved.

Note: We can also connect to HuggingFace API to connect to LLM’s with the below code

from langchain import HuggingFaceHub
os.environ['HUGGINGFACEHUB_API_TOKEN'] = 'Your Hugging Face API Token'

The Open AI model did a good job with the response. This is simple text input and text output. But our queries are not always simple so how do you customize our inputs and also let the model give the output in the format we need? Let’s find that in the next section — prompt engineering.

Understanding Prompt Engineering

Prompt engineering is a crucial aspect of working with large language models like Langchain. It involves crafting effective prompts or instructions to guide the model’s responses. By carefully designing prompts, developers can elicit the desired behavior or output from the model, effectively customizing its functionality.

from langchain import PromptTemplate

# creating a Prompt Template
template = """The following is a conversation between a parent and a
child. The parent tends to give funny to child's questions:

Child: {query}

Parent: """

# assigning the template to the PromptTemplate Class
prompt = PromptTemplate(
input_variables=["query"],
template=template
)

Here, we are asking the model to be funny with its responses. The role play here is the conversation between a child and a parent.

llm = OpenAI(model_name="text-davinci-003",temperature=1)

# model's output to the query
print(
llm(prompt.format(
query='Why is sky blue?')
)
)

OUTPUT:
That's a good question! I think it's because the sky likes to
wear blue shirts on sunny days.

Utilizing pre-built prompt templates

In the previous section, we created a simple prompt to interact with the model. The LangChain also has built-in templates. These prompt templates are predefined prompts or instructions that can be easily customized to suit specific tasks or applications. These templates provide a starting point and can be modified or extended to generate tailored responses. By leveraging prompt templates, developers can streamline the prompt engineering process and achieve more consistent and accurate outputs from the language model. In the below example, we are stitching together multiple questions.

template = """Answer the following questions one at a time.

Questions:
{questions}

Answers:
"""
long_prompt = PromptTemplate(
template=template,
input_variables=["questions"]
)

qs = [
"Who won men wimbeldon in 2015?",
"How many hours does it take to travel from Delhi to New york?",
"What is the biological name of flower Hibiscus?"
]

print(
llm(prompt.format(
query=qs)
)
)

OUTPUT:
Oh wow, that's a really good question! Let's see, who won Wimbledon in 2015?
Well that was Novak Djokovic. How many hours does it take to travel from
Delhi to New York? Hmm, about 16 hours. And what is the biological name of
the flower Hibiscus? Ah, that would be Hibiscus rosa-sinensis!

Agents in LangChain

While Large Language Models possess remarkable capabilities, they often lack fundamental functionalities like logic and calculation, which can be handled more efficiently by small calculator programs. In contrast, agents are equipped with specialized tools and toolkits that enable them to perform specific actions. For example, the Python Agent utilizes the PythonREPLTool to execute Python commands. In this collaborative setup, the Large Language Model provides instructions to the agent regarding which code to run.

from langchain.agents.agent_toolkits import create_python_agent
from langchain.tools.python.tool import PythonREPLTool
from langchain.python import PythonREPL
from langchain.llms.openai import OpenAI

# creating Python agent
agent_executor = create_python_agent(
llm=OpenAI(temperature=0, max_tokens=1000),
tool=PythonREPLTool(),
verbose=True
)

agent_executor.run("What is 2.45 raised to the power 3.78?")

OUTPUT:
I need to use the power operator
Action: Python REPL
Action Input: print(2.45 ** 3.78)
Observation: 29.58338046366395

Thought: I now know the final answer
Final Answer: 29.58338046366395

> Finished chain.
29.58338046366395

The power of Langchain

Langchain empowers developers with several powerful capabilities, including:

  • Natural Language Interaction: Langchain allows users to interact with applications using natural language, providing a more intuitive and user-friendly experience.
  • Customizability: Through prompt engineering and prompt templates, Langchain enables developers to fine-tune and customize the behavior of the language model to suit specific application requirements.
  • Versatility: Langchain can be applied to a wide range of applications, including chatbots, virtual assistants, content generation, language translation, and much more.
  • Rapid Development: With Langchain, developers can quickly prototype and build intelligent applications without the need for extensive language model training or expertise.
  • Scalability: Langchain leverages the power of large language models like GPT-3, which can handle a wide variety of language-related tasks, making it suitable for both small-scale and enterprise-level applications.

Conclusion:

Langchain and the concept of prompt engineering have revolutionized the way we interact with large language models. By harnessing the power of these models, developers can create intelligent applications that understand and respond to natural language inputs. With the ability to customize prompts and leverage prompt templates, Langchain opens up a world of possibilities for building innovative and user-friendly applications. There is a lot more to explore in LangChain and in this blog, we learned the below topics.

  • LangChain simplifies the utilization of diverse Language Models, facilitating seamless work with different models.
  • Agents created using LangChain can undertake tasks beyond the capabilities of basic Language Models.
  • By modifying the Prompt Template, multiple queries can be inputted into the Language Models.
  • Accurate answers can be obtained by combining one or more Language Models with Chain Components.
  • LangChain significantly reduces the time required to switch from one Language Model to another.

I hope you liked the article and found it helpful.

You can connect with me — on Linkedin and Github

References

Quick start guide on LangChain

Leave a Reply

Your email address will not be published. Required fields are marked *