You are currently viewing What is LangChain?

What is LangChain?

LangChain is a framework written in Python and JavaScript that provides tools to manipulate and build applications based on LLMs. You can see LangChain installation instructions here . LangChain aims to solve problems when working with LLMs so below are the core modules of LangChain.

Model I/O

There are many LLMs for you to use (OpenAI, Hugging Face…), LangChain provides an interface so you can interact with different models without any difficulty.

image.png

You can use model LLMs from providers like OpenAI with an API key:

from langchain_openai import ChatOpenAI
from langchain_openai import OpenAI

llm = OpenAI(openai_api_key="...")
chat_model = ChatOpenAI()

You can also interact directly with models running locally using Ollama . This is an open source project, used to interact with LLMs without having to connect to external service providers. We will learn about Ollama in another article. Here is an example of using Ollama in LangChain:

from langchain_community.llms import Ollama
from langchain_community.chat_models import ChatOllama

llm = Ollama(model="llama2")
chat_model = ChatOllama()

With objects llmand chat_model, you can start interacting with the LLMs model.

Prompts

LangChain provides a prompt template that helps you structure the input for LLMs effectively. You can create a dynamic prompt, containing parameters that change depending on the intended use.

prompt = PromptTemplate(
    template="Tell me a joke in {language}", input_variables=["language"]
)

print(prompt.format(language="spanish"))
'Tell me a joke in spanish'

Output Parsers

The output of an LLM is text, in many cases you want the returned result to be another format such as JSON, CSV… output parsers will help you do this. Let’s look at the example below:

template = "Generate a list of 5 {text}.\n\n{format_instructions}"

chat_prompt = ChatPromptTemplate.from_template(template)
chat_prompt = chat_prompt.partial(format_instructions=output_parser.get_format_instructions())

chain = chat_prompt | chat_model | output_parser
chain.invoke({"text": "colors"})
['red', 'blue', 'green', 'yellow', 'orange']

Retrieval

Typically, LLMs will be limited by the data set at training time. Just like Chat-GPT will not be able to answer questions related to events that take place after 2021. In many cases, you also need the model to understand other documents you request. RAG (Retrieval Augmented Generation) was born to solve these problems.

image.png

LangChain provides modules to help you build a complete RAG application. From embedding to manipulating externally added data.

Document Loaders

This is a module that supports loading documents from sources such as Github, S3… along with many different formats such as .txt.csv…:

from langchain_community.document_loaders import TextLoader

loader = TextLoader("./main.rb")
documents = loader.load()
[Document(page_content='puts("Hello LangChain!")\n', metadata={'source': './main.rb'})]

Text Splitting

After loading data, it will be processed to extract meaningful information, then divided into small parts before moving to the next step.

from langchain.text_splitter import Language
from langchain.text_splitter import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.RUBY, chunk_size=2000, chunk_overlap=200
)

documents = splitter.split_documents(raw_documents)
[Document(page_content='puts("Hello LangChain!")', metadata={'source': './main.rb'})]

Text Embedding Models

The data continues to be converted to vector space and you can also cache them for reuse.

from langchain_community.embeddings import OllamaEmbeddings

embeddings_model = OllamaEmbeddings()
embeddings = embeddings_model.embed_documents(documents)

Vector Stores

Data after conversion to vector can be saved to vector store. LangChain provides a module for you to do this:

from langchain_community.vectorstores import Chroma

db = Chroma.from_documents(documents, OllamaEmbeddings())

Retrievers

Now you can manipulate the above data through retrievers.

from langchain.chains import RetrievalQA
from langchain.llms import Ollama

retriever = db.as_retriever(search_kwargs={"k": 1})
llm = Ollama(model="codellama:7b")

qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
qa.invoke("What's inside this document?")["result"]
'The text "Hello LangChain!" is a string of characters written in the Ruby programming language. It is not clear what "LangChain" refers to or what context it may be used in, so I cannot provide a helpful answer without more information.'

Agents

For each user request, you need to perform different steps to return the desired results. For example, with a simple question, users only need a regular text answer. But when you want to display tabular results or a request to export data to PDF format, you will need to perform additional actions to get the final result. LangChain Agents will help you solve the above problem.

image.png

Each agent can be considered a collection of many tools, a tool will include the following main components:

  • Name : Name of the tool
  • Description : Is a brief description of the tool’s intended use
  • JSON schema : Contains tool input information
  • Function call : Is the main content that will be called when running the tool

In which name, description and JSON schema are the most important components, they are used in all prompts.

Built-In Tool

Lang chain provides many built-in tools for you to use. Here is an example:

from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper

api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100)
tool = WikipediaQueryRun(api_wrapper=api_wrapper)

print("Name:", tool.name)
print("Description:", tool.description)
print("JSON schema:", tool.args)

Tool information:

Name: wikipedia
Description: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.
JSON Schema: {'query': {'title': 'Query', 'type': 'string'}}

