NLP (56) Introduction to LangChain

Introduction to LangChain

background

  Large Language Model (LLM) has become very popular due to the release of ChatGPT. Maybe you may not have enough money and computing resources to train a large model from scratch, but you can still use a large model to do some cool things, such as:

  • Personal Assistant: Interact with the outside world based on your data
  • Conversational Bots: Conversational Bots That Fit Your Personal Preferences
  • Analysis and summary: analyze and summarize the document or code

  Through various APIs and prompt engineering (Prompt Engineering), large models are changing the way we create products based on AI development. This is why, in the context of LLM, new developer tools are popping up all over the place now, which has a new term: LLMOps (a similar term is DevOps).
  One of these new tools is LangChain.

Introduction to LangChain

  LangChain is a language model-based application development framework. Its role in boosting the application is:

  • Data-aware: can use other data sources to connect a language model
  • Proxy: Allows a language model to interact with its environment

  The main props of LangChain are:

  1. Components: A summary of how the language model is used to do work, each with a collection of actions to perform. Branches are modular and easy to use, whether you are using the rest of the LangChain framework or not.
  2. Ready-made Chains: A structured assembly of branches for specific higher-order tasks

Ready-made chains make it easy to get started. For more complex applications and granular use cases, forks make it easy to adapt existing chains or create new ones.

What LangChain can do

  LangChain provides support for six main modules, which are arranged in order of increasing complexity as follows:

  • Models : Various model types and model integrations supported by LangChain.
  • Prompts (prompts) : including prompt management, prompt optimization and prompt serialization.
  • memory : Memory is the concept of maintaining state between chain/proxy calls. LangChain provides a standard memory interface, a set of memory implementations, and chain/broker examples using memory.
  • Indexes : Language models are often more powerful when combined with your own text data - this module covers best practices for doing this.
  • Chains : A chain is not just a single LLM call, but a sequence of calls (whether calling LLM or different tools). LangChain provides a standard chain interface, many integrations with other tools. LangChain provides end-to-end chain calls for common applications.
  • Agents : Agents involve the LLM in making an action decision, executing that action, looking at an observation, and repeating the process until completion. LangChain provides a standard proxy interface, a series of proxy options, and end-to-end proxy examples.

LangChain Quick Start

  LangChain is an open source project by Harrison Chase , and the Github access URL is: https://github.com/hwchase17/langchain .

  LangChain provides corresponding Python third-party modules. Before installation, you need to ensure that your Python version is greater than or equal to 3.8.1 and less than 4.0. The installation method is as follows:

pip install langchain

  The version of langchain used in this article is 0.0.201. Before starting to introduce the use of LangChain, you also need to have the API key of the relevant large model, such as the OpenAI key.

model support

  LangChain provides support for a series of large models, but first you need the API key of these large models. The large model supported by LangChain is as follows:

  • Proprietary models: Closed-source models developed by companies with large professional teams and large AI budgets. They are usually larger and perform better than open-source models, but API calls are more expensive. Providers of private models include OpenAI, co:here, AI21 Labs, Anthropic, etc.
  • Open-source LLMS (Open Source Models): Smaller and less capable than proprietary models, but they are more cost-effective than proprietary models. Representatives of open source models include BLOOM, LLaMA, Flan-T5, GPT-J, etc. Many open source models are already well supported by Hugging Face.
  • Model Hub (model warehouse): a warehouse for model storage, such as Hugging Face, etc.

Below is sample code for loading different models for langchain:

# Proprietary LLM from e.g. OpenAI
# pip install openai
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003")

# Alternatively, open-source LLM hosted on Hugging Face
# pip install huggingface_hub
from langchain import HuggingFaceHub
llm = HuggingFaceHub(repo_id="google/flan-t5-xl")

This article is mainly based on OpenAI for demonstration, so if you have an OpenAI key, you will have a better experience using langchain.

