Added new demo

This commit is contained in:
ShubhamSaboo 2024-09-15 20:53:01 -05:00
parent 3635c93854
commit 77dcf86fb1
3 changed files with 149 additions and 0 deletions

43
autonomous_rag/README.md Normal file
View file

@ -0,0 +1,43 @@
### 🤖 AutoRAG: Autonomous RAG with GPT-4o and Vector Database
This Streamlit application implements an Autonomous Retrieval-Augmented Generation (RAG) system using OpenAI's GPT-4o model and PgVector database. It allows users to upload PDF documents, add them to a knowledge base, and query the AI assistant with context from both the knowledge base and web searches.
Features
### Freatures
- Chat interface for interacting with the AI assistant
- PDF document upload and processing
- Knowledge base integration using PostgreSQL and Pgvector
- Web search capability using DuckDuckGo
- Persistent storage of assistant data and conversations
### How to get Started?
1. Clone the GitHub repository
```bash
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
```
2. Install the required dependencies:
```bash
pip install -r requirements.txt
```
3. Ensure PgVector Database is running:
The app expects PgVector to be running on [localhost:6333](http://localhost:5532/). Adjust the configuration in the code if your setup is different.
```bash
docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v pgvolume:/var/lib/postgresql/data \
-p 5532:5432 \
--name pgvector \
phidata/pgvector:16
```
4. Run the Streamlit App
```bash
streamlit run autorag.py
```

97
autonomous_rag/autorag.py Normal file
View file

@ -0,0 +1,97 @@
import streamlit as st
import nest_asyncio
from io import BytesIO
from phi.assistant import Assistant
from phi.document.reader.pdf import PDFReader
from phi.llm.openai import OpenAIChat
from phi.knowledge import AssistantKnowledge
from phi.tools.duckduckgo import DuckDuckGo
from phi.embedder.openai import OpenAIEmbedder
from phi.vectordb.pgvector import PgVector2
from phi.storage.assistant.postgres import PgAssistantStorage
# Apply nest_asyncio to allow nested event loops, required for running async functions in Streamlit
nest_asyncio.apply()
# Database connection string for PostgreSQL
DB_URL = "postgresql+psycopg://ai:ai@localhost:5532/ai"
# Function to set up the Assistant, utilizing caching for resource efficiency
@st.cache_resource
def setup_assistant(api_key: str) -> Assistant:
llm = OpenAIChat(model="gpt-4o-mini", api_key=api_key)
# Set up the Assistant with storage, knowledge base, and tools
return Assistant(
name="auto_rag_assistant", # Name of the Assistant
llm=llm, # Language model to be used
storage=PgAssistantStorage(table_name="auto_rag_storage", db_url=DB_URL),
knowledge_base=AssistantKnowledge(
vector_db=PgVector2(
db_url=DB_URL,
collection="auto_rag_docs",
embedder=OpenAIEmbedder(model="text-embedding-ada-002", dimensions=1536, api_key=api_key),
),
num_documents=3,
),
tools=[DuckDuckGo()], # Additional tool for web search via DuckDuckGo
instructions=[
"Search your knowledge base first.",
"If not found, search the internet.",
"Provide clear and concise answers.",
],
show_tool_calls=True,
search_knowledge=True,
read_chat_history=True,
markdown=True,
debug_mode=True,
)
# Function to add a PDF document to the knowledge base
def add_document(assistant: Assistant, file: BytesIO):
reader = PDFReader()
docs = reader.read(file)
if docs:
assistant.knowledge_base.load_documents(docs, upsert=True)
st.success("Document added to the knowledge base.")
else:
st.error("Failed to read the document.")
# Function to query the Assistant and return a response
def query_assistant(assistant: Assistant, question: str) -> str:
return "".join([delta for delta in assistant.run(question)])
# Main function to handle Streamlit app layout and interactions
def main():
st.set_page_config(page_title="AutoRAG", layout="wide")
st.title("🤖 Auto-RAG: Autonomous RAG with GPT-4o")
api_key = st.sidebar.text_input("Enter your OpenAI API Key 🔑", type="password")
if not api_key:
st.sidebar.warning("Enter your OpenAI API Key to proceed.")
st.stop()
assistant = setup_assistant(api_key)
uploaded_file = st.sidebar.file_uploader("📄 Upload PDF", type=["pdf"])
if uploaded_file and st.sidebar.button("🛠️ Add to Knowledge Base"):
add_document(assistant, BytesIO(uploaded_file.read()))
question = st.text_input("💬 Ask Your Question:")
# When the user submits a question, query the assistant for an answer
if st.button("🔍 Get Answer"):
# Ensure the question is not empty
if question.strip():
with st.spinner("🤔 Thinking..."):
# Query the assistant and display the response
answer = query_assistant(assistant, question)
st.write("📝 **Response:**", answer)
else:
# Show an error if the question input is empty
st.error("Please enter a question.")
# Entry point of the application
if __name__ == "__main__":
main()

View file

@ -0,0 +1,9 @@
streamlit
phidata
openai
psycopg-binary
pgvector
requests
sqlalchemy
pypdf
duckduckgo-search