web search tools
Browse files- agent.py +28 -0
- requirements.txt +2 -1
agent.py
CHANGED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
from typing import TypedDict, List, Dict, Any, Optional
|
|
|
|
| 3 |
from langgraph.graph import StateGraph, START, END
|
| 4 |
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFacePipeline
|
| 5 |
from langchain_core.messages import HumanMessage, AIMessage
|
|
|
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
|
|
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
# Base Hugging Face LLM used by the chat wrapper
|
|
@@ -18,9 +22,33 @@ base_llm = HuggingFaceEndpoint(
|
|
| 18 |
# Chat model that works with LangGraph
|
| 19 |
model = ChatHuggingFace(llm=base_llm)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
class AgentState(TypedDict):
|
| 23 |
messages: List[HumanMessage | AIMessage]
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
def read_message(state: AgentState) -> AgentState:
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import TypedDict, List, Dict, Any, Optional
|
| 3 |
+
from langchain_core import tools
|
| 4 |
from langgraph.graph import StateGraph, START, END
|
| 5 |
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFacePipeline
|
| 6 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 7 |
+
from langchain_core.tools import Tool
|
| 8 |
+
from duckduckgo_search import DDGS
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
+
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
# Base Hugging Face LLM used by the chat wrapper
|
|
|
|
| 22 |
# Chat model that works with LangGraph
|
| 23 |
model = ChatHuggingFace(llm=base_llm)
|
| 24 |
|
| 25 |
+
@tools
|
| 26 |
+
def web_search(keywords: str, max_results:int = 5) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Uses duckduckgo to search the web
|
| 29 |
+
|
| 30 |
+
Use cases:
|
| 31 |
+
- Identify personal information
|
| 32 |
+
- Information search
|
| 33 |
+
- Finding organisation information
|
| 34 |
+
- Obtain the latest news
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
keywords: keywords used to search the web
|
| 38 |
+
max_results: number of results to show after searching the web, defaults to 5
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
Search result (Header + body + URL)
|
| 42 |
+
"""
|
| 43 |
+
with DDGS() as ddgs:
|
| 44 |
+
# Perform a text search
|
| 45 |
+
results = ddgs.text(keywords, max_results)
|
| 46 |
+
print(results)
|
| 47 |
+
|
| 48 |
|
| 49 |
class AgentState(TypedDict):
|
| 50 |
messages: List[HumanMessage | AIMessage]
|
| 51 |
+
search_required:
|
| 52 |
|
| 53 |
|
| 54 |
def read_message(state: AgentState) -> AgentState:
|
requirements.txt
CHANGED
|
@@ -17,4 +17,5 @@ wikipedia
|
|
| 17 |
pgvector
|
| 18 |
python-dotenv
|
| 19 |
pandas
|
| 20 |
-
numpy
|
|
|
|
|
|
| 17 |
pgvector
|
| 18 |
python-dotenv
|
| 19 |
pandas
|
| 20 |
+
numpy
|
| 21 |
+
duckduckgo_search
|