Spaces:
Sleeping
Sleeping
from langchain.docstore.document import Document | |
from langchain_community.retrievers import BM25Retriever | |
from smolagents import Tool | |
from faq_data import faq_entries | |
# Convert the FAQ data into LangChain Documents | |
docs = [ | |
Document( | |
page_content="\n".join([ | |
f"Category: {faq['category']}", | |
f"Question: {faq['question']}", | |
f"Answer: {faq['answer']}" | |
]), | |
metadata={"category": faq["category"]} | |
) | |
for faq in faq_entries | |
] | |
# Create the custom FAQ retriever tool | |
class FAQRetrieverTool(Tool): | |
name = "faq_retriever" | |
description = "Answers company FAQ questions using internal documentation." | |
inputs = { | |
"query": { | |
"type": "string", | |
"description": "The user question to answer from internal FAQ documents." | |
} | |
} | |
output_type = "string" | |
def __init__(self, docs): | |
self.is_initialized = True # <-- questo mancava! | |
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 "Sorry, I couldn't find a matching FAQ." | |
# Initialize the retriever tool | |
faq_tool = FAQRetrieverTool(docs) | |