You can use the tool simply as follows:

tool.run({"query": "LangChain"})
'Page: LangChain\nSummary: LangChain is a framework designed to simplify the creation of applications '

By using the tool, you can fully interact with external data and on the internet, thereby maximizing the power of the model.

Defining Custom Tools

Not limited by built-in tools, LangChain gives you a way to create your own tool to suit any purpose. To declare a tool you need to use @toola decorator:

from langchain.tools import tool

@tool
def search(query: str) -> str:
    """Look up things online."""
    return "LangChain"

@tool
def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

Then you need to create an agent from the above tools:

from langchain.agents import initialize_agent, AgentType
from langchain.llms import Ollama

tools = [search, multiply]

agent = initialize_agent(
    tools,
    Ollama(),
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)

You can then use:

agent.invoke("Multiply two numbers: 2 and 3")
> Entering new AgentExecutor chain...
Action: multiply(a: int, b: int) -> int
Action_input: {"a": 2, "b": 3}
> Finished chain.
{'input': 'Multiply two numbers: 2 and 3', 'output': 'Action: multiply(a: int, b: int) -> int\nAction_input: {"a": 2, "b": 3}\n'}

Try with another input:

agent.invoke("Go online and search")
> Entering new AgentExecutor chain...
Action: search
Action_input: {'query': {'title': 'Query', 'type': 'string'}}
> Finished chain.
{'input': 'Go online and search', 'output': "Action: search\nAction_input: {'query': {'title': 'Query', 'type': 'string'}}\n\n"}

Agent worked exactly as we expected.

Conclusion

LangChain is a very powerful framework that makes it easy for you to interact and harness the power of LLMs. The article provides you with basic information about LangChain. In the following sections we will use LangChain to solve specific problems that promise to be very interesting.

Source : https://viblo.asia/p/langchain-la-gi-WR5JRBPQJGv

Please follow and like us:
Pin Share

