chat with pdf giving "none" answer for each question #14

Closed
opened 2025-11-06 14:31:15 -06:00 by GiteaMirror · 4 comments
Owner

Originally created by @AmanAgwall on GitHub (May 26, 2024).

Originally assigned to: @Shubhamsaboo on GitHub.

import os
import tempfile
import time
import openai
import streamlit as st
from embedchain import App

OpenAI API Key

OPENAI_API_KEY = "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"

Function to retry API calls with rate limit handling

def chat_with_retry(app, prompt, retries=3, delay=5):
for attempt in range(retries):
try:
answer = app.chat(prompt)
if not answer:
print(f"Attempt {attempt + 1}: No response received for prompt: {prompt}")
return answer
except Exception as e:
print(f"Attempt {attempt + 1}: Error: {str(e)}")
if "rate limit" in str(e).lower():
if attempt < retries - 1:
st.warning(f"Rate limit exceeded. Retrying in {delay} seconds...")
time.sleep(delay)
else:
st.error("Max retries reached. Unable to get response from OpenAI API.")
raise

Function to initialize Embedchain bot

def embedchain_bot(db_path, api_key):
print(f"Initializing embedchain bot with db_path: {db_path}")
return App.from_config(
config={
"llm": {"provider": "openai", "config": {"api_key": api_key}},
"vectordb": {"provider": "chroma", "config": {"dir": db_path}},
"embedder": {"provider": "openai", "config": {"api_key": api_key}},
}
)

Function to extract and log PDF content

def extract_pdf_content(pdf_path):
try:
import PyPDF2

    with open(pdf_path, "rb") as file:
        reader = PyPDF2.PdfReader(file)
        text = ""
        for page in reader.pages:
            text += page.extract_text()
    print(f"Extracted PDF Content: {text[:500]}...")  # Log the first 500 characters for verification
    return text
except Exception as e:
    print(f"Error extracting PDF content: {e}")
    return None

Main function to run the application

def main():
st.title("Chat with PDF")

db_path = tempfile.mkdtemp()
app = embedchain_bot(db_path, OPENAI_API_KEY)

pdf_file = st.file_uploader("Upload a PDF file", type="pdf")

if pdf_file:
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f:
        f.write(pdf_file.getvalue())
        print(f"Adding PDF to knowledge base: {f.name}")
        
        # Extract and log PDF content
        pdf_content = extract_pdf_content(f.name)
        if pdf_content:
            print(f"PDF Content:\n{pdf_content}")  # Log the full content for debugging
            app.add(f.name, data_type="pdf_file")
            st.success(f"Added {pdf_file.name} to knowledge base!")
        else:
            st.error("Failed to extract PDF content.")
        
    os.remove(f.name)

prompt = st.text_input("Ask a question about the PDF")

if prompt:
    answer = chat_with_retry(app, prompt)
    if answer:
        st.write(answer)
    else:
        st.warning("No response received. Please check if the content is correctly indexed and the prompt is relevant.")

if name == "main":
main()

