Setting Up the Environment
Before diving into the script, ensure you have the following:
- Groq API Key: Obtain a valid API key from Groq.
-
Python Environment: Set up a Python environment with the necessary libraries. You’ll need
requestsfor API calls andPyPDF2for handling PDF files. -
Install Libraries: Run
pip install requests PyPDF2to install the required libraries.
Creating the Python Script
The script will involve the following steps:
- Read PDF Content: Extract text from the PDF file.
- Send Question to API: Use the Groq API to send the question and the extracted text to the LLaMA-3 model.
- Get Answer: Receive the answer from the API and print it.
Step 1: Read PDF Content
First, we’ll write a function to extract text from a PDF file using PyPDF2.
import PyPDF2
def extract_text_from_pdf(file_path):
pdf_file_obj = open(file_path, 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)
num_pages = pdf_reader.numPages
text = ''
for page in range(num_pages):
page_obj = pdf_reader.getPage(page)
text += page_obj.extractText()
pdf_file_obj.close()
return text
Step 2: Send Question to API
Next, we’ll create a function to send the question and the PDF content to the Groq API.
import requests
import json
def send_question_to_api(question, pdf_content, groq_api_key):
url = 'https://api.groq.com/openai/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {groq_api_key}'
}
data = {
'model': 'llama-3.3-70b-versatile',
'messages': [
{
'role': 'user',
'content': f'Answer the following question based on the provided text: {question}\n\nText: {pdf_content}'
}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response
Step 3: Get Answer
Finally, we’ll parse the API response to get the answer.
def
Post Views: 37