This Post Has 9,684 Comments

  1. pinko

    Pinco yukle etdikdən sonra oyundan zövq alırsız. Pinco azerbaycan versiyası lokal müştərilər üçün əladır pinco giris . Pinco giris etdikdən sonra xoş bonus alırsız. Pinco yukle ilə mobil oyunlar daha əlçatan olur. Pinco kazino ilə oyun daha maraqlı olur. Pinco bonusları yeni istifadəçilər üçün cazibəlidir. Pinco casino istifadəçiləri üçün müxtəlif aksiyalar keçirir. Pinco kazino oyunları yüksək keyfiyyətlidir. Pinco kazino oyunu real uduşlarla təchiz olunub [url=https://pinco-kazino.website.yandexcloud.net/]pinco giriş[/url].

  2. tipclubteam

    Do you mind if I quote a couple of your articles
    as long as I provide credit and sources back to your website?

    My blog is in the exact same area of interest as yours
    and my users would really benefit from some of the information you present here.
    Please let me know if this okay with you. Many thanks!

  3. Porn

    Quality content is the secret to invite the users to go to see the web
    page, that’s what this web site is providing.

  4. Having read this I believed it was extremely enlightening.
    I appreciate you taking the time and energy to put this content together.
    I once again find myself personally spending a significant amount
    of time both reading and leaving comments. But so what, it was still worthwhile!

  5. Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way, how could we communicate?

  6. It’s wonderful that you are getting thoughts from this piece of writing as well as from our dialogue made
    at this place.

  7. Elmanana songs_jnKi

    бесплатно скачать песню [url=https://muzmo-ru.su]бесплатно скачать песню[/url] .

  8. Ꭲhe upcoming neѡ physical area at OMT guarantees immersive math experiences, triggering lifelong love fⲟr
    the subject and motivation fօr test accomplishments.

    Experience versatile learning anytime, аnywhere tһrough OMT’ѕ detailed online e-learning platform, including limitless access tߋ
    video lessons and interactive quizzes.

    Ƭhe holistic Singapore Math approach, wһіch builds multilayered analytical capabilities,
    highlights ѡhy math tuition iѕ indispensable fߋr mastering the curriculum and preparing for future careers.

    primary tuition іs essential for PSLE as it offerѕ restorative assistance fⲟr topics likе
    whole numbers and measurements, ensuring no foundational weak ⲣoints continue.

    Secondary school math tuition іs crucial foг O Levels as іt
    enhances proficiency of algebraic manipulation, а core element tһat regularly shߋws սp in test concerns.

    By supplying extensive exercise ᴡith paѕt Ꭺ Level exam documents, math
    tuition acquaints students ԝith inquiry layouts ɑnd marking schemes
    fоr optimum performance.

    The exclusive OMT educational program stands ⲟut
    by integrating MOE syllabus aspects ᴡith gamified quizzes and difficulties tо make discovering mοre enjoyable.

    OMT’s ѕystem tracks ʏour enhancement ovеr time ѕia, inspiring you to intend greatеr in mathematics qualities.

    Вү including modern technology, օn the internet math tuition involves
    digital-native Singapore pupils fоr interactive examination modification.

    Mу pаge;Kaizenaire Math Tuition Centres Singapore

  9. By stressing conceptual proficiency, OMT discloses math’ѕ internal charm, igniting love аnd drive for
    leading examination grades.

    Founded іn 2013 by Ⅿr. Justin Tan, OMT Math Tuition һaѕ helped numerous trainees ace examinations ⅼike PSLE, Ⲟ-Levels, аnd A-Levels ԝith proven analytical techniques.

    Singapore’ѕ world-renowned math curriculum highlights
    conceptual understanding ⲟver mere computation, maкing math tuition essential
    fⲟr trainees tto grasp deep concepts ɑnd master national exams ⅼike PSLE and Օ-Levels.

    Tuition stresses heuristic analytical аpproaches, crucial fⲟr taking on PSLE’s
    tough woгd issues tһat require numerous actions.

    Individualized math tuition іn hiցh school addresses
    private finding оut spaces inn subjects like calculus and statistics, avoiding
    tһеm from impeding Օ Level success.

    Τhrough routine simulated exams and detailed responses, tuition aids
    junior college trainees recognize аnd fіx weaknesses before thе actual А Levels.

    OMT’ѕ exclusive mathematics program complements MOE requirements Ьү stressing conceptual proficiency ᧐ver
    rote learning, resulting in deeper long-term retention.

    OMT’ѕ system is straightforward оne, so even newbies cаn browse аnd begin improving qualities swiftly.

    Іn Singapore’s affordable education аnd learning landscape, math
    tuition ⲟffers the extra ѕide required for students to master һigh-stakesexams ⅼike the PSLE, O-Levels, ɑnd A-Levels.

    Μy paցe; Maths tuition near bukit batok West ave 6

  10. pinko

    Pinco giriş bağlantısı həmişə yenilənir. PİNKO kazinosu ilə qazanmaq daha asandır pinco bet . Pinco oyun platforması etibarlıdır və sürətlidir. Pinco slotları yüksək ödəniş faizinə malikdir. Pinco kazino ilə oyun daha maraqlı olur. Pinco kazino slotları ən məşhur provayderlərlə təmin olunur. Pinco giris hər zaman aktual qalır. Pinco apk ilə istənilən yerdə oyna. Pinco app istifadəçiləri üçün fərdi bonuslar verir [url=https://pinco-kazino.website.yandexcloud.net/]pinco giriş[/url].

  11. ラブドール

    but she clearsherself of treachery to him.The letter had been secured from her byfalse representations and in exchange for two others written by hermother just before her birth,ラブドール

  12. Pink Salt trick

    I don’t even know how I ended up here, but I thought this post was good.

    I don’t know who you are but certainly you are going to a famous blogger if you are
    not already 😉 Cheers!

  13. Http://Karayaz.ru/

    Its such as you read my thoughts! You seem to understand so much about this, such as you wrote the guide in it or something.
    I feel that you simply can do with a few % to pressure the message house a little bit, but other than that, this is great blog.

    A fantastic read. I’ll definitely be back.

    References:

    is it illegal to take steroids – http://Karayaz.ru/

  14. Betting Win

    Hi I am so thrilled I found your weblog, I really found you by mistake, while
    I was browsing on Askjeeve for something else, Nonetheless I am here now and
    would just like to say kudos for a fantastic post and a all round thrilling blog (I also love the theme/design), I don’t have time to browse it all at the moment but I have bookmarked it and also included your RSS feeds,
    so when I have time I will be back to read much more,
    Please do keep up the fantastic work.

  15. Lindsey

    I every time emailed this website post page to all my contacts,
    for the reason that if like to read it then my links will too.

  16. big boos

    Amazing blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my blog stand out.
    Please let me know where you got your theme. Kudos

  17. web site

    Link exchange is nothing else except it is only placing the other person’s web site link on your page
    at suitable place and other person will also do same in favor of you.

  18. vsegda-pomnim.Com

    Good day! This is kind of off topic but I need some help from an established blog.
    Is it tough to set up your own blog? I’m not very techincal
    but I can figure things out pretty quick. I’m thinking about setting up my own but I’m not sure where
    to start. Do you have any points or suggestions? Thanks

    References:

    Best Lean muscle building stack, vsegda-pomnim.Com,

  19. discover this

    This blog was… how do I say it? Relevant!! Finally I have found something that helped me.
    Appreciate it!

  20. You are so awesome! I do not think I have read
    a single thing like this before. So nice to find somebody with genuine thoughts on this subject.
    Seriously.. thank you for starting this up. This
    site is one thing that’s needed on the internet, someone with a bit of originality!

    References:

    Safest Steroid Cycle (Hedge.Fachschaft.Informatik.Uni-Kl.De)

Leave a Reply