-
-
Notifications
You must be signed in to change notification settings - Fork 741
feat: Add Agentic RAG Tools Implementation #1085
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from praisonaiagents import Agent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wire an actual web search tool; the referenced internet_search function doesn’t exist. Instructions tell the model to use internet_search, but no tool is registered. Add a minimal DuckDuckGo tool with defensive imports. Apply: +
+def internet_search(query: str) -> str:
+ """
+ Lightweight DuckDuckGo search tool for the Agent.
+ Returns a short bulleted list of top results (title, url, snippet).
+ """
+ try:
+ from duckduckgo_search import DDGS # pip install duckduckgo-search
+ except Exception as import_err:
+ return f"Search tool unavailable: {import_err}"
+ try:
+ with DDGS(timeout=10) as ddgs:
+ results = list(ddgs.text(query, max_results=5))
+ lines = []
+ for r in results:
+ title = r.get("title") or r.get("h") or "Untitled"
+ url = r.get("href") or r.get("u") or ""
+ body = r.get("body") or r.get("a") or ""
+ lines.append(f"- {title} — {url}\n {body}")
+ return "\n".join(lines) if lines else "No results found."
+ except Exception as e:
+ return f"Search error: {e}"📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Set OpenAI API key if not already set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not os.getenv("OPENAI_API_KEY"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Please set your OpenAI API key: export OPENAI_API_KEY='your-api-key-here'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+7
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better practice in command-line applications, it's recommended to print error messages to standard error (
Suggested change
Comment on lines
+4
to
+7
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not exit at import time; move API key check into main and use sys.exit. Top-level exit breaks imports/tests and is brittle. Validate the key inside the main block and call sys.exit(1). Apply this diff to remove the top-level check (see a follow-up comment for adding it inside main): -# Set OpenAI API key if not already set
-if not os.getenv("OPENAI_API_KEY"):
- print("Please set your OpenAI API key: export OPENAI_API_KEY='your-api-key-here'")
- exit(1)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Create the RAG agent with web search capabilities | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rag_agent = Agent( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| instructions="""You are a helpful AI assistant specialized in Thai recipes and cooking. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You have access to a PDF knowledge base about Thai recipes from: https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You can also search the web for additional information about Thai cooking, ingredients, and techniques. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainPDF “knowledge base” is only mentioned in the prompt; no retrieval/RAG is wired. If the goal is Agentic RAG, connect an actual knowledge component (e.g., vector store over ThaiRecipes.pdf) instead of relying solely on instructions. Otherwise, reword the prompt to avoid overstating capabilities. I can help integrate the library’s knowledge/RAG API once confirmed. To locate the supported API surface in this repo, run: 🏁 Script executed: #!/bin/bash
# Locate Knowledge APIs and Agent constructor to see how to pass knowledge.
rg -nP -C2 'class\s+Knowledge\b|def\s+(from_pdfs|from_pdf|add_pdf|add_document|add_documents)\b' src
rg -nP -C3 'class\s+Agent\b|def\s+__init__\(' srcLength of output: 55975 🏁 Script executed: #!/bin/bash
# Locate the Agentic RAG example file
find examples -type f -name 'rag_agent.py'
# Display the first 50 lines to inspect retrieval wiring
sed -n '1,50p' examples/python/tools/exa-tool/rag_examples/agentic_rag/rag_agent.pyLength of output: 1840 Wire actual PDF retrieval or remove PDF reference 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| When answering questions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1. Use your knowledge about Thai cuisine to provide helpful information | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2. If needed, search the web for additional details, current information, or clarification | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3. Provide comprehensive, helpful answers about Thai cuisine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4. Always be informative and helpful about Thai cooking! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You can use the internet_search function to search the web when needed.""", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| llm="gpt-4o", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| markdown=True, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verbose=True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The agent's instructions on line 23 mention using
Suggested change
Comment on lines
+10
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example is presented as a RAG agent, but it doesn't use the framework's RAG capabilities effectively. The PDF URL is only mentioned in the instructions, which doesn't guarantee the agent can access and retrieve information from it. To make this a true RAG example, you should use the Here's a suggestion on how to modify the code to implement this. You would need to add # Download the knowledge base if it doesn't exist
pdf_url = "https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
pdf_path = "ThaiRecipes.pdf"
if not os.path.exists(pdf_path):
print(f"Downloading knowledge base from {pdf_url}...")
try:
response = requests.get(pdf_url, timeout=30)
response.raise_for_status()
with open(pdf_path, "wb") as f:
f.write(response.content)
print("Download complete.")
except requests.RequestException as e:
print(f"Error downloading knowledge base: {e}", file=sys.stderr)
sys.exit(1)
# Create the RAG agent with web search capabilities
rag_agent = Agent(
instructions="""You are a helpful AI assistant specialized in Thai recipes and cooking.
You have access to a PDF knowledge base about Thai recipes.
You can also search the web for additional information about Thai cooking, ingredients, and techniques.
When answering questions:
1. Use your knowledge about Thai cuisine to provide helpful information
2. If needed, search the web for additional details, current information, or clarification
3. Provide comprehensive, helpful answers about Thai cuisine
4. Always be informative and helpful about Thai cooking!
You can use the internet_search function to search the web when needed.""",
knowledge=[pdf_path],
llm="gpt-4o",
markdown=True,
verbose=True,
tools=[Tools.internet_search]
)
Comment on lines
+10
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Register the search tool with the Agent; otherwise the LLM can’t call it. Apply: rag_agent = Agent(
@@
- llm="gpt-4o",
+ llm="gpt-4o",
+ tools=[internet_search],
markdown=True,
verbose=True
)📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("🤖 Thai Recipe RAG Agent is ready!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Ask me anything about Thai recipes or cooking!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Type 'quit' to exit.\n") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate OPENAI_API_KEY inside main and fix the exit message list. Apply: if __name__ == "__main__":
- print("🤖 Thai Recipe RAG Agent is ready!")
- print("Ask me anything about Thai recipes or cooking!")
- print("Type 'quit' to exit.\n")
+ # Validate key at runtime (avoid exiting on import)
+ if not os.getenv("OPENAI_API_KEY"):
+ print("Please set your OpenAI API key first, e.g.:")
+ print(" export OPENAI_API_KEY='your-api-key-here'")
+ sys.exit(1)
+ print("🤖 Thai Recipe RAG Agent is ready!")
+ print("Ask me anything about Thai recipes or cooking!")
+ print("Type 'quit', 'exit', or 'bye' to exit.\n")📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while True: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user_input = input("You: ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if user_input.lower() in ['quit', 'exit', 'bye']: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("👋 Goodbye! Happy cooking!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response = rag_agent.start(user_input) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"\n🤖 Assistant: {response}\n") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"❌ Error: {e}\n") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To provide the agent with web search capabilities as described in the instructions, you need to import the
Toolsclass which contains theinternet_searchtool.