nhung_unit3_agentic_RAG / retriever.py
Sofpast's picture
submit
e44cbb4
import datasets
from langchain.docstore.document import Document
from smolagents import Tool
from langchain_community.retrievers import BM25Retriever
from smolagents import CodeAgent, InferenceClientModel
import os
# from huggingface_hub import HfApi, InferenceClient
from dotenv import load_dotenv
# import os
load_dotenv()
# Load the Hugging Face API key from environment variables
api_key = os.getenv("HUGGINGFACE_API_KEY")
class GuestInfoRetrieverTool(Tool):
name = "guest_info_retriever"
description = "Retrieves detailed information about gala guests based on their name or relation."
inputs = {
"query": {
"type": "string",
"description": "The name or relation of the guest you want information about."
}
}
output_type = "string"
def __init__(self, docs):
self.is_initialized = False
self.retriever = BM25Retriever.from_documents(docs)
def forward(self, query: str):
results = self.retriever.get_relevant_documents(query)
if results:
return "\n\n".join([doc.page_content for doc in results[:3]])
else:
return "No matching guest information found."
def load_guest_dataset():
# 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
]
# Return the tool
return GuestInfoRetrieverTool(docs)
# Initialize the tool
# guest_info_tool = GuestInfoRetrieverTool(docs)
# Initialize the Hugging Face model
model = InferenceClientModel(token=api_key)
# Create Alfred, our gala agent, with the guest info tool
# alfred = CodeAgent(tools=[guest_info_tool], model=model,
# )
# Example query Alfred might receive during the gala
# response = alfred.run("Tell me about our guest named 'Nhung ham'.")
# print("🎩 Alfred's Response:")
# print(response)