Spaces:
Sleeping
Sleeping
| import os | |
| import math | |
| from dotenv import load_dotenv | |
| from langchain_ollama import ChatOllama | |
| from langchain.tools import tool | |
| from langchain_community.tools import DuckDuckGoSearchRun | |
| from langgraph.prebuilt import create_react_agent | |
| load_dotenv() | |
| # ββ LLM β runs on your laptop, no API key needed ββββββββββββββββββββββββββββββ | |
| llm = ChatOllama( | |
| model="llama3.2", | |
| temperature=0 | |
| ) | |
| # ββ TOOL 1: Web Search ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| search = DuckDuckGoSearchRun() | |
| def web_search(query: str) -> str: | |
| """Search the internet for current information about any topic.""" | |
| return search.run(query) | |
| # ββ TOOL 2: Calculator ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def calculator(expression: str) -> str: | |
| """Evaluate a mathematical expression. | |
| Examples: '2 + 2', '10 * 5', 'sqrt(16)' | |
| """ | |
| try: | |
| allowed = {k: v for k, v in math.__dict__.items() | |
| if not k.startswith("_")} | |
| result = eval(expression, {"__builtins__": {}}, allowed) | |
| return f"Result: {result}" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # ββ TOOL 3: RAG Retrieval βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def rag_retrieval(query: str) -> str: | |
| """Search a knowledge base about ML, NLP, and RAG systems.""" | |
| import requests | |
| try: | |
| response = requests.post( | |
| "https://rohith2026-hybrid-rag-api.hf.space/search", | |
| json={"query": query, "top_k": 3}, | |
| timeout=15 | |
| ) | |
| if response.status_code == 200: | |
| return str(response.json()) | |
| return f"Status {response.status_code}" | |
| except Exception as e: | |
| return f"RAG unavailable: {str(e)}" | |
| # ββ BUILD AGENT βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| tools = [web_search, calculator, rag_retrieval] | |
| agent = create_react_agent(llm, tools) | |
| # ββ RUN AGENT βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def run_agent(question: str) -> str: | |
| print(f"\n{'='*60}") | |
| print(f"Question: {question}") | |
| print(f"{'='*60}\n") | |
| response = agent.invoke({ | |
| "messages": [{"role": "user", "content": question}] | |
| }) | |
| final = response["messages"][-1].content | |
| print(f"Answer: {final}\n") | |
| return final | |
| # ββ TEST ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if __name__ == "__main__": | |
| run_agent("Use the web_search tool to find what RAG Retrieval Augmented Generation means and summarise it") |