mirror of
https://github.com/Shubhamsaboo/awesome-llm-apps.git
synced 2026-07-16 23:44:06 -05:00
chat with pdf giving "none" answer for each question #14
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
Main function to run the application
def main():
st.title("Chat with PDF")
if name == "main":
main()
@Shubhamsaboo commented on GitHub (May 26, 2024):
FYI: you would not want to publicly disclose your OpenAI API key.
@AmanAgwall commented on GitHub (May 26, 2024):
yeah, I made a mistake, OpenAi automatically disabled it. will keep in mind.
@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:
I get the following results after running the above code 👇
@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?