LLM Essentials: Getting start with LangChain and OpenAI API
note LangChain version v0.0.329
Introduction
In this age, where the word “AI” is often thrown around needlessly, the rise of chatbots like ChatGPT and Google Bard has shown that these tools can be used for a variety of tasks, including writing reports, emails, and even resumes (and even some parts of this blog!).
It is important to understand what AI really is and how to use it with your programs. In this article, I will guide you on how to use AI in Python. Let’s get started!
What is LLM?
Many of you have used it before in apps like ChatGPT or GitHub Co-pilot.
But what really an LLM?
LLM stands for Large Language Model. It is a type of artificial intelligence (AI) model that can generate and understand human language. LLMs are trained on massive datasets of text and code, which allows them to learn the patterns and rules of language. This enables them to perform a variety of tasks, including:
- Generating text, such as poems, code, scripts, musical pieces, emails, letters, etc.
- Translating languages
- Answering questions in a comprehensive and informative way
- Writing different kinds of creative content
- Classifying text
LLMs have revolutionised the way we interact with computers. They can be used to create new and innovative applications in a variety of fields, including education, healthcare, customer service, and entertainment.
Some examples of LLMs include GPT-3 (which we will explore on this article) and Bard.
Why LangChain?
LangChain is a powerful building block for building natural language processing (NLP) applications with large language models (LLMs). It offers a number of advantages over other approaches, including:
- Standardized interfaces: LangChain provides a standardized interface for interacting with LLMs, making it easier for developers to build and deploy NLP applications.
- Extensibility: LangChain is extensible, allowing developers to add new features and functionality to their applications.
- Integration with other tools and frameworks: LangChain can be integrated with other tools and frameworks, such as vector databases and code generators, to create even more powerful NLP applications.
While it is technically true that LLM application is NLP, it is important to understand that
Using LLM does not require any NLP knowledge.
(maybe just a fraction of the NLP concept)
OpenAI API
One of the simplest way to use LLM is via OpenAI API. The OpenAI API is a cloud-based interface that gives developers access to OpenAI’s AI models, such as GPT-3, Codex, and DALL-E 2. These models can be used to generate and understand text, translate languages, write different kinds of creative content, and answer your questions in an informative way.
In this article, I will be focusing on the usage of GPT-model from OpenAI.
Lang Chain and OpenAI API
Getting the keys from OpenAI API
To prevent this article from becoming obsolete in the next few months, I will provide essential instructions on how to retrieve your OpenAI API key.
Navigate to the following page:
https://platform.openai.com/account/api-keys
To access this page, you will need an OpenAI API account and set soft and hard limits for your API usage. If you are developing an automated system, I recommend setting a hard limit of 30 USD (It can gets really wild, speaking from direct experience).
Remember to save the key somewhere safe and do not worry if you lost it since you can always get a new one for free. The key will look like “sk-xxxxxxxxxxxxxxxx”.
Installing LangChain
I would recommend creating an environment for your langchain project using conda:
conda create -n mylangchainproject python=3.10 # uses python 3.10 because the LangFlow gui editor but dont worry about that for now
conda activate mylangchainproject
pip install langchain python-dotenv openai
or install it directly into you environment ***highly not recommended, even if you use the Conda base environment***
pip install langchain python-dotenv openai
Lets see it in action!
Imports
main.ipynb
#main.ipynb
import os # for accessing environment variables
from dotenv import load_dotenv # for loading environment variables from .env file
from abc import ABC, abstractmethod # abstract class, dont worry about this yet I will explain later in another article
# imports from langchain package
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
# load environment variables from .env file
load_dotenv()
Using the key
main.ipynb
remember the key that we got it from OpenAI site earlier, this is where we will use it.
There are two ways to use the key
best practice would be putting it in an environment variable
— or —
bad practice (that saves you 1 minute from putting it in the env file or export it to you system) would be putting it directly in your code.
#main.ipynb
# Your OpenAI API Key loaded from .env file
# this code works both ways
# by opting in accessing your env first and
# "replace this with your key if you dont have an env file"
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY","replace this with your key if you dont have an env file")
llm = OpenAI(openai_api_key=OPENAI_API_KEY) # Language Model
chat_model = ChatOpenAI(openai_api_key=OPENAI_API_KEY) # Another LLM interface
Try out your first prompt
question = "What is the meaning of life?"
# use llm.predict to get the answer
answer = llm.predict(question).strip()
print(question)
print(answer)
# use chat_model.predict to get the answer
answer = chat_model.predict(question).strip()
print(question)
print(answer)
output
What is the meaning of life?
The meaning of life is subjective and can vary significantly from person to person. It may involve finding purpose and fulfillment, creating meaningful relationships and connections, pursuing passions, and discovering one's true self. Ultimately, the meaning of life is up to the individual to define.
What is the meaning of life?
The meaning of life is a philosophical question that has been debated for centuries. Different individuals and cultures may have different interpretations and beliefs about the purpose and significance of life. Some may find meaning in religious or spiritual beliefs, others may seek fulfillment through personal relationships, experiences, or the pursuit of knowledge and self-improvement. Ultimately, the meaning of life may vary from person to person, and it is a subjective concept that each individual must find and define for themselves.
Getting more advanced with prompt templating
#implementing prompt template for chatbot to enable parameterized questions
promptTemplate = PromptTemplate.from_template(
"What is {topic} in the context of {context}?"
)
# this format the prompt template with the given parameters
prompt = promptTemplate.format(
topic="the meaning of life",
context="philosophy")
print(prompt)
output
What is the meaning of life in the context of philosophy?
Creating a wrapper function for it
def get_answer(topic, context):
"""
This function returns the answer to the question "What is {topic} in the context of {context}?"
in dictionary format
"""
promptTemplate = PromptTemplate.from_template(
"What is {topic} in the context of {context}?"
)
question = promptTemplate.format(
topic=topic,
context=context)
answer = chat_model.predict(question).strip()
return {"question": question, "answer": answer}
pp = pprint.PrettyPrinter(indent=4) # for pretty printing the output
pp.pprint(get_answer("the meaning of life", "biology"))
pp.pprint(get_answer("the meaning of life", "philosophy"))
pp.pprint(get_answer("the meaning of life", "physics"))
output formatted by pprint
{ 'answer': 'In the context of biology, the meaning of life refers to the '
'purpose or significance of living organisms and their '
'existence. The field of biology seeks to understand the '
'mechanisms and processes that govern life and its various '
'forms, from single-celled organisms to complex multicellular '
'organisms. The fundamental goal of all living organisms, as '
'dictated by biology, is to survive, reproduce, and pass on '
'their genetic information to the next generation. Therefore, '
'the meaning of life in the context of biology revolves around '
'the continuation and propagation of life through these '
'fundamental processes.',
'question': 'What is the meaning of life in the context of biology?'}
{ 'answer': 'In the context of philosophy, the meaning of life refers to the '
'question of why we exist and what gives our lives purpose and '
'significance. It is a fundamental and complex question that has '
'been explored by various philosophical schools of thought '
'throughout history. Different philosophical perspectives offer '
'different interpretations and answers to this question, '
'reflecting diverse cultural, religious, and individual beliefs. '
'Some key philosophical approaches to understanding the meaning '
'of life include existentialism, nihilism, religious '
'perspectives, and teleological theories. Ultimately, the '
'meaning of life is an individual and subjective inquiry, with '
'each person potentially finding their own unique purpose and '
'significance.',
'question': 'What is the meaning of life in the context of philosophy?'}
{ 'answer': 'In the context of physics, the question about the meaning of '
'life falls outside the scope of the field. Physics is primarily '
'concerned with studying the fundamental laws and principles '
'that govern the natural world, including matter, energy, and '
'their interactions. It addresses questions related to the '
'physical properties of objects, the behavior of particles, the '
'forces between them, and the nature of space and time.\n'
'\n'
'The meaning of life is a philosophical and existential question '
'that pertains to the purpose, significance, and overall value '
'of human existence. It involves pondering subjective and '
'abstract aspects such as personal fulfillment, morality, '
'consciousness, and the pursuit of happiness. These are beyond '
'the realm of physics, which deals with the objective and '
'measurable aspects of the universe.\n'
'\n'
'While physics can provide insights into how life and its '
'processes emerge from the fundamental building blocks of '
'matter, it does not address the philosophical or metaphysical '
"aspects of life's meaning.",
'question': 'What is the meaning of life in the context of physics?'}
Conclusion
In this article, we covered the basic concepts and usage of the OpenAI API with LangChain, written in a Jupyter notebook. We saw how to use prompt templates to enable parameterised prompting, and we introduced the concept of DRY (Don’t Repeat Yourself) to eliminate repetitive code by writing functions.
I hope this article was helpful for your project. I will continue to create content on programming practices and prompting techniques, such as Chain of Thought.
You can find the code here
What’s next
In our next tutorial, we’ll deploy our LangChain app to Streamlit, a popular Python library for building interactive web apps. This will allow us to share our app with others and get feedback on our work.
Refactoring your code
This can involve simplifying the code, making it more modular, and reducing duplication. Refactoring is an important part of the development process, and it can help to make our code more maintainable and efficient.
Dependancy injection… man! we really need to talk about this
Dependency injection is a design pattern that allows us to loosely couple our code. This means that the different components of our code do not depend on each other directly, but instead on a common abstraction. This makes our code more flexible and reusable, and it can also help to reduce bugs.