File size: 2,615 Bytes
b3d4017 c97999e 91e3a88 c97999e eaf9631 b3d4017 c97999e b3d4017 eaf9631 c97999e b3d4017 c97999e 8cb5d70 91e3a88 7a8dd44 8cb5d70 7a8dd44 c97999e b3d4017 b31d773 c97999e eaf9631 c97999e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.core.workflow import Context
from llama_index.core.tools import FunctionTool
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
from llama_index.tools.wikipedia import WikipediaToolSpec
from llama_index.core.tools.tool_spec.load_and_search import LoadAndSearchToolSpec
from llama_index.readers.web import SimpleWebPageReader
from llama_index.core.tools.ondemand_loader_tool import OnDemandLoaderTool
from langfuse.llama_index import LlamaIndexInstrumentor
class BasicAgent:
def __init__(self):
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
# Langfuse
self.instrumentor = LlamaIndexInstrumentor()
# Initialize tools
tool_spec = DuckDuckGoSearchToolSpec()
search_tool = FunctionTool.from_defaults(tool_spec.duckduckgo_full_search)
wiki_spec = WikipediaToolSpec()
wiki_search_tool = wiki_spec.to_tool_list()[1]
# Convert into a LoadAndSearchToolSpec because the wikipedia search tool returns
# entire Wikipedia pages and this can pollute the context window of the LLM
wiki_spec = WikipediaToolSpec()
wiki_search_tool = wiki_spec.to_tool_list()[1]
# Convert into a LoadAndSearchToolSpec because the wikipedia search tool returns
# entire Wikipedia pages and this can pollute the context window of the LLM
wiki_search_tool_las = LoadAndSearchToolSpec.from_defaults(wiki_search_tool).to_tool_list()
webpage_tool = OnDemandLoaderTool.from_defaults(
SimpleWebPageReader(html_to_text=True),
name="Webpage search tool",
description="A tool for loading the content of a webpage and querying it for information",
)
# Create Alfred with all the tools
self.agent = AgentWorkflow.from_tools_or_functions(
wiki_search_tool_las + [search_tool], # webpage_tool does not work properly
llm=llm,
verbose=True,
system_prompt=("You are a helpful agent that can search Wikipedia and the Internet for answers. "
"Please be concise when answering questions and make sure your final outputs are relevant to the question.")
)
# self.ctx = Context(self.agent)
async def __call__(self, question: str) -> str:
response = await self.agent.run(user_msg=question) # ctx=self.ctx)
self.instrumentor.flush()
return response.response.content
|