from utils import get_doc_tools
from pathlib import Path
from llama_index.core.agent import FunctionCallingAgentWorker
from llama_index.core.agent import AgentRunner
urls = [
"https://openreview.net/pdf?id=VtmBAGCN7o",
"https://openreview.net/pdf?id=6PmJoRfdaK",
"https://openreview.net/pdf?id=hSyW5go0v8",
]
papers = [
"metagpt.pdf",
"longlora.pdf",
"selfrag.pdf",
]
paper_to_tools_dict = {}
for paper in papers:
print(f"Getting tools for paper: {paper}")
vector_tool, summary_tool = get_doc_tools(paper, Path(paper).stem)
paper_to_tools_dict[paper] = [vector_tool, summary_tool]
initial_tools = [t for paper in papers for t in paper_to_tools_dict[paper]] # 총 3개의 문서에 대해 각각 Vector tool, Summary tool 생성
agent_worker = FunctionCallingAgentWorker.from_tools(
initial_tools,
llm=OpenAI(model="gpt-3.5-turbo"),
verbose=True
)
agent = AgentRunner(agent_worker) # 여러 개의 도구를 동시에 사용하며 대답할 수 있음
Tool Retrieval
# define an "object" index and retriever over these tools
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex
from llama_index.core.agent import FunctionCallingAgentWorker
from llama_index.core.agent import AgentRunner
obj_index = ObjectIndex.from_objects( # 도구는 Python 객체이기 때문에, String representation으로 바꿔야 인덱싱이 가능해짐
all_tools,
index_cls=VectorStoreIndex,
)
obj_retriever = obj_index.as_retriever(similarity_top_k=3)
agent_worker = FunctionCallingAgentWorker.from_tools(
tool_retriever=obj_retriever,
llm=llm,
system_prompt=""" \
You are an agent designed to answer queries over a set of given papers.
Please always use the tools provided to answer a question. Do not rely on prior knowledge.\ # 추가적인 가이드가 필요하다면 시스템 프롬프트 추가 가능
""",
verbose=True
)
agent = AgentRunner(agent_worker)