Spaces:
Sleeping
Sleeping
File size: 1,344 Bytes
4b42f38 e8b5a9a 38812af 4b42f38 38812af 4b42f38 e8b5a9a 4b42f38 | 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 | from llama_index.core.schema import Document
from llama_index.core.tools import FunctionTool
from llama_index.retrievers.bm25 import BM25Retriever
from llama_index.core.node_parser import SentenceSplitter
import datasets
# Load the dataset
guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
# Convert dataset entries into Document objects
docs = [
Document(
text="\n".join([
f"Name: {guest['name']}",
f"Relation: {guest['relation']}",
f"Description: {guest['description']}",
f"Email: {guest['email']}"
]),
metadata={"name": guest["name"]}
)
for guest in guest_dataset
]
# initialize node parser
splitter = SentenceSplitter(chunk_size=512)
nodes = splitter.get_nodes_from_documents(docs)
bm25_retriever = BM25Retriever.from_defaults(nodes=nodes)
def get_guest_info_retreiver(query: str) -> str:
"""Fetches guest information based on the query."""
# Retrieve the most relevant document
results = bm25_retriever.retrieve(query)
if results:
# Return the text of the most relevant document
return "\n\n".join([doc.text for doc in results[:3]])
else:
return "No relevant guest information found."
guest_info_tool = FunctionTool.from_defaults(get_guest_info_retreiver)
|