Summarize the following text in less than 50 words. Let's think step by step: rag architecture: advanced rag last updated on july 22, 2024 by editorial team author(s): igor novikov originally published on towards ai. since the writing of my last article, not much time has passed, but progress doesn't stand still, and several important changes have occurred. here, i won't cover the basics read the original article for that. context the first significant change is the substantial increase in the context window size and the decrease in token costs. for example, the context window size of the largest model claude from anthropic is over 200,000 tokens, and according to the latest news, gemini's context window can reach up to 10 million tokens. under these conditions, rag (retrieval-augmented generation) may not be required for many tasks (or at least not all of its components) since all the data can fit into the context window. we've already encountered several financial and analytical projects where the task was completely solved without using a vector database as an intermediate storage. the trend of token cost reduction and context window size increase is likely to continue, reducing the relevance of using external mechanisms for llms. however, they are still required for now. if, however, the context size is still insufficient, different methods of summarization and context compression have been devised. langchain has introduced a class aimed at this: conversationsummarymemory. llm = openai(temperature=0) conversation_with_summary = conversationchain( llm=llm, memory=conversationsummarymemory(llm=openai()), verbose=true ) conversation_with_summary.predict(input="hi, what's up?") knowledge graphs as the amount of data llms have to navigate grows, the ability to navigate this data becomes increasingly important. sometimes, without being able to analyze the data structure and other attributes, it's impossible to use them effectively. for example, suppose the data source is a company's wiki. the wiki has a page with the company's phone number, but this isn't explicitly indicated anywhere. so, how does the llm understand that this is the company's phone number? it doesn't, which is why standard rag won't provide any information about the company's phone number (as it sees no connection). how does a person understand that this is the company's phone number in this case? it's simple the page is stored in a subdirectory called "company information." so, a person can understand what the data means from the convention of how the data is stored (i.e., from the structure or metadata) and use it effectively. for llms, this problem is solved with knowledge graphs with metadata (also known as knowledge maps), which means the llm has not only the raw data but also information about the storage structure and the connections between different data entities. this approach is also known as graph retrieval-augmented generation (graphrag). graphs are excellent for representing and storing heterogeneous and interconnected information in a structured form, easily capturing complex relationships and attributes among different types of data, which vector databases struggle with. example of a knowledge graph how to create a knowledge graph? this is an interesting question. usually, this process involves collecting and structuring data, requiring a deep understanding of both the subject area and graph modeling. this process can largely be automated with llms (surprise ). thanks to their understanding of language and context, llms can automate significant parts of the knowledge graph creation process. by analyzing textual data, these models can identify entities, understand their relationships, and suggest how best to represent them in a graph structure. a vanilla rag looks something like this: the modified process will look like this: so, in fact, this is an ensemble of a vector database and a knowledge graph. as i mentioned in the section on ensembles in the previous article, they generally improve accuracy and often include a search through a regular database or by keywords (e.g., elasticsearch). i won't describe the vector retriever as it is covered in the first article. but let's look at the knowledge graph retriever. as mentioned above, the most obvious way is to ask the llm. for example, a user asks a question about the company's phone number: if you do this in code, you can ask to format the found entities in json format or use with_structured_output from langchain. so, the entities from the question are extracted what next? next, we'll look at 100500 use cases from our company on how we applied this . just kidding. next, we need to search for these entities in the knowledge graph. how this is done depends on where the graph is stored. there are already many graph storage solutions (though companies often make their own versions), so let's take nebula as an example. documents = parse_and_load_data_from_wiki_including_metadata() graph_store = nebulagraphstore( space_name="company wiki", tags=["entity"] ) storage_context = storagecontext.from_defaults(graph_store=graph_store) index = knowledgegraphindex.from_documents( documents, max_triplets_per_chunk=2, space_name=space_name, tags=tags=["entity"] ) query_engine = index.as_query_engine() response = query_engine.query("tell me more about our company") as you can see, the search is not much different from searching in a vector database, except that we search for attributes and related entities, not similar vectors. returning to the first question, since the wiki structure was transferred to the graph, if everything worked correctly, the company's phone number would be added as a related entity in the graph. then, we pass these data and the data from the vector database search to the llm to generate a complete answer. it looks simple, but there are a few problems. access control the first problem is that access to data may not be uniform. in the same wiki, there may be roles and permissions, and not every user can potentially see all the information. this problem also exists for search in the vector database. so, the issue of access management arises. this problem is further complicated by the fact that there are many different approaches and their hybrids, and for example, anyone who has worked with sharepoint knows that those who have will not laugh at the circus. there is at least role-based access control (rbac), attribute-based access control (abac), and relationship-based access control (rebac) and their combinations. generally speaking, user directories (like active directory), for example, also represent a graph where the access question is approximately "is there a path from node user u to node resource r." if such a path exists, access is granted. permissions and categories are also a form of metadata, and for this whole system to work, these metadata must be preserved at the data ingestion stage in the knowledge graph and vector database. correspondingly, when searching in the vector database, it is necessary to check on the found documents whether the role or other access attributes correspond to what is available to the user. some (especially commercial corporate vector) databases already have this functionality as standard. this will not work if the data was embedded in the llm during training . here, one has to rely on the llm's reasonableness, and i would not do that for now. additionally, it is possible to put a censor (guard) on top, filtering the model's output if something slips through. everyone knows lakera; our company also developed a similar product. ingestion and parsing data needs to be somehow inserted into the graph, as well as into the vector database. however, for the graph, the format is critical, as it reflects the data structure and serves as metadata. here begins the nightmare of all data scientists, also known as pdf format. you can put everything in a pdf: tables, images, text, graphics. but getting it all back out is sometimes impossible (especially nested tables). there are different frameworks and libraries that do this with varying degrees of success, llama parse being the most notable one. unfortunately, there is no good solution for this yet, and sometimes it is easier to use ocr or recognize a document image instead of parsing. maybe someone will create a separate model focused only on parsing pdfs into something more acceptable, but dreaming doesn't hurt. in general, the current focus is on improving the quality of answers. besides using knowledge graphs, there are several approaches: crag (corrective retrieval augmented generation) we've seen that rag sometimes gives incorrect results, and different methods can be used to evaluate them, for example, the llm itself (or some lighter version). if the results are not relevant, prompt correction, graph search, or even google search can occur. crag goes a bit further, offering a framework that automates this process. essentially, this is another graph, implementing a state machine (surprise ), which looks something like this: to implement it, it's easiest to use langgraph, which will be discussed further. self-rag self-reflective rag is based on research claiming that this approach provides better results than regular rag. overall, the idea is very similar to the previous one (crag) but goes further. the idea is to fine-tune the llm to generate self-reflection tokens in addition to the regular ones. this is very convenient, as there is no need to guess how confident the llm is and what to do with it. the following tokens are generated: - retrieve token determines whether d chunks need to be retrieved for a given prompt x. options: yes, no, continue - isrel token determines whether chunk d from d is relevant to the given prompt x. options: relevant and irrelevant - issup token determines whether the llm's response y to chunk d is supported by chunk d. options: fully supported, partially supported, no support - isuse token determines whether the llm's response to each chunk d is a useful answer to the query x. options represent a usefulness scale from 5 to 1. using these tokens, a state machine can be built, using the aforementioned langgraph, which looks something like this: for more details, see here. hyde another method, similar to rag fusion, is that it modifies the usual rag retrieval process. hyde stands for hypothetical document embeddings and is based on the study "precise zero-shot dense retrieval without relevance labels." the idea is very simple instead of using the user's question for searching in the vector database, we use the llm to generate a response (a virtual hypothetical document) and then use the response for searching in the vector database (to find similar answers). why all this? sometimes users' questions are too abstract and require more context, which the llm can provide, and without which the search in the database makes no sense. i think this is not an exhaustive review of the new changes; if i forgot something write in the comments. join thousands of data leaders on the ai newsletter. join over 80,000 subscribers and keep up to date with the latest developments in ai. from research to projects and ideas. if you are building an ai startup, an ai-related product, or a service, we invite you to consider becoming a sponsor. published via towards ai