Prompt management

  The performance of the large model depends on the Prompt (prompt). A good Prompt can make the performance of the large model good, otherwise, the performance of the large model may be unsatisfactory.
  langchain provides PromptTemplatesto help you better create reasonable prompts for different branches. For example, to create an ordinary prompt (prompt template for zero-sample questions), the Python code is as follows:

# -*- coding: utf-8 -*-
from langchain import PromptTemplate

template = "What is a good name for a company that makes {product}?"

prompt = PromptTemplate(
    input_variables=["product"],
    template=template,
)

print(prompt.format(product="colorful socks"))

The output is as follows:

What is a good name for a company that makes colorful socks?

Similarly, langchain also provides a Prompt template for few-shot (less sample) text, and the Python sample code is as follows:

# -*- coding: utf-8 -*-
from langchain import PromptTemplate, FewShotPromptTemplate

examples = [
    {
    
    "word": "happy", "antonym": "sad"},
    {
    
    "word": "tall", "antonym": "short"},
    {
    
    "word": "fat", "antonym": "thin"},
]

example_template = """
-> Word: {word}
-> Antonym: {antonym}
"""

example_prompt = PromptTemplate(
    input_variables=["word", "antonym"],
    template=example_template,
)

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Give the antonym of every input",
    suffix="\n->Word: {input}\n->Antonym:",
    input_variables=["input"],
    example_separator="\n",
)

print(few_shot_prompt.format(input="big"))

The output is as follows:

Give the antonym of every input

-> Word: happy
-> Antonym: sad


-> Word: tall
-> Antonym: short


-> Word: fat
-> Antonym: thin


->Word: big
->Antonym:

Chains

  A chain in langchain describes the process of combining a large model with other branches to create an application. For example, LLMChain allows us to use a large model for the created Prompt. The Python sample code (openai module needs to be installed and used pip install openai) is as follows:

# -*- coding: utf-8 -*-
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain import PromptTemplate

# set api key
import os
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

# install openai and choose model
llm = OpenAI(model_name='gpt-3.5-turbo')

# make prompt
template = "What is a good name for a company that makes {product}?"

prompt = PromptTemplate(
    input_variables=["product"],
    template=template,
)

# chain
chain = LLMChain(llm=llm, prompt=prompt)

# Run the chain only specifying the input variable.
print(chain.run("colorful socks"))

The output is as follows:

Rainbow Socks Co.

  For the few-sample prompt, the Python sample code is as follows:

# -*- coding: utf-8 -*-
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain import PromptTemplate, FewShotPromptTemplate

# set api key
import os
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

# install openai and choose model
llm = OpenAI(model_name='gpt-3.5-turbo')

# make few-shot prompt
examples = [
    {
    
    "word": "happy", "antonym": "sad"},
    {
    
    "word": "tall", "antonym": "short"},
    {
    
    "word": "fat", "antonym": "thin"},
]

example_template = """
-> Word: {word}
-> Antonym: {antonym}
"""

example_prompt = PromptTemplate(
    input_variables=["word", "antonym"],
    template=example_template,
)

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Give the antonym of every input",
    suffix="\n->Word: {input}\n->Antonym:",
    input_variables=["input"],
    example_separator="\n",
)

# chain
chain = LLMChain(llm=llm, prompt=few_shot_prompt)

# Run the chain only specifying the input variable.
print(chain.run("big"))

The output is as follows:

small

  If we want to use the output of the previous LLM as the input of the current LLM, we can use it SimpleSequentialChain. The sample Python code is as follows:

# -*- coding: utf-8 -*-
from langchain.llms import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain

# set api key
import os
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

# install openai and choose model
llm = OpenAI(model_name='gpt-3.5-turbo')

# Define the first chain as in the previous code example
template = "What is a good name for a company that makes {product}?"

prompt = PromptTemplate(
    input_variables=["product"],
    template=template,
)

chain = LLMChain(llm=llm, prompt=prompt)

