Spaces:
Running
Running
Update tools.py
Browse files
tools.py
CHANGED
@@ -1,118 +1,132 @@
|
|
1 |
-
from langchain_core.tools import tool
|
2 |
-
from sefaria import get_text as sefaria_get_text, get_commentaries as sefaria_get_commentaries
|
3 |
-
from tantivy_search import TantivySearch
|
4 |
-
from typing import Optional
|
5 |
-
from pydantic import BaseModel, Field
|
6 |
-
|
7 |
-
from app import INDEX_PATH
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
-
|
34 |
-
-
|
35 |
-
-
|
36 |
-
-
|
37 |
-
|
38 |
-
|
39 |
-
-
|
40 |
-
-
|
41 |
-
-
|
42 |
-
-
|
43 |
-
-
|
44 |
-
- the
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
-
|
49 |
-
-
|
50 |
-
-
|
51 |
-
|
52 |
-
|
53 |
-
-
|
54 |
-
-
|
55 |
-
-
|
56 |
-
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
-
|
68 |
-
-
|
69 |
-
-
|
70 |
-
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
@tool(args_schema=
|
101 |
-
def
|
102 |
-
"""
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.tools import tool
|
2 |
+
from sefaria import get_text as sefaria_get_text, get_commentaries as sefaria_get_commentaries
|
3 |
+
from tantivy_search import TantivySearch
|
4 |
+
from typing import Optional
|
5 |
+
from pydantic import BaseModel, Field
|
6 |
+
|
7 |
+
from app import INDEX_PATH
|
8 |
+
|
9 |
+
def download_index_from_gdrive(self) -> bool:
|
10 |
+
try:
|
11 |
+
zip_path = "index.zip"
|
12 |
+
url = f"https://drive.google.com/uc?id={self.gdrive_index_id}"
|
13 |
+
gdown.download(url, zip_path, quiet=False)
|
14 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
15 |
+
zip_ref.extractall(".")
|
16 |
+
os.remove(zip_path)
|
17 |
+
return True
|
18 |
+
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"Failed to download index: {str(e)}")
|
21 |
+
return False
|
22 |
+
|
23 |
+
class ReadTextArgs(BaseModel):
|
24 |
+
reference: str = Field(description="The reference to retrieve the text for. examples: 讘专讗砖讬转 讗 驻专拽 讗, Genesis 1:1")
|
25 |
+
|
26 |
+
class SearchArgs(BaseModel):
|
27 |
+
query: str = Field(description="""the query for the search.
|
28 |
+
Instructions for generating a query:
|
29 |
+
|
30 |
+
1. Boolean Operators:
|
31 |
+
|
32 |
+
- AND: term1 AND term2 (both required)
|
33 |
+
- OR: term1 OR term2 (either term)
|
34 |
+
- Multiple words default to OR operation (cloud network = cloud OR network)
|
35 |
+
- AND takes precedence over OR
|
36 |
+
- Example: Shabath AND (walk OR go)
|
37 |
+
|
38 |
+
2. Field-specific Terms:
|
39 |
+
- Field-specific terms: field:term
|
40 |
+
- Example: text:讗讚诐 AND reference:讘专讗砖讬转
|
41 |
+
- available fields: text, reference, topics
|
42 |
+
- text contains the text of the document
|
43 |
+
- reference contains the citation of the document, e.g. 讘专讗砖讬转, 驻专拽 讗
|
44 |
+
- topics contains the topics of the document. available topics includes: 转谞讱, 讛诇讻讛, 诪讚专砖, etc.
|
45 |
+
|
46 |
+
3. Required/Excluded Terms:
|
47 |
+
- Required (+): +term (must contain)
|
48 |
+
- Excluded (-): -term (must not contain)
|
49 |
+
- Example: +security cloud -deprecated
|
50 |
+
- Equivalent to: security AND cloud AND NOT deprecated
|
51 |
+
|
52 |
+
4. Phrase Search:
|
53 |
+
- Use quotes: "exact phrase"
|
54 |
+
- Both single/double quotes work
|
55 |
+
- Escape quotes with \\"
|
56 |
+
- Slop operator: "term1 term2"~N
|
57 |
+
- Example: "cloud security"~2
|
58 |
+
- the above will find "cloud framework and security "
|
59 |
+
- Prefix matching: "start of phrase"*
|
60 |
+
|
61 |
+
5. Wildcards:
|
62 |
+
- ? for single character
|
63 |
+
- * for any number of characters
|
64 |
+
- Example: sec?rity cloud*
|
65 |
+
|
66 |
+
6. Special Features:
|
67 |
+
- All docs: *
|
68 |
+
- Boost terms: term^2.0 (positive numbers only)
|
69 |
+
- Example: security^2.0 cloud
|
70 |
+
- the above will boost security by 2.0
|
71 |
+
|
72 |
+
Query Examples:
|
73 |
+
1. Basic: +砖讘转 +讞讜诇讛 +讗住讜专
|
74 |
+
2. Field-specific: text:住讬谞讬 AND topics:转谞讱
|
75 |
+
3. Phrase with slop: "security framework"~2
|
76 |
+
4. Complex: +reference:讘专讗砖讬转 +text:"讛讘诇"^2.0 +(讚诪讬 OR 讚诪讬诐) -讛讘诇讬诐
|
77 |
+
6. Mixed: (text:"专讘谞讜 诪砖讛"^2.0 OR reference:"诪砖谞讛 转讜专讛") AND topics:讛诇讻讛) AND text:"转讜专讛 讛诪诇讱"~3 AND NOT topics:诪讚专砖
|
78 |
+
|
79 |
+
Tips:
|
80 |
+
- Group complex expressions with parentheses
|
81 |
+
- Use quotes for exact phrases
|
82 |
+
- Add + for required terms, - for excluded terms
|
83 |
+
- Boost important terms with ^N
|
84 |
+
- use field-specific terms for better results.
|
85 |
+
- the corpus to search in is an ancient Hebrew corpus: Tora and Talmud. so Try to use ancient Hebrew terms and or Talmudic expressions and prevent modern words that are not common in talmudic texts
|
86 |
+
""")
|
87 |
+
num_results: int = Field(description="the maximum number of results to return. Default: 10", default=10)
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
index_path = INDEX_PATH
|
92 |
+
try:
|
93 |
+
tantivy = TantivySearch(index_path)
|
94 |
+
tantivy.validate_index()
|
95 |
+
except Exception as e:
|
96 |
+
raise Exception(f"failed to create index: {e}")
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
@tool(args_schema=SearchArgs)
|
101 |
+
def search( query: str, num_results: int = 10):
|
102 |
+
"""Searches the index for the given query."""
|
103 |
+
results = tantivy.search(query, num_results)
|
104 |
+
formatted_results = []
|
105 |
+
for result in results:
|
106 |
+
formatted_results.append({
|
107 |
+
'text': result.get('text', 'N/A'),
|
108 |
+
'reference': result.get('reference', 'N/A')
|
109 |
+
})
|
110 |
+
|
111 |
+
return formatted_results
|
112 |
+
|
113 |
+
|
114 |
+
@tool(args_schema=ReadTextArgs)
|
115 |
+
def read_text(reference: str )->str:
|
116 |
+
"""Retrieves the text for a given reference.
|
117 |
+
"""
|
118 |
+
text = sefaria_get_text(reference)
|
119 |
+
return {
|
120 |
+
'text': str(text),
|
121 |
+
'reference': reference
|
122 |
+
}
|
123 |
+
|
124 |
+
@tool
|
125 |
+
def get_commentaries(reference: str, num_results: int = 10)->str:
|
126 |
+
"""Retrieves references to all available commentaries on the given verse."""
|
127 |
+
commentaries = sefaria_get_commentaries(reference)
|
128 |
+
return {
|
129 |
+
'text': '\n'.join(commentaries) if isinstance(commentaries, list) else str(commentaries),
|
130 |
+
'reference': f"Commentaries on {reference}"
|
131 |
+
}
|
132 |
+
|