iridescentX commited on
Commit
56049a6
·
verified ·
1 Parent(s): 0f8e02e

Upload search.py

Browse files
Files changed (1) hide show
  1. search.py +165 -0
search.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from pydantic import BaseModel, Field
3
+ from typing import Callable, Any, Optional
4
+
5
+
6
+ class Valves(BaseModel):
7
+ SEARXNG_URL: str = Field(
8
+ default="https://search.ricky.cloudns.be/search",
9
+ description="SearXNG search URL",
10
+ )
11
+ BAIDU_URL: str = Field(
12
+ default="https://www.baidu.com/s", description="Baidu search URL"
13
+ )
14
+ # 添加 Bing 搜索 URL
15
+ BING_URL: str = Field(
16
+ default="https://www.bing.com/search", description="Bing search URL"
17
+ )
18
+ TIMEOUT: int = Field(default=30, description="Request timeout in seconds")
19
+
20
+
21
+ class Tools:
22
+ def __init__(self):
23
+ self.valves = Valves()
24
+ self.reader_api = "https://r.jina.ai/"
25
+ self.citation = True
26
+
27
+ async def read_url(
28
+ self, url: str, __event_emitter__: Optional[Callable[[dict], Any]] = None
29
+ ) -> str:
30
+ """
31
+ Read and extract the main content from a given URL.
32
+ :param url: The URL to read from.
33
+ :return: The main content of the page in clean, LLM-friendly text.
34
+ """
35
+ try:
36
+ if __event_emitter__:
37
+ await __event_emitter__(
38
+ {
39
+ "type": "status",
40
+ "data": {
41
+ "description": f"Reading content from {url}",
42
+ "status": "in_progress",
43
+ },
44
+ }
45
+ )
46
+
47
+ data = {"url": url}
48
+ response = requests.post(
49
+ self.reader_api, data=data, timeout=self.valves.TIMEOUT
50
+ )
51
+ response.raise_for_status()
52
+ content = response.text
53
+
54
+ if __event_emitter__:
55
+ await __event_emitter__(
56
+ {
57
+ "type": "status",
58
+ "data": {
59
+ "description": "Content retrieved",
60
+ "status": "complete",
61
+ },
62
+ }
63
+ )
64
+
65
+ result_presentation = """
66
+
67
+ Result Presentation
68
+ STRICTLY ADHERE TO THIS FORMAT:
69
+ ---
70
+ ## [Site Name - Title](URL)
71
+ 核心信息(Key Insights):
72
+ - Point 1
73
+ - Point 2
74
+ ...
75
+ 总结(Summary):
76
+
77
+ (Leave a blank line between each result summary)
78
+ ---"""
79
+
80
+ return f"{content}\n---\n{result_presentation}"
81
+ except Exception as e:
82
+ if __event_emitter__:
83
+ await __event_emitter__(
84
+ {
85
+ "type": "status",
86
+ "data": {
87
+ "description": f"Error reading URL: {str(e)}",
88
+ "status": "error",
89
+ },
90
+ }
91
+ )
92
+ return f"Error reading URL: {str(e)}"
93
+
94
+ async def search(
95
+ self,
96
+ search_term: str,
97
+ engine: str = "google",
98
+ __event_emitter__: Optional[Callable[[dict], Any]] = None,
99
+ ) -> str:
100
+ """
101
+ Issues a query to a search engine and displays the results. (Note: Use this when the query requires gathering information from external sources, such as current events, unfamiliar terms, or when explicit browsing is requested.)
102
+ :param search_term: The search term or phrase to look up. (Note: think the keywords most likely to help users retrieve information as the "search_term",don't include search engine)
103
+ :param engine: The search engine to use ('google', 'baidu', or 'bing'). Note:bing(Default, suitable for most queries), baidu(Best for content about China), google(Relative equilibrium choices)
104
+ :return: The search results in text format.
105
+ """
106
+ try:
107
+ if __event_emitter__:
108
+ await __event_emitter__(
109
+ {
110
+ "type": "status",
111
+ "data": {
112
+ "description": f"Searching with {engine}: {search_term}",
113
+ "status": "in_progress",
114
+ },
115
+ }
116
+ )
117
+
118
+ if engine == "baidu":
119
+ url = f"{self.reader_api}{self.valves.BAIDU_URL}?wd={search_term}"
120
+ headers = {"X-Target-Selector": "#content_left"}
121
+ elif engine == "bing":
122
+ # 添加 Bing 搜索的处理
123
+ url = f"{self.reader_api}{self.valves.BING_URL}?q={search_term}"
124
+ headers = {"X-Target-Selector": "#b_results"}
125
+ else:
126
+ url = f"{self.reader_api}{self.valves.SEARXNG_URL}?q={search_term}"
127
+ headers = {"X-Target-Selector": "#urls"}
128
+
129
+ response = requests.get(url, headers=headers, timeout=self.valves.TIMEOUT)
130
+ response.raise_for_status()
131
+ content = response.text
132
+
133
+ if __event_emitter__:
134
+ await __event_emitter__(
135
+ {
136
+ "type": "status",
137
+ "data": {
138
+ "description": f"Search completed with {engine}",
139
+ "status": "complete",
140
+ },
141
+ }
142
+ )
143
+
144
+ search_result_processing = """
145
+ Search Result Processing:
146
+ - Identify 1-3 highly relevant results from the search.
147
+ - Focus on the most informative and reliable sources.
148
+ - Integrate the key points from these sources to formulate a comprehensive answer to the query.
149
+ - If applicable, mention additional relevant information inferred from visible snippets in the search results.
150
+ """
151
+
152
+ return f"{content}\n---\n{search_result_processing}"
153
+
154
+ except Exception as e:
155
+ if __event_emitter__:
156
+ await __event_emitter__(
157
+ {
158
+ "type": "status",
159
+ "data": {
160
+ "description": f"Error during search: {str(e)}",
161
+ "status": "error",
162
+ },
163
+ }
164
+ )
165
+ return f"Error during search: {str(e)}"