# Create a second chain with a prompt template and an LLM
second_prompt = PromptTemplate(
    input_variables=["company_name"],
    template="Write a catchphrase for the following company: {company_name}",
)

chain_two = LLMChain(llm=llm, prompt=second_prompt)

# Combine the first and the second chain
overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)

# Run the chain specifying only the input variable for the first chain.
catchphrase = overall_chain.run("colorful socks")
print(catchphrase)

The output is as follows:

> Entering new  chain...
Rainbow Sox Co.
"Step up your sock game with Rainbow Sox Co."

> Finished chain.
"Step up your sock game with Rainbow Sox Co."

Advanced use of LangChain

  Langchain also supports more interesting high-level applications (implemented through plug-ins), such as document Q&A, weather query, mathematical calculation, Q&A based on WikaPedia, etc. The detailed application introduction can be accessed at: https://python.langchain . com/docs/modules/agents/tools . This article will introduce the three plug-in applications of document question answering, weather query, and mathematical calculation.

Document Q&A

  As we all know, ChatGPT’s knowledge base is as of September 2021. Therefore, ChatGPT cannot answer questions after that. For example, when we ask ChatGPT “Who is the Nobel Prize winner in 2022?”, the result is as follows: langchain’s document reading

allows We combine large models with external documents to answer the content of the documents. We look for information about the Nobel Prize winners in 2022 on the Internet, such as the website: https://www.theguardian.com/books/2022/oct/06/annie-ernaux-wins-the-2022-nobel -prize-in-literature , saved as Annie Ernaux.txt, as an external input document for ChatGPT.

  Langchain uses document loader to load data as Document. A Document is a series of text fragments and related metadata. There are three main steps after loading the file:

  1. Split the document into chunks
  2. Create embedding vectors for each document
  3. Storing documents and embedding vectors in a vector library

By default, LangChain uses Chroma as vector storage to index and search embeddings. So we need to install chromadb first, the command is: `pip install chromadb`.

  Based on this, we can do Q&A on external documentation, Python sample code:

# -*- coding: utf-8 -*-
from langchain.llms import OpenAI
from langchain.document_loaders import TextLoader
from langchain.indexes import VectorstoreIndexCreator

# set api key
import os
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

# install openai and choose model
llm = OpenAI(model_name='gpt-3.5-turbo')

# prompt with no answer
prompt = "Who is the winner of 2022 Noble Prize in literature?"
completion = llm(prompt)
print(completion)

# load other source data
loader = TextLoader('Annie Ernaux.txt')
index = VectorstoreIndexCreator().from_loaders([loader])
print('index the document.')

# prompt with answer
query = "Who is the winner of 2022 Noble Prize in literature?"
print(index.query_with_sources(query))

The output is as follows:

As an AI language model, I do not have the ability to predict future events or outcomes such as the winner of the 2022 Nobel Prize in Literature. Only the Nobel Committee can make such announcements.
index the document.
{
    
    'question': 'Who is the winner of 2022 Noble Prize in literature?', 'answer': ' Annie Ernaux is the winner of the 2022 Nobel Prize in Literature.\n', 'sources': 'Annie Ernaux.txt'}

It can be seen that the original ChatGPT cannot give an accurate answer to the question "Who is the winner of 2022 Noble Prize in literature?", but after adding external data, and then using the document Q&A, it can accurately answer the question.

weather query

  ChatGPT cannot query real-time information, such as weather, stock information, etc. The following is an example of ChatGPT answering "What is the weather in Shanghai today?", as shown in the figure below: Therefore,

  we need to use agents (Agents) tools OpenWeatherMap APIto obtain weather information. OpenWeatherMap can get weather information from all over the world, but first you need to register and get it on its official website OPENWEATHERMAP_API_KEY. The following is a Python sample code that uses the proxy tool OpenWeatherMap APIto answer the weather:

