Function 1: Let the model have memory
It can be seen that as a plug-in for accessing gpt, langchain allows the gpt model to record the previous conversations through the memory variable, so that the model has memory (without changing the model parameters).
Check what the memory variable contains: You can set the length of the memory (remember the previous conversations)
through the parameter k (or parameters such as max_tokens):
Why use langchain
Large models do not have the concept of "state" (compared to models such as lstm).
The langchain plugin can solve this problem.
Function 2: contextual connection
That is, the next dialog (output) needs some information from the previous dialog (as input).
normal chain
Suppose a csv file is read:
The function llmchain needs to accept two variables, model and prompt:
Sequetial Chain
In this type of chain, the dialogue before and after has an input-output relationship.
Let's see how to use it:
The output is as follows:
The schematic diagram of the simple chain is as follows:
When multiple conversations are mixed, you can do this:
The schematic diagram at this time is as follows:
Router Chain
It is used for processing in different fields, and different prompts are given for different fields (you need to define which kinds of prompts you have in advance).
For example, when a teacher with multiple subjects needs to answer questions in a certain field, first define prompts for different subjects:
Then define all promt information:
then start calling MultiPromptChain
functions such as:
also define the default chain:
Function 3: Answer questions according to the document
Need to use embedding and vector storage (Vector Database) technology to perform retrieval, matching and other operations in the uploaded documents.
How does Kangkang achieve it? Suppose a csv file is uploaded, and gpt needs to answer questions based on the uploaded file.
But sometimes when the document is very large, gpt cannot input such a long content (the token input by the LLM model is limited): at
this time, embedding and vector storage technology must be introduced.
embedding
That is, using vector to express the content of a certain paragraph, when the similarity between the two sentences is relatively high, their corresponding embedding results are also very similar:
Vector Database
Embedding each piece of content, and then storing their embeddings according to the index.
When entering a query to query, the query is also embbedding, and then query the similarity between it and the embedding of all sentences stored before, and select a few sentences with relatively high similarity to answer:
Code
Load a document:
Check the usage of embedding, you can see that it has 1536 latitudes:
Create a vector database and find sentences with high similarity:
Four documents are returned:
Retrieve the required (high similarity) content: