File size: 2,264 Bytes
10bf19f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pandas as pd
import fitz  # PyMuPDF
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from tqdm.notebook import tqdm

# Function to read a PDF and convert to text
def pdf_to_text(pdf_path):
    pdf_text = ""
    with fitz.open(pdf_path) as pdf_document:
        for page_num in range(len(pdf_document)):
            page = pdf_document.load_page(page_num)
            pdf_text += page.get_text()
    return pdf_text

# Load dataset

# Extract the last 30 English questions and answers
last_30_QA = QA.tail(80)

# Read PDF and convert to text
pdf_path = 'ahmed.pdf'  # Replace with your actual PDF path
pdf_text = pdf_to_text(pdf_path)

# Setup LangChain with ChatOpenAI
llm = ChatOpenAI(
    model="gpt-4",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    api_key="""""",  # Replace with your actual API key
)

# Define the prompt template manually
prompt_template = PromptTemplate(
    template="You are a helpful assistant. Please rewrite the following into a RAG-able paragraph.\n\n{text}",
    input_variables=["text"]
)

# Initialize list to store RAG-able texts
rag_able_texts = []

# Process each question and answer
for index, row in tqdm(last_30_QA.iterrows(), total=last_30_QA.shape[0]):
    question = row['english']
    answer = row['answer']
    combined_text = f"\n\n{index + 1}. {question} correct answer {answer} +  \t\n\n"
    
    # Define the system and human messages
    system_message = SystemMessage(content="You are a helpful assistant. Please rewrite the following into a RAG-able paragraph.")
    human_message = HumanMessage(content=combined_text)

    # Generate the RAG-able text
    # response = llm([system_message, human_message])
    # print(response)
    # rag_able_text = response.content
    rag_able_text = combined_text
    
    # Append the generated text to the list
    rag_able_texts.append(rag_able_text)

# Combine all the RAG-able texts
final_rag_able_text =  pdf_text +"END__"+"\n\n".join(rag_able_texts)

# Save the final RAG-able text to a file
with open('final_rag_able_text.txt', 'w') as file:
    file.write(final_rag_able_text)

print("RAG-able text created successfully.")