How to use RecursiveTextSplitter for Chat Models like OpenAI and LLama?
Labels: name: auto:bug, name: auto:question, name: area: models

### Issue with current documentation:

I developed a piece of code that will read data from a pdf file, send it to chat models, and then return the result. What to do if the input length is too long when using chat models? I attempted embeddings, but the solutions are simple and not as good as Chat models, in my opinion. For chat models, I tried using RecursiveTextSplitter, but it didn't work. The code is below.


```
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from dotenv import load_dotenv
from pytesseract import image_to_string
from langchain.text_splitter import RecursiveCharacterTextSplitter
from PIL import Image
from io import BytesIO
import pypdfium2 as pdfium
import pandas as pd
import pytesseract
import json
import os
import requests

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

load_dotenv()
os.environ["OPENAI_API_KEY"] = "sk-H................."


class DocumentProcessor:
    def __init__(self):
        self.llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")

    def convert_pdf_to_images(self, file_path, scale=300 / 72):
        pdf_file = pdfium.PdfDocument(file_path)
        page_indices = [i for i in range(len(pdf_file))]
        renderer = pdf_file.render(
            pdfium.PdfBitmap.to_pil,
            page_indices=page_indices,
            scale=scale,
        )
        final_images = []
        for i, image in zip(page_indices, renderer):
            image_byte_array = BytesIO()
            image.save(image_byte_array, format='jpeg', optimize=True)
            image_byte_array = image_byte_array.getvalue()
            final_images.append(dict({i: image_byte_array}))
        return final_images

    def extract_text_from_img(self, list_dict_final_images):
        image_list = [list(data.values())[0] for data in list_dict_final_images]
        image_content = []
        for index, image_bytes in enumerate(image_list):
            image = Image.open(BytesIO(image_bytes))
            raw_text = str(image_to_string(image))
            image_content.append(raw_text)
        return "\\n".join(image_content)

    def extract_content_from_url(self, url):
        images_list = self.convert_pdf_to_images(url)
        text_with_pytesseract = self.extract_text_from_img(images_list)
        return text_with_pytesseract

    def extract_structured_data(self, content):
        template = """
        You are an expert admin people who will extract core information from documents

        {content}

        Above is the content; please try to return the abstract and extractive summary

        """
        prompt = PromptTemplate(
            input_variables=["content"],
            template=template,
        )
        chain = LLMChain(llm=self.llm, prompt=prompt)
        # results = chain.run(content=content, data_points=self.data_points)
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
        chunks = text_splitter.split_documents(content)
        results = [chain.run(content=chunk) for chunk in chunks]
        print(results)
        return results

    def process_documents(self, file_paths):
        results = []
        for file_path in file_paths:
            content = self.extract_content_from_url(file_path)
            data = self.extract_structured_data(content)
            if isinstance(data, list):
                results.extend(data)
            else:
                results.append(data)
        return results


# Example usage
if __name__ == '__main__':
    document_processor = DocumentProcessor()
    uploaded_files_paths = [r'Questionnaire_2021 (1).pdf']
    processed_results = document_processor.process_documents(uploaded_files_paths)
    if len(processed_results) > 0:
        try:
            df = pd.DataFrame(processed_results)
            print("Results:")
            df.to_excel('output.xlsx', index=False)
            print(df)
        except Exception as e:
            print(f"An error occurred while creating the DataFrame: {e}")
```

Below is the error for above code

```
Traceback (most recent call last):
  File "Documents\langchain_projects\Invoice Extraction using LLM\testing.py", line 117, in <module>
    processed_results = document_processor.process_documents(uploaded_files_paths)
  File "Documents\langchain_projects\Invoice Extraction using LLM\testing.py", line 104, in process_documents
    data = self.extract_structured_data(content)
  File "Documents\langchain_projects\Invoice Extraction using LLM\testing.py", line 95, in extract_structured_data
    chunks = text_splitter.split_documents(content)
  File "anaconda3\lib\site-packages\langchain\text_splitter.py", line 112, in split_documents
    texts.append(doc.page_content)
AttributeError: 'str' object has no attribute 'page_content'
```

Below is the error if i'm not using RecursiveTextSplitter

`openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 36248 tokens. Please reduce the length of the messages.`

Could someone possibly explain to me how to apply RecursiveTextSplitter to chat models rather than embeddings?

### Idea or request for content:

_No response_