Originally created by @AmanAgwall on GitHub (May 26, 2024). Originally assigned to: @Shubhamsaboo on GitHub. import os import tempfile import time import openai import streamlit as st from embedchain import App # OpenAI API Key OPENAI_API_KEY = "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" # Function to retry API calls with rate limit handling def chat_with_retry(app, prompt, retries=3, delay=5): for attempt in range(retries): try: answer = app.chat(prompt) if not answer: print(f"Attempt {attempt + 1}: No response received for prompt: {prompt}") return answer except Exception as e: print(f"Attempt {attempt + 1}: Error: {str(e)}") if "rate limit" in str(e).lower(): if attempt < retries - 1: st.warning(f"Rate limit exceeded. Retrying in {delay} seconds...") time.sleep(delay) else: st.error("Max retries reached. Unable to get response from OpenAI API.") raise # Function to initialize Embedchain bot def embedchain_bot(db_path, api_key): print(f"Initializing embedchain bot with db_path: {db_path}") return App.from_config( config={ "llm": {"provider": "openai", "config": {"api_key": api_key}}, "vectordb": {"provider": "chroma", "config": {"dir": db_path}}, "embedder": {"provider": "openai", "config": {"api_key": api_key}}, } ) # Function to extract and log PDF content def extract_pdf_content(pdf_path): try: import PyPDF2 with open(pdf_path, "rb") as file: reader = PyPDF2.PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() print(f"Extracted PDF Content: {text[:500]}...") # Log the first 500 characters for verification return text except Exception as e: print(f"Error extracting PDF content: {e}") return None # Main function to run the application def main(): st.title("Chat with PDF") db_path = tempfile.mkdtemp() app = embedchain_bot(db_path, OPENAI_API_KEY) pdf_file = st.file_uploader("Upload a PDF file", type="pdf") if pdf_file: with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f: f.write(pdf_file.getvalue()) print(f"Adding PDF to knowledge base: {f.name}") # Extract and log PDF content pdf_content = extract_pdf_content(f.name) if pdf_content: print(f"PDF Content:\n{pdf_content}") # Log the full content for debugging app.add(f.name, data_type="pdf_file") st.success(f"Added {pdf_file.name} to knowledge base!") else: st.error("Failed to extract PDF content.") os.remove(f.name) prompt = st.text_input("Ask a question about the PDF") if prompt: answer = chat_with_retry(app, prompt) if answer: st.write(answer) else: st.warning("No response received. Please check if the content is correctly indexed and the prompt is relevant.") if __name__ == "__main__": main()
Author
Owner

@Shubhamsaboo commented on GitHub (May 26, 2024):

FYI: you would not want to publicly disclose your OpenAI API key.

@Shubhamsaboo commented on GitHub (May 26, 2024): FYI: you would not want to publicly disclose your OpenAI API key.
Author
Owner

@AmanAgwall commented on GitHub (May 26, 2024):

yeah, I made a mistake, OpenAi automatically disabled it. will keep in mind.

@AmanAgwall commented on GitHub (May 26, 2024): yeah, I made a mistake, OpenAi automatically disabled it. will keep in mind.
Author
Owner

@Shubhamsaboo commented on GitHub (May 26, 2024):

You code mainly has some indentation and syntax errors. Here's the revised version that works for me:

import os
import tempfile
import time
import streamlit as st
from embedchain import App
import PyPDF2

# Function to retry API calls with rate limit handling
def chat_with_retry(app, prompt, retries=3, delay=5):
    for attempt in range(retries):
        try:
            answer = app.chat(prompt)
            if not answer:
                print(f"Attempt {attempt + 1}: No response received for prompt: {prompt}")
            return answer
        except Exception as e:
            print(f"Attempt {attempt + 1}: Error: {str(e)}")
            if "rate limit" in str(e).lower():
                if attempt < retries - 1:
                    st.warning(f"Rate limit exceeded. Retrying in {delay} seconds...")
                    time.sleep(delay)
                else:
                    st.error("Max retries reached. Unable to get response from OpenAI API.")
                    raise

# Function to initialize Embedchain bot
def embedchain_bot(db_path, api_key):
    print(f"Initializing embedchain bot with db_path: {db_path}")
    return App.from_config(
        config={
            "llm": {"provider": "openai", "config": {"api_key": api_key}},
            "vectordb": {"provider": "chroma", "config": {"dir": db_path}},
            "embedder": {"provider": "openai", "config": {"api_key": api_key}},
        }
    )

# Function to extract and log PDF content
def extract_pdf_content(pdf_path):
    try:
        with open(pdf_path, "rb") as file:
            reader = PyPDF2.PdfReader(file)
            text = ""
            for page in reader.pages:
                text += page.extract_text()
        print(f"Extracted PDF Content: {text[:500]}...")  # Log the first 500 characters for verification
        return text
    except Exception as e:
        print(f"Error extracting PDF content: {e}")
        return None

# Main function to run the application
def main():
    st.title("Chat with PDF")

    openai_access_token = st.text_input("OpenAI API Key", type="password")

    if openai_access_token:
        db_path = tempfile.mkdtemp()
        app = embedchain_bot(db_path, openai_access_token)

        pdf_file = st.file_uploader("Upload a PDF file", type="pdf")

        if pdf_file:
            with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f:
                f.write(pdf_file.getvalue())
                print(f"Adding PDF to knowledge base: {f.name}")
                
                # Extract and log PDF content
                pdf_content = extract_pdf_content(f.name)
                if pdf_content:
                    print(f"PDF Content:\n{pdf_content}")  # Log the full content for debugging
                    app.add(f.name, data_type="pdf_file")
                    st.success(f"Added {pdf_file.name} to knowledge base!")
                else:
                    st.error("Failed to extract PDF content.")
                
            os.remove(f.name)

        prompt = st.text_input("Ask a question about the PDF")

        if prompt:
            answer = chat_with_retry(app, prompt)
            if answer:
                st.write(answer)
            else:
                st.warning("No response received. Please check if the content is correctly indexed and the prompt is relevant.")

