DEV Community

Liam Tobei
Liam Tobei

Posted on

Building a Question-Answering App with LangChain

Let’s build a question-answering app using LangChain in five easy steps:

Step 1 – Setting Up the Development Environment
Before we get coding, let’s set up the development environment. I assume you already have Python installed in your working environment.

You can now install the LangChain library using pip:

pip install langchain
Enter fullscreen mode Exit fullscreen mode

As we’ll be using OpenAI’s language models, we need to install the OpenAI SDK as well:

pip install openai
Enter fullscreen mode Exit fullscreen mode

Step 2 – Setting the OPENAI_API_KEY as an Environment Variable

Next, sign into your OpenAI account. Navigate to account settings > View API Keys. Generate a secret key and copy it.

In your Python script, use the os module and tap into the dictionary of environment variables, os.environ. Set the "OPENAI_API_KEY" to your to the secret API key that you just copied:

import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"
Enter fullscreen mode Exit fullscreen mode

Step 3 – Simple LLM Call Using LangChain

Now that we’ve installed the required libraries, let's see how to make a simple LLM call using LangChain.

To do so, let’s import the OpenAI wrapper. In this example, we’ll use the text-davinci-003 model:

from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003")
Enter fullscreen mode Exit fullscreen mode

Let's define a question string and generate a response:

question = "Which is the best programming language to learn in 2023?"
print(llm(question))
Enter fullscreen mode Exit fullscreen mode
Output >>
It is difficult to predict which programming language will be the most popular in 2023. However, the most popular programming languages today are JavaScript, Python, Java, C++, and C#, so these are likely to remain popular for the foreseeable future. Additionally, newer languages such as Rust, Go, and TypeScript are gaining traction and could become popular choices in the future.
Enter fullscreen mode Exit fullscreen mode

Step 4 – Creating a Prompt Template

Let's ask another question on the top resources to learn a new programming language, say, Golang:

question = "What are the top 4 resources to learn Golang in 2023?"
print(llm(question))
Enter fullscreen mode Exit fullscreen mode
Output >>

1. The Go Programming Language by Alan A. A. Donovan and Brian W. Kernighan
2. Go in Action by William Kennedy, Brian Ketelsen and Erik St. Martin
3. Learn Go Programming by John Hoover
4. Introducing Go: Build Reliable, Scalable Programs by Caleb Doxsey
Enter fullscreen mode Exit fullscreen mode

While this works fine for starters, it quickly becomes repetitive when we’re trying to curate a list of resources to learn a list of programming languages and tech stacks.

Here’s where prompt templates come in handy. You can create a template that can be formatted using one or more input variables.

We can create a simple template to get the top k resources to learn any tech stack. Here, we use the k and this as input_variables:

from langchain import PromptTemplate
template = "What are the top {k} resources to learn {this} in 2023?"
prompt = PromptTemplate(template=template,input_variables=['k','this'])
Enter fullscreen mode Exit fullscreen mode

Step 5 – Running Our First LLM Chain

We now have an LLM and a prompt template that we can reuse across multiple LLM calls.

llm = OpenAI(model_name="text-davinci-003")
prompt = PromptTemplate(template=template,input_variables=['k','this'])
Enter fullscreen mode Exit fullscreen mode

Let’s go ahead and create an LLMChain:
from langchain import LLMChain
chain = LLMChain(llm=llm,prompt=prompt)

You can now pass in the inputs as a dictionary and run the LLM chain as shown:

input = {'k':3,'this':'Rust'}
print(chain.run(input))
Enter fullscreen mode Exit fullscreen mode
Output >>

1. Rust By Example - Rust By Example is a great resource for learning Rust as it provides a series of interactive exercises that teach you how to use the language and its features.

2. Rust Book - The official Rust Book is a comprehensive guide to the language, from the basics to the more advanced topics.

3. Rustlings - Rustlings is a great way to learn Rust quickly, as it provides a series of small exercises that help you learn the language step-by-step.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)