Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAG is not Working in a team of Agents #1668

Closed
MuhammadAhsaanAbbasi opened this issue Dec 31, 2024 · 4 comments
Closed

RAG is not Working in a team of Agents #1668

MuhammadAhsaanAbbasi opened this issue Dec 31, 2024 · 4 comments

Comments

@MuhammadAhsaanAbbasi
Copy link

Hello guys, I used team of agents & want to used Embedding RAG in team of Agents how can I implement it.
This is my code let's check the code & also some other problem then provide me a solution of this:

app/agent_team.py

------------------------------------------------------------------------------

type: ignore

import os
import typer
from rich.prompt import Prompt
from typing import Optional
from dotenv import load_dotenv

--- phi imports ---

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.storage.agent.sqlite import SqlAgentStorage
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools
from phi.embedder.openai import OpenAIEmbedder
from phi.document.chunking.agentic import AgenticChunking
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.chroma import ChromaDb
load_dotenv()

vector_db = ChromaDb(
collection="recipes",
embedder=OpenAIEmbedder(model="text-embedding-3-small"),
)

knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=vector_db,
chunking_strategy=AgenticChunking(max_chunk_size=1000),
num_documents=5,
)

Load the knowledge base (comment out after first run)

knowledge_base.load(recreate=False)

YFinance Agent

finance_agent = Agent(
name="Finance Agent",
role="Get financial data",
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
show_tool_calls=True,
)

DuckDuckGo Agent

web_agent = Agent(
name="Web Agent",
role="Search the web for information",
tools=[DuckDuckGo()],
show_tool_calls=True,
)

RAG Agent

ragAgent = Agent(
knowledge_base=knowledge_base,
search_knowledge=True,
)

agent_team = Agent(
name="Multi-Capability Team",
team=[finance_agent, web_agent, ragAgent],
instructions=[
"Use the Finance Agent for financial data queries.",
"Use the Web Agent for general web searches.",
"Use the RAG Agent to retrieve information from the knowledge base.",
],
show_tool_calls=True,
markdown=True,
debug_mode=True
)

agent_team.print_response("What's the current stock price of AAPL and can you find a recipe for Thai green curry?", stream=True)

When I don't commit the line of command # Load the knowledge base (comment out after first run)
knowledge_base.load(recreate=False) so it's doesn't execute the below code & when I committed so it's agent doesn't retrieve information from Knowledgebase & give me answer using web search tool
I attached two documents first when I don't commit a line of knowledge_base.load(recreate=False) & second when I commit the line.
Screenshot (378)
Screenshot (377)

@cpunion @chug2k @ofri @sroecker @adieyal & all team members of Phidata please look at down my issue & help me out to resolve this issue.

@Cipher-unhsiV
Copy link

Interesting! I used LanceDB and not ChromaDB. But still got stuck lol

@manthanguptaa
Copy link
Contributor

Hey @MuhammadAhsaanAbbasi @Cipher-unhsiV this worked for me perfectly. I had to play around with instructions and also added the role of the rag agent

import os
import typer
from rich.prompt import Prompt
from typing import Optional
from dotenv import load_dotenv

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.storage.agent.sqlite import SqlAgentStorage
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools
from phi.embedder.openai import OpenAIEmbedder
from phi.document.chunking.agentic import AgenticChunking
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.chroma import ChromaDb
load_dotenv()

vector_db = ChromaDb(
    collection="recipes",
    embedder=OpenAIEmbedder(model="text-embedding-3-small"),
)

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=vector_db,
    chunking_strategy=AgenticChunking(max_chunk_size=1000),
    num_documents=5,
)

knowledge_base.load(recreate=False)

finance_agent = Agent(
    name="Finance Agent",
    role="Get financial data",
    tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
    show_tool_calls=True,
)

web_agent = Agent(
    name="Web Agent",
    role="Search the web for information",
    tools=[DuckDuckGo()],
    show_tool_calls=True,
)

ragAgent = Agent(
    name="RAG Agent",
    role="Search the knowledge base for recipes",
    knowledge=knowledge_base,
    search_knowledge=True,
)

agent_team = Agent(
    name="Multi-Capability Team",
    team=[ragAgent, finance_agent, web_agent],
    instructions=[
        "Use the RAG Agent to retrieve recipes from the knowledge base.",
        "If you don't find the information in the knowledge base, use the Finance Agent for financial data queries.",
        "If you don't find the information in the knowledge base, use the Web Agent for general web searches.",
    ],
    show_tool_calls=True,
    markdown=True,
    debug_mode=True
)

agent_team.print_response("What's the current stock price of AAPL and can you find a recipe for Thai green curry?", stream=True)


@MuhammadAhsaanAbbasi
Copy link
Author

Hey @MuhammadAhsaanAbbasi @Cipher-unhsiV this worked for me perfectly. I had to play around with instructions and also added the role of the rag agent

import os
import typer
from rich.prompt import Prompt
from typing import Optional
from dotenv import load_dotenv

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.storage.agent.sqlite import SqlAgentStorage
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools
from phi.embedder.openai import OpenAIEmbedder
from phi.document.chunking.agentic import AgenticChunking
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.chroma import ChromaDb
load_dotenv()

vector_db = ChromaDb(
    collection="recipes",
    embedder=OpenAIEmbedder(model="text-embedding-3-small"),
)

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=vector_db,
    chunking_strategy=AgenticChunking(max_chunk_size=1000),
    num_documents=5,
)

knowledge_base.load(recreate=False)

finance_agent = Agent(
    name="Finance Agent",
    role="Get financial data",
    tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
    show_tool_calls=True,
)

web_agent = Agent(
    name="Web Agent",
    role="Search the web for information",
    tools=[DuckDuckGo()],
    show_tool_calls=True,
)

ragAgent = Agent(
    name="RAG Agent",
    role="Search the knowledge base for recipes",
    knowledge=knowledge_base,
    search_knowledge=True,
)

agent_team = Agent(
    name="Multi-Capability Team",
    team=[ragAgent, finance_agent, web_agent],
    instructions=[
        "Use the RAG Agent to retrieve recipes from the knowledge base.",
        "If you don't find the information in the knowledge base, use the Finance Agent for financial data queries.",
        "If you don't find the information in the knowledge base, use the Web Agent for general web searches.",
    ],
    show_tool_calls=True,
    markdown=True,
    debug_mode=True
)

agent_team.print_response("What's the current stock price of AAPL and can you find a recipe for Thai green curry?", stream=True)

image

@manthanguptaa , See if I copied this code so it's just load the document & don't run the below code after loading the document.

@manthanguptaa
Copy link
Contributor

@MuhammadAhsaanAbbasi please check if you PgVector is working or not. You will have to make sure the infra is up on your side. The code is working on my side and looks perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants