newChatbot / pdf_rag.py
npcJensen's picture
Upload 15 files
e0225ee verified
from openai import OpenAI
from dotenv import load_dotenv
import os
import requests
import PyPDF2
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
model = "gemini-2.5-flash-lite"
base_url = "https://generativelanguage.googleapis.com/v1beta/openai/"
client = OpenAI(base_url=base_url, api_key=api_key)
#url = "https://github.com/hereandnowai/sathyabama-be-cse-aiml-pt1-07-2025-hands-on-professional-training-on-genai-and-ai-agents/blob/73aae8f435d303c8ed08250dc5f719e178879543/general-profile-of-hereandnowai.pdf"
url = "https://raw.githubusercontent.com/hereandnowai/sathyabama-be-cse-aiml-pt1-07-2025-hands-on-professional-training-on-genai-and-ai-agents/main/general-profile-of-hereandnowai.pdf"
response = requests.get(url)
PDF_File_name = "profile-of-hereandnowai.pdf"
PDF_dir =os.path.dirname(__file__)
PDF_File_path = os.path.join(PDF_dir, PDF_File_name)
with open(PDF_File_path, "wb") as f:
f.write(response.content)
try:
with open(PDF_File_path, "rb") as f:
reader = PyPDF2.PdfReader(f)
pdf_text_chunks = []
for page in reader.pages:
page_text = page.extract_text()
if page_text:
pdf_text_chunks.append(page_text.strip())
pdf_context = "\n".join(pdf_text_chunks) if pdf_text_chunks else "no text found in PDF"
except Exception as e:
print(f"Error reading the PDF file: {e}")
pdf_context = "Error extracting text from PDF file."
def get_response(message, history):
system_prompt = f"""context from {PDF_File_path}:\n{pdf_context}
\n\n Qustion:{message}
\n\n Answer based only on the context:"""
messages = [{"role": "system", "content": system_prompt}]
messages.extend(history)
messages.append({"role": "user", "content": message})
response = client.chat.completions.create(model=model, messages=messages)
ai_response = response.choices[0].message.content
return ai_response
print(get_response("who is the ceo of here and now ai?", []))