feat: add handoff tool for agent creation and embedding query tool for database
Browse files- tools/__init__.py +6 -1
- tools/handoff_tools.py +31 -0
- tools/search_tools.py +41 -6
tools/__init__.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
|
|
| 1 |
from tools.math_tools import add, div, mod, mult, sub
|
| 2 |
-
from tools.search_tools import arxiv_search
|
|
|
|
|
|
|
| 3 |
|
| 4 |
__all__ = [
|
| 5 |
"add",
|
|
@@ -8,6 +11,8 @@ __all__ = [
|
|
| 8 |
"mult",
|
| 9 |
"sub",
|
| 10 |
"arxiv_search",
|
|
|
|
| 11 |
"internet_search",
|
| 12 |
"wiki_search",
|
|
|
|
| 13 |
]
|
|
|
|
| 1 |
+
from tools.handoff_tools import create_handoff_tool
|
| 2 |
from tools.math_tools import add, div, mod, mult, sub
|
| 3 |
+
from tools.search_tools import arxiv_search
|
| 4 |
+
from tools.search_tools import create_retriever_from_supabase as retriever_tool
|
| 5 |
+
from tools.search_tools import internet_search, wiki_search
|
| 6 |
|
| 7 |
__all__ = [
|
| 8 |
"add",
|
|
|
|
| 11 |
"mult",
|
| 12 |
"sub",
|
| 13 |
"arxiv_search",
|
| 14 |
+
"retriever_tool",
|
| 15 |
"internet_search",
|
| 16 |
"wiki_search",
|
| 17 |
+
"create_handoff_tool",
|
| 18 |
]
|
tools/handoff_tools.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Annotated
|
| 2 |
+
|
| 3 |
+
from langchain_core.tools import InjectedToolCallId, tool
|
| 4 |
+
from langgraph.graph import MessagesState
|
| 5 |
+
from langgraph.prebuilt import InjectedState
|
| 6 |
+
from langgraph.types import Command
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def create_handoff_tool(*, agent_name: str, description: str | None = None):
|
| 10 |
+
name = f"transfer_to_{agent_name}"
|
| 11 |
+
description = description or f"Ask {agent_name} for help."
|
| 12 |
+
|
| 13 |
+
@tool(name_or_callable=name, description=description)
|
| 14 |
+
def handoff_tool(
|
| 15 |
+
state: Annotated[MessagesState, InjectedState],
|
| 16 |
+
tool_call_id: Annotated[str, InjectedToolCallId],
|
| 17 |
+
) -> Command:
|
| 18 |
+
tool_message = {
|
| 19 |
+
"role": "tool",
|
| 20 |
+
"content": f"Successfully transferred to {agent_name}",
|
| 21 |
+
"name": name,
|
| 22 |
+
"tool_call_id": tool_call_id,
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
return Command(
|
| 26 |
+
goto=agent_name,
|
| 27 |
+
update={**state, "messages": state["messages"] + [tool_message]},
|
| 28 |
+
graph=Command.PARENT,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
return handoff_tool
|
tools/search_tools.py
CHANGED
|
@@ -1,8 +1,43 @@
|
|
|
|
|
| 1 |
from typing import Dict, List
|
| 2 |
|
|
|
|
| 3 |
from langchain_community.document_loaders import ArxivLoader, WikipediaLoader
|
|
|
|
| 4 |
from langchain_core.tools import tool
|
|
|
|
| 5 |
from langchain_tavily import TavilySearch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
@tool
|
|
@@ -10,7 +45,7 @@ def internet_search(query: str) -> Dict[str, List[Dict[str, str]]]:
|
|
| 10 |
"""Perform a web search using Tavily Search API.
|
| 11 |
|
| 12 |
This tool searches the web for relevant information based on the provided query.
|
| 13 |
-
It returns up to
|
| 14 |
|
| 15 |
Args:
|
| 16 |
query (str): The search query to look up on the web.
|
|
@@ -22,7 +57,7 @@ def internet_search(query: str) -> Dict[str, List[Dict[str, str]]]:
|
|
| 22 |
- Title: Title of the webpage
|
| 23 |
- Content: Main content/text from the webpage
|
| 24 |
"""
|
| 25 |
-
response = TavilySearch(max_results=
|
| 26 |
|
| 27 |
formatted_answer = [
|
| 28 |
{
|
|
@@ -41,7 +76,7 @@ def wiki_search(query: str) -> Dict[str, List[Dict[str, str]]]:
|
|
| 41 |
"""Search Wikipedia articles using the provided query.
|
| 42 |
|
| 43 |
This tool searches Wikipedia for articles matching the query and returns
|
| 44 |
-
up to
|
| 45 |
|
| 46 |
Args:
|
| 47 |
query (str): The search query to look up on Wikipedia.
|
|
@@ -53,7 +88,7 @@ def wiki_search(query: str) -> Dict[str, List[Dict[str, str]]]:
|
|
| 53 |
- Title: Title of the Wikipedia article
|
| 54 |
- Content: Main content/text from the article
|
| 55 |
"""
|
| 56 |
-
docs = WikipediaLoader(query=query, load_max_docs=
|
| 57 |
|
| 58 |
formatted_answer = [
|
| 59 |
{
|
|
@@ -72,7 +107,7 @@ def arxiv_search(query: str) -> Dict[str, List[Dict[str, str]]]:
|
|
| 72 |
"""Search academic papers on arXiv using the provided query.
|
| 73 |
|
| 74 |
This tool searches arXiv for academic papers matching the query and returns
|
| 75 |
-
up to
|
| 76 |
|
| 77 |
Args:
|
| 78 |
query (str): The search query to look up on arXiv.
|
|
@@ -84,7 +119,7 @@ def arxiv_search(query: str) -> Dict[str, List[Dict[str, str]]]:
|
|
| 84 |
- Title: Title of the academic paper
|
| 85 |
- Content: Main content/abstract of the paper
|
| 86 |
"""
|
| 87 |
-
docs = ArxivLoader(query=query, load_max_docs=
|
| 88 |
|
| 89 |
formatted_answer = [
|
| 90 |
{
|
|
|
|
| 1 |
+
import os
|
| 2 |
from typing import Dict, List
|
| 3 |
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
from langchain_community.document_loaders import ArxivLoader, WikipediaLoader
|
| 6 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 7 |
from langchain_core.tools import tool
|
| 8 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 9 |
from langchain_tavily import TavilySearch
|
| 10 |
+
from supabase.client import create_client
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def create_retriever_from_supabase(query: str) -> str:
|
| 17 |
+
"""Search for similar documents in the Supabase vector store.
|
| 18 |
+
|
| 19 |
+
This tool uses semantic search to find documents that are semantically similar to the provided query.
|
| 20 |
+
It leverages the Supabase vector store and HuggingFace embeddings to perform the search.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
query (str): The search query to find similar documents.
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
str: A list of documents that are semantically similar to the query.
|
| 27 |
+
"""
|
| 28 |
+
embeddings = HuggingFaceEmbeddings(
|
| 29 |
+
model_name="sentence-transformers/all-mpnet-base-v2"
|
| 30 |
+
)
|
| 31 |
+
supabase = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_KEY"])
|
| 32 |
+
|
| 33 |
+
vector_store = SupabaseVectorStore(
|
| 34 |
+
client=supabase,
|
| 35 |
+
embedding=embeddings,
|
| 36 |
+
table_name="documents",
|
| 37 |
+
query_name="match_documents_langchain",
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
return vector_store.similarity_search(query)
|
| 41 |
|
| 42 |
|
| 43 |
@tool
|
|
|
|
| 45 |
"""Perform a web search using Tavily Search API.
|
| 46 |
|
| 47 |
This tool searches the web for relevant information based on the provided query.
|
| 48 |
+
It returns up to 3 most relevant results with their sources, titles, and content.
|
| 49 |
|
| 50 |
Args:
|
| 51 |
query (str): The search query to look up on the web.
|
|
|
|
| 57 |
- Title: Title of the webpage
|
| 58 |
- Content: Main content/text from the webpage
|
| 59 |
"""
|
| 60 |
+
response = TavilySearch(max_results=3).invoke(query)
|
| 61 |
|
| 62 |
formatted_answer = [
|
| 63 |
{
|
|
|
|
| 76 |
"""Search Wikipedia articles using the provided query.
|
| 77 |
|
| 78 |
This tool searches Wikipedia for articles matching the query and returns
|
| 79 |
+
up to 3 most relevant results with their sources, titles, and content.
|
| 80 |
|
| 81 |
Args:
|
| 82 |
query (str): The search query to look up on Wikipedia.
|
|
|
|
| 88 |
- Title: Title of the Wikipedia article
|
| 89 |
- Content: Main content/text from the article
|
| 90 |
"""
|
| 91 |
+
docs = WikipediaLoader(query=query, load_max_docs=3).load()
|
| 92 |
|
| 93 |
formatted_answer = [
|
| 94 |
{
|
|
|
|
| 107 |
"""Search academic papers on arXiv using the provided query.
|
| 108 |
|
| 109 |
This tool searches arXiv for academic papers matching the query and returns
|
| 110 |
+
up to 3 most relevant results with their sources, titles, and content.
|
| 111 |
|
| 112 |
Args:
|
| 113 |
query (str): The search query to look up on arXiv.
|
|
|
|
| 119 |
- Title: Title of the academic paper
|
| 120 |
- Content: Main content/abstract of the paper
|
| 121 |
"""
|
| 122 |
+
docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 123 |
|
| 124 |
formatted_answer = [
|
| 125 |
{
|