Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,5 +1,60 @@
|
|
| 1 |
from llama_index.core.tools import FunctionTool
|
| 2 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 3 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from llama_index.core.tools import FunctionTool
|
| 2 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 3 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 4 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 5 |
+
from llama_index.schema import Document
|
| 6 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 7 |
+
from langchain_core.tools import tool
|
| 8 |
|
| 9 |
+
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-VL-32B-Instruct")
|
| 10 |
+
|
| 11 |
+
@tool
|
| 12 |
+
def web_search(query: str) -> list:
|
| 13 |
+
"""Search Tavily for a query and return a maximum 3 results as LlamaIndex Documents.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
query: The search query.
|
| 17 |
+
"""
|
| 18 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 19 |
+
|
| 20 |
+
llama_docs = []
|
| 21 |
+
for doc in search_docs:
|
| 22 |
+
source = doc.metadata.get("source", "")
|
| 23 |
+
page = doc.metadata.get("page", "")
|
| 24 |
+
content = doc.page_content
|
| 25 |
+
full_metadata = {
|
| 26 |
+
"source": source,
|
| 27 |
+
"page": page
|
| 28 |
+
}
|
| 29 |
+
llama_docs.append(Document(text=content, metadata=full_metadata))
|
| 30 |
+
|
| 31 |
+
return llama_docs
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def wiki_search(query: str) -> list:
|
| 35 |
+
"""Search Wikipedia for a query and return maximum 2 results as LlamaIndex Documents.
|
| 36 |
+
|
| 37 |
+
Args:
|
| 38 |
+
query: The search query.
|
| 39 |
+
"""
|
| 40 |
+
# Perform Wikipedia search using WikipediaLoader
|
| 41 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 42 |
+
|
| 43 |
+
# Create a list of LlamaIndex Document objects
|
| 44 |
+
llama_docs = []
|
| 45 |
+
for doc in search_docs:
|
| 46 |
+
source = doc.metadata.get("source", "")
|
| 47 |
+
page = doc.metadata.get("page", "")
|
| 48 |
+
content = doc.page_content
|
| 49 |
+
full_metadata = {
|
| 50 |
+
"source": source,
|
| 51 |
+
"page": page
|
| 52 |
+
}
|
| 53 |
+
llama_docs.append(Document(text=content, metadata=full_metadata))
|
| 54 |
+
|
| 55 |
+
return llama_docs
|
| 56 |
+
|
| 57 |
+
agent = AgentWorkflow.from_tools_or_functions(
|
| 58 |
+
[web_search],
|
| 59 |
+
llm=llm
|
| 60 |
+
)
|