LangChain Big Model Application Development Guide - AI New Capabilities Derived from Big Models

Last class,I introduced the three core concepts of Chain,Agent,and Callback in LangChain,using traditional application programming design patterns and thinking as the entry point and comparison object. I also organized the capabilities and tools that LangChain has built-in for many developers

Last class,I introduced the three core concepts of Chain,Agent,and Callback in LangChain,using traditional application programming design patterns and thinking as the entry point and comparison object. I also organized the capabilities and tools that LangChain has built-in for many developers. Friends who have not seen it can click on the link to view: 'Link'

Today,I will introduce LangChain's new capabilities derived From the AI big model,including Model I/O,Retrieval,and Memory.

Component Overview

After reading today's article,you can come back and take a look at the components and architecture diagram of LangChain,which can basically clarify the components and abstraction layers of LangChain From a global perspective,as well as their related relationships.

This article mainly introduces LangChain's new capabilities derived From the AI big model. The overall outline of this course is as follows:

ModelI/O interaction rather than IO

Traditional application development usually requires defining the format and specifications of input and output (IO,such as text,images,audio,video,etc. The advantage of doing so is that it can ensure the consistency and interpretability of the Data,but it also brings some limitations and inconveniences,such as the need to preprocess and post-process the Data,adapt to different devices and platforms,and consider user habits and preferences.

LangChain provides a new approach: ModelI/O,which directly interacts with the model without worrying about the details of IO. You can imagine it as the mouth and ears we use in daily communication. As shown in the above figure,there are three types of ModelI/O:

type

effect

Prompts

Templates used to provide input to language models,which can define input variables,output formats,partial variables,etc.,in order to generate text that meets user needs

Languagemodels

Parameter customization for artificial intelligence models,which can generate text with different styles and content based on different parameters (such as temperature,maximum length,prefix,etc.

Outputparsers

A class used to parse the output of a language model into more structured information,such as JSON and XML

The core idea of ModelI/O is to utilize the powerful natural language understanding and generation capabilities of AI large models to convert any form of input into natural language,then input the natural language into the model to obtain the output of the natural language,and then convert the natural language into any form of output. The advantage of doing so is that it can simplify the application development process,improve user experience,and increase application scenarios and functions.

The following is an example of a Model I/O interaction that provides dialogue content for multiple roles:

Fromlangchain.promptsImportChatPromptTemplateTemplate =ChatPromptTemplate.From_messages([    (System,YouareahelpfulAIbot. Yournames {name},(Human,"Hello,how are you doing?",(AI,"I'm doing well,thanks!",(Human,{user_input}Template. format_ Messages (name=Bob,user_input=What is your name

Retrieveval - Retrieve rather than query

Traditional application development usually requires defining the format and specifications of queries, such as keywords, labels, classifications, etc. The advantage of doing so is that it can ensure the accuracy and effectiveness of the query, but it also brings some limitations and inconveniences, such as the need to standardize and optimize the query, adapt to different Data sources and interfaces, and consider user intentions and needs.

LangChain provides a new approach: Retrieve, which directly retrieves the required information From the model after defining the Data source and loading method, without worrying about the details of the query. You can imagine it as a language model using search engines to search for relevant content. As shown in the above figure, we can divide the capabilities of Retrieval into the following parts:

ability

effect

DocumentLoaders

type

Documenttransformers

Transform the document, extract relevant parts, and divide them into small pieces

TextEmbeddingmodels

Create semantic Embeddings for documents to achieve fast and effective similarity search

Vectorstores

type

Retrievers

Retrieve Data From a Database, supporting multiple retrieval algorithms and optimization methods

RetrievalLangChainRetrievalAIability

The following is an example of using MultiQueryRetriever:

#BuildasamplevectorDBFrom langchain.vectorstores Import ChromaFrom langchain.document_Loaders Import WebBaseLoaderFrom langchain.Embeddings.openai ImportOpenAIEmbeddingsFrom langchain.Text_ Splitter Import RecursiveCharacterTextSplitter#LoadblogpostLoader=WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/"Data=Loader.load(#SplitText_ Splitter=RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=0Splits=Text_ Splitter.split_documents(Data#VectorDBEmbedding=OpenAIEmbeddings(Vectordb=Chroma.From_documents(documents=Splits,Embedding=Embedding

Memory - Memory rather than storage

Traditional application development usually requires defining the format and specifications of storage, such as databases, files, caching, etc. The advantage of doing so is that it can ensure the security and reliability of data, but it also brings some limitations and inconveniences, such as the need to backup and restore data, adapt to different storage systems and protocols, and consider user privacy and permissions.

LangChainMemoryMemoryabilityMemoryLangChainLangChainMemorytype

type

effect

ConversationBuffer

Memory used to store all messages and metadata in a conversation, which can return a list containing each message in the conversation and its related information, such as sender, receiver, timestamp, etc

ConversationBuffer Window Entity

type

ConversationKnowledgeGraph

Memory used to construct and update knowledge graphs of entities and relationships involved in conversations, which can return a graph structure containing nodes (entities and edges (relationships, as well as some statistical information such as graph size, density, clustering coefficients, etc

ConversationSummary

Memory used to generate a summary of a conversation, which can return a string containing the main content and target of the conversation

ConversationSummary Buffer

Memory used to store conversation summaries, which can return a list containing each sentence of the conversation summary and its related information, such as generation time, confidence level, etc

ConversationTokenBuffer

type

VectorStore

Memory used to convert text or entities in a conversation into vector representations and perform similarity calculations or clustering analysis. It can return a matrix containing vector representations of each text or entity in the conversation, as well as some metrics such as cosine similarity, Euclidean distance, etc

The core idea of Memory is to utilize the powerful parameters and data capacity of AI large models to convert any form of data into natural language and use it as input or output to the model. The advantage of doing so is that it can simplify the application development process, improve data processing speed, and increase data sources and quality.

The following is an example of using memory in a chain:

FromLangchain.llmsImportOpenAIFromLangchain.promptsImportPromptTemplateFromLangchain.chainsImportLLMChainFromLangchain. memoryImport ConversationBufferMemoryllm=OpenAI(temperature=0# Notice that Chat_history is present in the prompt TemplateTemplate=prompt=PromptTemplate.From_Template(Template#Noticethatneedtoalign the ` memory_ Key`memory=ConversationBufferMemory(memory_key=Chat_historyconversation=LLMChain(    llm=llm,prompt=prompt,verbose=True,memory=memory

summary

LangChainAIabilityModel I/ORetrievalMemoryAIIOQueryStorageabilityAI

AIability

In the next lesson, we will integrate the six core concepts and components of LangChain discussed in the previous two lessons, providing a practical application example that runs through all aspects. Please continue to pay attention.


Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.(Email:[email protected])