Summarize the following text in less than 50 words. Let's think step by step: another few tips for better results from llm rag solutions author(s): dmitry malishev originally published on towards ai. a structured and thoughtful response from llm based on perfectly selected data from rag is a promising technique. and it can be even better! by now, i've completed several projects related to llm based on rag: article analyzers, knowledge-based q&a bots, and insight generators. while working with it i had a chance to try many frameworks, models, and pipelines with an uncountable number of parameters, settings, and other tunings capable of turning these pieces of code into a real magic spell. this article is a compilation of my notes on building a better-performing llm rag pipeline in the hope of helping other engineers with experiments. before digging into the details, let me recall a general llm rag pipeline: as shown in the diagram, we deal with consequential steps to turn a user query into a llm response. in a real project, any step can contain additional pre- and post-processing routines. all steps are currently covered by well-known third-party frameworks. for the rag part, it's either llamaindex or langchain. for the llm part, it's typically a local setup based on open llama architecture or one of the cloud-based gpt services like chatgpt. you may find dozens of additional details just by googling any of them. i'm not going into detail on how the pipeline works, as i'm targeting a more experienced audience. but if you're only starting, i recommend checking out llamaindex and langchain beginner's guides. the basic workflows are easy to understand. there's no need for deep knowledge of large language models and data processing algorithms. all preparations are completed and it's time to get straight to the tips! context length this parameter relates to both llm and rag parts, it defines the final length of the prompt containing a query and a context. often it sets up as a maximum context length supported by the used llm with the intuition "the bigger is the better". meanwhile, i found several side effects of the big context value: - if the required information is contained only in a contiguous location of the knowledge base you may end up with many irrelevant chunks provided by the rag, which may trigger llm to wrong answers. - there's the so-called lost-in-the-middle problem, where llm pays attention to the chunks only at the beginning and at the end of the context and skips the middle. - most of the time a longer context means more vram demand for the pipeline and, consequently, more calculations and processing time. and one last argument! there are llms with really big context sizes (64k, 128k, ) which may feed all the knowledge database and make the rag part entirely unnecessary. it might be interesting to experiment and may work out in certain cases, but as we still have rag frameworks on the scene, big context doesn't solve all the problems. text cleaning depending on the task you may decide to clean the context from all unnecessary symbols (extra spaces, newlines, etc.). it sounds especially appealing when the knowledge database is extracted from text formats like docx and pdf. be careful at this step, as i discovered that some llms pay lots of attention to the information dividing into sections, paragraphs, and bulleted lists, and perform much worse without these special symbols, which, by the way, don't spend too many bytes to store. two parts two queries by default, an input query applies to rag and llm parts unaltered: vector searching, composing a prompt, and sending it to an llm. these parts are so different at the job they do, but we expect it's good enough to feed them with the same input. and this is not mandatory! it's possible to get optimized query modifications and provide them to each part of the pipeline. this is particularly useful when you work with fixed queries to find certain types of answers in the knowledge database. so, you can create two versions of the prompt manually whenever possible or use an llm to compose modified versions. other languages the vast majority of llms are trained on english text data. other languages are added to the training set too, but usually in smaller volumes. trained llms can also be finetuned for other languages, but it's hard. anyway, when english isn't the basic language for your solution, you may find yourself with a small number of options. for this case, i want to share another trick! llms are almost always much better at understanding non-english languages than using them for composing responses. just try to use your knowledge database in the local language, but ask an llm to provide the response in english. this may be enough by itself if english answers are appropriate, if not use a language translation service to turn it into a local language. faster on the same host llm-based solutions work rather slowly. having both a powerful cpu and gpu, you still want to make it faster without digging into low-level algorithmic optimization. i have a little something for this case, too! just try to build your app for an alternative os and launch it in a container (docker). once, i got a 20% performance boost by launching my pipeline for linux in a container on windows versus the initial windows-native. i assume frameworks may have different optimization sets for different platforms and oss, which is also the case for gpu drivers and low-level stacks. this advice is so easy to try: all needed frameworks are cross-platform and docker provides ready-made images with gpu support. that's it for now. i hope your list of ideas to test is now bigger. i'll be glad to receive feedback and let's keep in touch! 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