updated rag_reasoning_agent

This commit is contained in:
ShubhamSaboo 2025-06-06 17:50:54 -05:00
parent 641fbea532
commit 2db04989ad
2 changed files with 34 additions and 36 deletions

View file

@ -1,5 +1,5 @@
# 🧐 Agentic RAG with Reasoning # 🧐 Agentic RAG with Reasoning
A sophisticated RAG system that demonstrates an AI agent's step-by-step reasoning process using Agno, Claude and Cohere. This implementation allows users to upload documents, add web sources, ask questions, and observe the agent's thought process in real-time. A sophisticated RAG system that demonstrates an AI agent's step-by-step reasoning process using Agno, Claude and OpenAI. This implementation allows users to upload documents, add web sources, ask questions, and observe the agent's thought process in real-time.
## Features ## Features
@ -17,15 +17,14 @@ A sophisticated RAG system that demonstrates an AI agent's step-by-step reasonin
3. Advanced RAG Capabilities 3. Advanced RAG Capabilities
- Hybrid search combining keyword and semantic matching - Vector search using OpenAI embeddings for semantic matching
- Cohere reranking for improved relevance
- Source attribution with citations - Source attribution with citations
## Agent Configuration ## Agent Configuration
- Claude 3.5 Sonnet for language processing - Claude 3.5 Sonnet for language processing
- Cohere embedding and reranking models - OpenAI embedding model for vector search
- ReasoningTools for step-by-step analysis - ReasoningTools for step-by-step analysis
- Customizable agent instructions - Customizable agent instructions
@ -39,9 +38,9 @@ You'll need the following API keys:
- Navigate to API Keys section - Navigate to API Keys section
- Create a new API key - Create a new API key
2. Cohere API Key 2. OpenAI API Key
- Sign up at dashboard.cohere.ai - Sign up at platform.openai.com
- Navigate to API Keys section - Navigate to API Keys section
- Generate a new API key - Generate a new API key
@ -50,7 +49,7 @@ You'll need the following API keys:
1. **Clone the Repository**: 1. **Clone the Repository**:
```bash ```bash
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
cd rag_tutorials/agentic_rag cd rag_tutorials/agentic_rag_with_reasoning
``` ```
2. **Install the dependencies**: 2. **Install the dependencies**:
@ -66,7 +65,7 @@ You'll need the following API keys:
4. **Configure API Keys:** 4. **Configure API Keys:**
- Enter your Anthropic API key in the first field - Enter your Anthropic API key in the first field
- Enter your Cohere API key in the second field - Enter your OpenAI API key in the second field
- Both keys are required for the app to function - Both keys are required for the app to function
@ -83,15 +82,15 @@ The application uses a sophisticated RAG pipeline:
### Knowledge Base Setup ### Knowledge Base Setup
- Documents are loaded from URLs using WebBaseLoader - Documents are loaded from URLs using WebBaseLoader
- Text is chunked and embedded using Cohere's embedding model - Text is chunked and embedded using OpenAI's embedding model
- Vectors are stored in LanceDB for efficient retrieval - Vectors are stored in LanceDB for efficient retrieval
- Hybrid search enables both keyword and semantic matching - Vector search enables semantic matching for relevant information
### Agent Processing ### Agent Processing
- User queries trigger the agent's reasoning process - User queries trigger the agent's reasoning process
- ReasoningTools help the agent think step-by-step - ReasoningTools help the agent think step-by-step
- The agent searches the knowledge base for relevant information - The agent searches the knowledge base for relevant information
- Claude 3.5 Sonnet generates comprehensive answers with citations - Claude 4 Sonnet generates comprehensive answers with citations
### UI Flow ### UI Flow
- Enter API keys → Add knowledge sources → Ask questions - Enter API keys → Add knowledge sources → Ask questions

View file