if __name__ == "__main__":
    main()

I get the following results after running the above code 👇

Screenshot 2024-05-26 at 2 17 23 PM
@Shubhamsaboo commented on GitHub (May 26, 2024): You code mainly has some indentation and syntax errors. Here's the revised version that works for me: ```python import os import tempfile import time import streamlit as st from embedchain import App import PyPDF2 # Function to retry API calls with rate limit handling def chat_with_retry(app, prompt, retries=3, delay=5): for attempt in range(retries): try: answer = app.chat(prompt) if not answer: print(f"Attempt {attempt + 1}: No response received for prompt: {prompt}") return answer except Exception as e: print(f"Attempt {attempt + 1}: Error: {str(e)}") if "rate limit" in str(e).lower(): if attempt < retries - 1: st.warning(f"Rate limit exceeded. Retrying in {delay} seconds...") time.sleep(delay) else: st.error("Max retries reached. Unable to get response from OpenAI API.") raise # Function to initialize Embedchain bot def embedchain_bot(db_path, api_key): print(f"Initializing embedchain bot with db_path: {db_path}") return App.from_config( config={ "llm": {"provider": "openai", "config": {"api_key": api_key}}, "vectordb": {"provider": "chroma", "config": {"dir": db_path}}, "embedder": {"provider": "openai", "config": {"api_key": api_key}}, } ) # Function to extract and log PDF content def extract_pdf_content(pdf_path): try: with open(pdf_path, "rb") as file: reader = PyPDF2.PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() print(f"Extracted PDF Content: {text[:500]}...") # Log the first 500 characters for verification return text except Exception as e: print(f"Error extracting PDF content: {e}") return None # Main function to run the application def main(): st.title("Chat with PDF") openai_access_token = st.text_input("OpenAI API Key", type="password") if openai_access_token: db_path = tempfile.mkdtemp() app = embedchain_bot(db_path, openai_access_token) pdf_file = st.file_uploader("Upload a PDF file", type="pdf") if pdf_file: with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f: f.write(pdf_file.getvalue()) print(f"Adding PDF to knowledge base: {f.name}") # Extract and log PDF content pdf_content = extract_pdf_content(f.name) if pdf_content: print(f"PDF Content:\n{pdf_content}") # Log the full content for debugging app.add(f.name, data_type="pdf_file") st.success(f"Added {pdf_file.name} to knowledge base!") else: st.error("Failed to extract PDF content.") os.remove(f.name) prompt = st.text_input("Ask a question about the PDF") if prompt: answer = chat_with_retry(app, prompt) if answer: st.write(answer) else: st.warning("No response received. Please check if the content is correctly indexed and the prompt is relevant.") if __name__ == "__main__": main() ``` I get the following results after running the above code 👇 <img width="838" alt="Screenshot 2024-05-26 at 2 17 23 PM" src="https://github.com/Shubhamsaboo/awesome-llm-apps/assets/31396011/141e96b3-207a-4e93-a6d2-644078edfe56">
Author
Owner

@AmanAgwall commented on GitHub (May 26, 2024):

Thanks you so much for the response,
What is a vector database_ _ Cloudflare.pdf
is there any way you can send me the Pdf that you are using for the demonstration? I am using this above pdf and I am asking "what is a vector database?

@AmanAgwall commented on GitHub (May 26, 2024): Thanks you so much for the response, [What is a vector database_ _ Cloudflare.pdf](https://github.com/Shubhamsaboo/awesome-llm-apps/files/15448782/What.is.a.vector.database_._.Cloudflare.pdf) is there any way you can send me the Pdf that you are using for the demonstration? I am using this above pdf and I am asking "what is a vector database?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/awesome-llm-apps#14