Building a Question-Answering App with LangChain
Tags: chatgpt, gpt3, ai
Published: 2023-04-26 08:18:06

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:

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

```python
pip install openai
```
**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:

```python
import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"
```


**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:

```python
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003")
```

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

```python
question = "Which is the best programming language to learn in 2023?"
print(llm(question))
```

```python
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.
```


**Step 4 – Creating a Prompt Template**
 

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

```python
question = "What are the top 4 resources to learn Golang in 2023?"
print(llm(question))
```

```python
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
```


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:

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



**Step 5 – Running Our First LLM Chain**
 

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

```python
llm = OpenAI(model_name="text-davinci-003")
prompt = PromptTemplate(template=template,input_variables=['k','this'])
```


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:

```python
input = {'k':3,'this':'Rust'}
print(chain.run(input))
```


```python
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.
```