에이전트가 활용할 수 있는 일종의 인터페이스
from llama_index.core import SummaryIndex
from llama_index.core.tools import QueryEngineTool
summary_index = SummaryIndex(nodes)
summary_query_engine = summary_index.as_query_engine(
response_mode="tree_summarize",
use_async=True,
)
summary_tool = QueryEngineTool.from_defaults( # Query Engine을 Tool처럼 사용
name="summary_tool",
query_engine=summary_query_engine,
description=("Useful if you want to get a summary of MetaGPT"),
)
from llama_index.core.tools import FunctionTool
def add(x: int, y: int) -> int:
"""Adds two integers together.""" # LLM의 프롬프트로 사용됨
return x + y
def mystery(x: int, y: int) -> int:
"""Mystery function that operates on top of two numbers."""
return (x + y) * (x + y)
add_tool = FunctionTool.from_defaults(fn=add)
mystery_tool = FunctionTool.from_defaults(fn=mystery)
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-3.5-turbo")
response = llm.predict_and_call(
[add_tool, mystery_tool], # 사용할 도구
"Tell me the output of the mystery function on 2 and 9", # 프롬프트 혹은 일련의 ChatMessage
verbose=True
)
from llama_index.core import SimpleDirectoryReader
documents = SimpleDirectoryReader(input_files=["metagpt.pdf"]).load_data() # 문서 로드
from llama_index.core.node_parser import SentenceSplitter
splitter = SentenceSplitter(chunk_size=1024)
nodes = splitter.get_nodes_from_documents(documents) # 문서를 노드로 분할
print(nodes[0].get_content(metadata_mode="all")) # 첫 번째 노드의 정보를 출력 (메타데이터 포함)
# page_label: 1
# file_name: metagpt.pdf
# file_path: metagpt.pdf
# file_type: application/pdf
# file_size: 16911937
# creation_date: 2024-05-13
# last_modified_date: 2024-04-23 ...
from llama_index.core import VectorStoreIndex
from llama_index.core.vector_stores import MetadataFilters
vector_index = VectorStoreIndex(nodes)
query_engine = vector_index.as_query_engine(
similarity_top_k=2,
filters=MetadataFilters.from_dicts(
[
{"key": "page_label", "value": "2"} # page_label 메타데이터 정보를 활용해 필터링
]
)
)
response = query_engine.query(
"What are some high-level results of MetaGPT?",
)