@ -1,21 +1,25 @@
import streamlit as st import streamlit as st
from agno.agent import Agent, RunEvent from agno.agent import Agent, RunEvent
from agno.embedder.cohere import CohereEmbedder from agno.embedder.openai import OpenAIEmbedder
from agno.knowledge.url import UrlKnowledge from agno.knowledge.url import UrlKnowledge
from agno.models.anthropic import Claude from agno.models.anthropic import Claude
from agno.reranker.cohere import CohereReranker
from agno.tools.reasoning import ReasoningTools from agno.tools.reasoning import ReasoningTools
from agno.vectordb.lancedb import LanceDb, SearchType from agno.vectordb.lancedb import LanceDb, SearchType
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Page configuration # Page configuration
st.set_page_config( st.set_page_config(
page_title="Agentic RAG with Reasoning", page_title="Agentic RAG with Reasoning",
page_icon="🧠", page_icon="🧐",
layout="wide" layout="wide"
) )
# Main title and description # Main title and description
st.title("🧠 Agentic RAG with Reasoning") st.title("🧐 Agentic RAG with Reasoning")
st.markdown(""" st.markdown("""
This app demonstrates an AI agent that: This app demonstrates an AI agent that:
1. **Retrieves** relevant information from knowledge sources 1. **Retrieves** relevant information from knowledge sources
@ -32,17 +36,19 @@ with col1:
anthropic_key = st.text_input( anthropic_key = st.text_input(
"Anthropic API Key", "Anthropic API Key",
type="password", type="password",
value=os.getenv("ANTHROPIC_API_KEY", ""),
help="Get your key from https://console.anthropic.com/" help="Get your key from https://console.anthropic.com/"
) )
with col2: with col2:
cohere_key = st.text_input( openai_key = st.text_input(
"Cohere API Key", "OpenAI API Key",
type="password", type="password",
help="Get your key from https://dashboard.cohere.ai/" value=os.getenv("OPENAI_API_KEY", ""),
help="Get your key from https://platform.openai.com/"
) )
# Check if both API keys are provided # Check if API keys are provided
if anthropic_key and cohere_key: if anthropic_key and openai_key:
# Initialize knowledge base (cached to avoid reloading) # Initialize knowledge base (cached to avoid reloading)
@st.cache_resource(show_spinner="📚 Loading knowledge base...") @st.cache_resource(show_spinner="📚 Loading knowledge base...")
@ -53,18 +59,13 @@ if anthropic_key and cohere_key:
vector_db=LanceDb( vector_db=LanceDb(
uri="tmp/lancedb", uri="tmp/lancedb",
table_name="agno_docs", table_name="agno_docs",
search_type=SearchType.hybrid, # Uses both keyword and semantic search search_type=SearchType.vector, # Use vector search
embedder=CohereEmbedder( embedder=OpenAIEmbedder(
id="embed-v4.0", api_key=openai_key
api_key=cohere_key
),
reranker=CohereReranker(
model="rerank-v3.5",
api_key=cohere_key
), ),
), ),
) )
kb.load(recreate=False) # Load documents into vector DB kb.load(recreate=True) # Load documents into vector DB
return kb return kb
# Initialize agent (cached to avoid reloading) # Initialize agent (cached to avoid reloading)
@ -202,8 +203,8 @@ else:
1. **Anthropic API Key** - For Claude AI model 1. **Anthropic API Key** - For Claude AI model
- Sign up at [console.anthropic.com](https://console.anthropic.com/) - Sign up at [console.anthropic.com](https://console.anthropic.com/)
2. **Cohere API Key** - For embeddings and reranking 2. **OpenAI API Key** - For embeddings
- Sign up at [dashboard.cohere.ai](https://dashboard.cohere.ai/) - Sign up at [platform.openai.com](https://platform.openai.com/)
Once you have both keys, enter them above to start! Once you have both keys, enter them above to start!
""") """)
@ -215,16 +216,14 @@ with st.expander("📖 How This Works"):
**This app uses the Agno framework to create an intelligent Q&A system:** **This app uses the Agno framework to create an intelligent Q&A system:**
1. **Knowledge Loading**: URLs are processed and stored in a vector database (LanceDB) 1. **Knowledge Loading**: URLs are processed and stored in a vector database (LanceDB)
2. **Hybrid Search**: Combines keyword and semantic search to find relevant information 2. **Vector Search**: Uses OpenAI's embeddings for semantic search to find relevant information
3. **Reasoning Tools**: The agent uses special tools to think through problems step-by-step 3. **Reasoning Tools**: The agent uses special tools to think through problems step-by-step
4. **Claude AI**: Anthropic's Claude model processes the information and generates answers 4. **Claude AI**: Anthropic's Claude model processes the information and generates answers
5. **Reranking**: Cohere's reranker ensures the most relevant information is used
**Key Components:** **Key Components:**
- `UrlKnowledge`: Manages document loading from URLs - `UrlKnowledge`: Manages document loading from URLs
- `LanceDb`: Vector database for efficient similarity search - `LanceDb`: Vector database for efficient similarity search
- `CohereEmbedder`: Converts text to embeddings for semantic search - `OpenAIEmbedder`: Converts text to embeddings using OpenAI's embedding model
- `CohereReranker`: Improves search result relevance
- `ReasoningTools`: Enables step-by-step reasoning - `ReasoningTools`: Enables step-by-step reasoning
- `Agent`: Orchestrates everything to answer questions - `Agent`: Orchestrates everything to answer questions
""") """)