Ubik80 commited on
Commit
599b410
·
verified ·
1 Parent(s): 4dc3ec9

added retrivier tool

Browse files
Files changed (1) hide show
  1. faq_retriever.py +41 -0
faq_retriever.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.docstore.document import Document
2
+ from langchain_community.retrievers import BM25Retriever
3
+ from smolagents import Tool
4
+ from faq_data import faq_entries
5
+
6
+ # Convert the FAQ data into LangChain Documents
7
+ docs = [
8
+ Document(
9
+ page_content="\n".join([
10
+ f"Category: {faq['category']}",
11
+ f"Question: {faq['question']}",
12
+ f"Answer: {faq['answer']}"
13
+ ]),
14
+ metadata={"category": faq["category"]}
15
+ )
16
+ for faq in faq_entries
17
+ ]
18
+
19
+ # Create the custom FAQ retriever tool
20
+ class FAQRetrieverTool(Tool):
21
+ name = "faq_retriever"
22
+ description = "Retrieves relevant internal company FAQ entries based on a user query."
23
+ inputs = {
24
+ "query": {
25
+ "type": "string",
26
+ "description": "User's question about internal company processes or services."
27
+ }
28
+ }
29
+ output_type = "string"
30
+
31
+ def __init__(self, documents):
32
+ self.retriever = BM25Retriever.from_documents(documents)
33
+
34
+ def forward(self, query: str) -> str:
35
+ results = self.retriever.get_relevant_documents(query)
36
+ if not results:
37
+ return "No relevant FAQ entry found."
38
+ return "\n\n".join(doc.page_content for doc in results[:2]) # Return top 2
39
+
40
+ # Initialize the retriever tool
41
+ faq_tool = FAQRetrieverTool(docs)