# -*- coding: utf-8 -*-
from langchain.llms import OpenAI
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.utilities import OpenWeatherMapAPIWrapper
import os

os.environ["OPENWEATHERMAP_API_KEY"] = "xxx"
os.environ["OPENAI_API_KEY"] = "sk-xxx"

# direct get weather info
weather = OpenWeatherMapAPIWrapper()
weather_data = weather.run("shanghai")
print(weather_data)

# use LLM to do NLU
llm = OpenAI(temperature=0)
tools = load_tools(["openweathermap-api"], llm)

agent_chain = initialize_agent(
    tools=tools,
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# get weather info by natural language
print(agent_chain.run("今天上海天气如何?"))

The output is as follows:

In shanghai, the current weather is as follows:
Detailed status: light rain
Wind speed: 5 m/s, direction: 300°
Humidity: 77%
Temperature: 
  - Current: 28.6°C
  - High: 29.92°C
  - Low: 27.71°C
  - Feels like: 33.09°C
Rain: {
    
    '1h': 0.69}
Heat index: None
Cloud cover: 75%


> Entering new  chain...
 我需要查询上海的天气信息。
Action: OpenWeatherMap
Action Input: Shanghai,CN
Observation: In Shanghai,CN, the current weather is as follows:
Detailed status: light rain
Wind speed: 5 m/s, direction: 300°
Humidity: 77%
Temperature: 
  - Current: 28.6°C
  - High: 29.92°C
  - Low: 27.71°C
  - Feels like: 33.09°C
Rain: {
    
    '1h': 0.65}
Heat index: None
Cloud cover: 75%
Thought: 根据上海的天气信息,我可以得出结论。
Final Answer: 今天上海有轻度降雨,风速为5米/秒,湿度为77%,温度为28.6°C,最高温度为29.92°C,最低温度为27.71°C,体感温度为33.09°C,降雨量为0.65毫米,云量为75%。

> Finished chain.
今天上海有轻度降雨,风速为5米/秒,湿度为77%,温度为28.6°C,最高温度为29.92°C,最低温度为27.71°C,体感温度为33.09°C,降雨量为0.65毫米,云量为75%。

mathematical calculation

  Langchain provides proxy tools Wolfram Alphafor better mathematical calculations. First, you need to register on the official website of Wolfram Alpha and obtain WOLFRAM_ALPHA_APPID, and then install the wolframalpha module. The command is: . The sample pip install wolframalphaPython code is as follows:

# -*- coding: utf-8 -*-
import os
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

os.environ["WOLFRAM_ALPHA_APPID"] = "xxx"

from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper

wolfram = WolframAlphaAPIWrapper()

# 一元一次方程
print(wolfram.run("What is 2x+5 = -3x+7?"))

# 一元二次方程
print(wolfram.run("What is x^2-5x+4=0?"))

# 多项式展开
print(wolfram.run("Expand (x+y)^3?"))

The output is as follows:

Assumption: 2 x + 5 = -3 x + 7 
Answer: x = 2/5
Assumption: x^2 - 5 x + 4 = 0 
Answer: x = 1
Assumption: expand | (x + y)^3 
Answer: x^3 + 3 x^2 y + 3 x y^2 + y^3

Summarize

  This article mainly introduces LangChain, as well as LangChain's model support, prompt management, chain, and on this basis, three interesting tools are introduced.
  In the follow-up, the author will further introduce the use of LangChain, and welcome your attention~

references

  1. Getting Started with LangChain: A Beginner’s Guide to Building LLM-Powered Applications: https://towardsdatascience.com/getting-started-with-langchain-a-beginners-guide-to-building-llm-powered-applications-95fc8898732c
  2. LangChain Document in Python: https://python.langchain.com/docs/get_started/introduction.html
  3. LangChain Agents: https://python.langchain.com/docs/modules/agents/

Guess you like

Origin blog.csdn.net/jclian91/article/details/131304689
#56