<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jude Gigy</title>
    <description>The latest articles on DEV Community by Jude Gigy (@judeg1234).</description>
    <link>https://dev.to/judeg1234</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1821305%2F6a4c6f11-75e4-4d91-80b8-f48947a09b7f.png</url>
      <title>DEV Community: Jude Gigy</title>
      <link>https://dev.to/judeg1234</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/judeg1234"/>
    <language>en</language>
    <item>
      <title>Memory error when running python RAG LLM</title>
      <dc:creator>Jude Gigy</dc:creator>
      <pubDate>Mon, 22 Jul 2024 17:12:11 +0000</pubDate>
      <link>https://dev.to/judeg1234/memory-error-when-running-python-rag-llm-2ee8</link>
      <guid>https://dev.to/judeg1234/memory-error-when-running-python-rag-llm-2ee8</guid>
      <description>&lt;p&gt;Everytime I run this LLM I get a memory error. Please help.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhjo8szpvhd2p3uqelmy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhjo8szpvhd2p3uqelmy.png" alt="Image description" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;importimport os&lt;br&gt;
 os&lt;/p&gt;

&lt;p&gt;from transformers import RagTokenizer, RagRetriever, RagTokenForGeneration&lt;/p&gt;

&lt;p&gt;from transformers import pipeline&lt;/p&gt;

&lt;h1&gt;
  
  
  Replace with the path to your local folder containing the text files
&lt;/h1&gt;

&lt;p&gt;folder_path = "C:\Users\asokw\Downloads\new"&lt;/p&gt;

&lt;h1&gt;
  
  
  Function to read and process text files
&lt;/h1&gt;

&lt;p&gt;def read_text_files(folder_path):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;all_files = os.listdir(folder_path)


text_files = [os.path.join(folder_path, f) for f in all_files if f.endswith('.txt')]


documents = []





for file_path in text_files:


    with open(file_path, 'r', encoding='utf-8') as file:


        content = file.read()


        documents.append(content)





return documents
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Load and preprocess documents
&lt;/h1&gt;

&lt;p&gt;documents = read_text_files(folder_path)&lt;/p&gt;
&lt;h1&gt;
  
  
  Initialize RAG tokenizer, retriever, and model
&lt;/h1&gt;

&lt;p&gt;tokenizer = RagTokenizer.from_pretrained('facebook/rag-token-base')&lt;/p&gt;

&lt;p&gt;retriever = RagRetriever.from_pretrained('facebook/rag-token-base', index_name='exact',  passages=documents)&lt;/p&gt;

&lt;p&gt;model = RagTokenForGeneration.from_pretrained('facebook/rag-token-base', retriever=retriever)&lt;/p&gt;

&lt;p&gt;your_prompt = "What information can be found in these documents?"&lt;/p&gt;

&lt;p&gt;inputs = tokenizer(your_prompt, return_tensors="pt")&lt;/p&gt;

&lt;p&gt;retrieval_output = model.get_retrieval_vector(inputs)&lt;/p&gt;

&lt;p&gt;generation_inputs = {&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"input_ids": inputs.input_ids,


"attention_mask": inputs.attention_mask,


"retrieval_logits": retrieval_output,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;generation_output = model.generate(**generation_inputs)&lt;/p&gt;

&lt;p&gt;generated_text = tokenizer.decode(generation_output.sequences[0])&lt;/p&gt;

&lt;p&gt;print(f"Retrieved documents:", retrieval_output)&lt;/p&gt;

&lt;p&gt;print(f"Generated text:", generated_text)&lt;/p&gt;

&lt;p&gt;from transformers import RagTokenizer, RagRetriever, RagTokenForGeneration&lt;/p&gt;

&lt;p&gt;from transformers import pipeline&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
