Spaces:
Runtime error
Runtime error
Download retriever.py from tu101/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/spaces/tu101/Final_Assignment_Template/resolve/main/retriever.py
- Command line
-
hf download hf://spaces/tu101/Final_Assignment_Template/retriever.py
-
curl -L -o retriever.py https://huggingface.co/spaces/tu101/Final_Assignment_Template/resolve/main/retriever.py
1.27 kB
| from langchain_community.retrievers import BM25Retriever | |
| from langchain.docstore.document import Document | |
| import datasets | |
| from langchain.tools import Tool | |
| from langchain_core.documents import Document | |
| #https://opentdb.com/browse.php | |
| # Load the dataset | |
| guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train") | |
| # Convert dataset entries into Document objects | |
| docs = [ | |
| Document( | |
| page_content="\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 | |
| ] | |
| bm25_retriever = BM25Retriever.from_documents(docs) | |
| def extract_text(query: str) -> str: | |
| """Retrieves detailed information about gala guests based on their name or relation.""" | |
| results = bm25_retriever.invoke(query) | |
| if results: | |
| return "\n\n".join([doc.page_content for doc in results[:3]]) | |
| else: | |
| return "No matching guest information found." | |
| guest_info_tool = Tool( | |
| name="guest_info_retriever", | |
| func=extract_text, | |
| description="Retrieves detailed information about gala guests based on their name or relation." | |
| ) | |