junaid17 commited on
Commit
1d8d8b5
·
verified ·
1 Parent(s): bce65eb

Delete tools.py

Browse files
Files changed (1) hide show
  1. tools.py +0 -108
tools.py DELETED
@@ -1,108 +0,0 @@
1
- from langchain_core.tools import tool
2
- from langchain_text_splitters import RecursiveCharacterTextSplitter
3
- from langchain_community.vectorstores import FAISS
4
- from langchain_community.document_loaders import PyPDFLoader
5
- from langchain_openai import OpenAIEmbeddings
6
- from langchain_community.tools import WikipediaQueryRun, ArxivQueryRun
7
- from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper
8
- from langchain_community.tools.tavily_search import TavilySearchResults
9
- from dotenv import load_dotenv
10
- import os
11
-
12
- load_dotenv()
13
-
14
- # ==============================
15
- # CONFIG
16
- # ==============================
17
- VECTORSTORE_DIR = "data/vectorstore"
18
- os.makedirs(VECTORSTORE_DIR, exist_ok=True)
19
-
20
-
21
- # ==============================
22
- # VECTOR STORE CREATION
23
- # ==============================
24
- def build_vectorstore(file_path: str):
25
- loader = PyPDFLoader(file_path)
26
- documents = loader.load()
27
-
28
- splitter = RecursiveCharacterTextSplitter(
29
- chunk_size=500,
30
- chunk_overlap=100
31
- )
32
-
33
- chunks = splitter.split_documents(documents)
34
-
35
- embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
36
- vectorstore = FAISS.from_documents(chunks, embeddings)
37
-
38
- vectorstore.save_local(VECTORSTORE_DIR)
39
- return vectorstore
40
-
41
-
42
- def update_retriever(file_path: str):
43
- """Rebuild vectorstore when a new document is uploaded."""
44
- build_vectorstore(file_path)
45
-
46
-
47
- # ==============================
48
- # RAG TOOL (HF SAFE)
49
- # ==============================
50
- def create_rag_tool():
51
-
52
- @tool
53
- def rag_search(query: str) -> str:
54
- """
55
- Retrieve relevant information from uploaded documents.
56
- """
57
-
58
- if not os.path.exists(os.path.join(VECTORSTORE_DIR, "index.faiss")):
59
- return "No document has been uploaded yet."
60
-
61
- embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
62
-
63
- vectorstore = FAISS.load_local(
64
- VECTORSTORE_DIR,
65
- embeddings,
66
- allow_dangerous_deserialization=True
67
- )
68
-
69
- retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
70
- docs = retriever.invoke(query)
71
-
72
- if not docs:
73
- return "No relevant information found in the document."
74
-
75
- return "\n\n".join(d.page_content for d in docs)
76
-
77
- return rag_search
78
-
79
-
80
- # ==============================
81
- # EXTRA TOOLS
82
- # ==============================
83
-
84
- @tool
85
- def wikipedia_search(query: str) -> dict:
86
- """Search Wikipedia."""
87
- try:
88
- return {"results": WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()).run(query)}
89
- except Exception as e:
90
- return {"error": str(e)}
91
-
92
-
93
- @tool
94
- def arxiv_search(query: str) -> dict:
95
- """Search academic papers on arXiv."""
96
- try:
97
- return {"results": ArxivQueryRun(api_wrapper=ArxivAPIWrapper()).run(query)}
98
- except Exception as e:
99
- return {"error": str(e)}
100
-
101
-
102
- @tool
103
- def tavily_search(query: str) -> dict:
104
- """Search the web using Tavily."""
105
- try:
106
- return {"results": TavilySearchResults(max_results=5).run(query)}
107
- except Exception as e:
108
- return {"error": str(e)}