-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocReader.py
More file actions
executable file
·302 lines (263 loc) · 9.73 KB
/
Copy pathDocReader.py
File metadata and controls
executable file
·302 lines (263 loc) · 9.73 KB
1
2
3
4
5
6
7
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
import os
from bs4 import BeautifulSoup
import requests
from urllib.parse import urljoin, urlparse
from typing import List, Tuple, Dict, Any
from openai import OpenAI
from dotenv import load_dotenv
from fastmcp import FastMCP
from prompts_en import *
load_dotenv()
api_key = os.getenv('API_KEY')
client = OpenAI(
base_url="https://api.netmind.ai/inference-api/openai/v1",
api_key=api_key,
)
# Create a FastMCP instance for the MCP Client
mcp = FastMCP(CLIENT_DESCRIPTION)
# Store search history
search_history = []
def extract_doc_links(base_url: str, max_depth: int = 1) -> List[Tuple[str, str]]:
"""
Extract document links and page titles from the base URL.
:param base_url: The starting URL to crawl.
:param max_depth: Maximum crawl depth (default 1).
:return: List of found document links and titles [(url, title)]
"""
visited = set()
to_visit = [(base_url, 0)]
doc_links = []
MAX_LINKS = 100
def normalize_url(url):
parts = urlparse(url)
return f"{parts.scheme}://{parts.netloc}{parts.path}"
while to_visit and len(doc_links) < MAX_LINKS:
url, depth = to_visit.pop(0)
norm_url = normalize_url(url)
if norm_url in visited or depth > max_depth:
continue
visited.add(norm_url)
try:
response = requests.get(url, timeout=10)
if not response.ok:
continue
soup = BeautifulSoup(response.text, 'html.parser')
for link in soup.find_all('a', href=True):
href = link['href']
absolute_url = urljoin(url, href)
if urlparse(absolute_url).netloc != urlparse(base_url).netloc:
continue
link_title = link.get_text().strip() or absolute_url
doc_links.append((absolute_url, link_title))
if depth < max_depth:
to_visit.append((absolute_url, depth + 1))
if len(doc_links) >= MAX_LINKS:
break
except Exception as e:
print(f"Error processing {url}: {str(e)}")
return doc_links
def find_most_relevant_page(pages: List[Tuple[str, str]], prompt: str, max_docs: int = 3) -> List[str]:
"""
Use LLM to find the most relevant pages to the user's prompt.
:param pages: List of pages [(url, title)]
:param prompt: User prompt
:param max_docs: Maximum number of documents to return (default 3)
:return: List of URLs of the most relevant pages
"""
# Format page info for LLM
page_info = "\n".join([f"URL: {url}\nTitle: {title}" for url, title in pages])
# Construct LLM prompt
llm_prompt = PROMPT_FIND_RELEVANT_PAGE.format(max_docs=max_docs, prompt=prompt, page_info=page_info)
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3-0324",
messages=[
{"role": "system", "content": SYSTEM_FIND_RELEVANT_PAGE},
{"role": "user", "content": llm_prompt}
],
temperature=0.3
)
# return the most relevant page URLs
content = response.choices[0].message.content if response and response.choices else ""
return content.strip().splitlines()[:max_docs] if content else []
def extract_page_content(url: str) -> str:
"""
Extract content from a web page.
:param url: Page URL
:return: Extracted page content
"""
try:
response = requests.get(url, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
for script in soup(["script", "style", "header", "footer", "nav"]):
script.extract()
main_content = soup.find("main") or soup.find("article") or soup.find("div", class_="content") or soup.find("div", class_="documentation")
if main_content:
page_content = main_content.get_text(strip=True)
elif soup.body:
page_content = soup.body.get_text(strip=True)
else:
page_content = ""
return page_content
except Exception as e:
print(f"Error getting content from {url}: {str(e)}")
return ""
def add_to_search_history(url: str, query: str, content: str) -> None:
"""Appending content to document search history."""
global search_history
search_history.append({
"url": url,
"query": query,
"content_snippet": content,
"content": content
})
def search_docs(
doc_url: str,
query: str,
depth: int = 2,
max_results: int = 5
) -> List[Dict[str, str]]:
"""
Search documentation pages and find the most relevant pages for the user's query.
Args:
doc_url: The documentation home or index page URL.
query: The user's query or question.
depth: Crawl depth (1-5).
max_results: Maximum number of pages to return.
Returns:
A list of relevant pages, each containing URL and title.
"""
doc_links = extract_doc_links(doc_url, max_depth=depth)
if not doc_links:
return []
relevant_urls = find_most_relevant_page(doc_links, query, max_docs=max_results)
result = []
for url in relevant_urls:
title = next((title for link, title in doc_links if link == url), url)
result.append({"url": url, "title": title})
return result
def extract_content(
url: str,
query: str = ""
) -> Dict[str, Any]:
"""
Extract content from the specified URL.
Args:
url: The page URL to extract content from.
query: The user's query or question (optional, for history recording).
Returns:
A dictionary containing the page content, URL, and title.
"""
content = extract_page_content(url)
if not content:
return {"url": url, "content": "", "success": False}
# 保存到搜索历史
add_to_search_history(url, query, content)
try:
response = requests.get(url, timeout=5)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string if soup.title else url
except:
title = url
return {
"url": url,
"title": title,
"content": content,
"success": True,
"length": len(content)
}
def summarize_findings(
query: str
) -> Dict[str, Any]:
"""
Finish user instruction based on collected information.
:param query: The user's original query or question
:return: LLM's response of the prompt according to the collected information
"""
global search_history
if not search_history:
return {"summary": MSG_NO_INFO_TO_SUMMARIZE, "sources": []}
# Chunk summary params
MAX_CONTENT_LENGTH = 12000
MAX_CHUNK_LENGTH = 3000
def chunk_and_summarize(content, url, query):
"""If content is too long, chunk and summarize each part with respect to the query."""
if len(content) <= MAX_CONTENT_LENGTH:
return content
# Chunk by paragraphs
paragraphs = content.split('\n\n')
chunks = []
current = ""
for para in paragraphs:
if len(current) + len(para) < MAX_CHUNK_LENGTH:
current += para + "\n\n"
else:
chunks.append(current)
current = para + "\n\n"
if current:
chunks.append(current)
summaries = []
for idx, chunk in enumerate(chunks):
prompt = PROMPT_SUMMARIZE_CHUNK.format(query=query, url=url, idx=idx+1, chunk=chunk)
prompt = prompt[:100000]
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3-0324",
messages=[
{"role": "system", "content": SYSTEM_SUMMARIZE_CHUNK},
{"role": "user", "content": prompt}
],
temperature=0.3
)
summary = response.choices[0].message.content if response and response.choices else ""
summaries.append(summary)
return "\n".join(summaries)
# Build summary prompt
content_blocks = []
sources = []
for entry in search_history:
# Summarize each content block
summarized_content = chunk_and_summarize(entry['content'], entry['url'], query)
content_blocks.append(f"Source: {entry['url']}\nContent: {summarized_content}...")
sources.append(entry['url'])
combined_content = "\n\n".join(content_blocks)
summary_prompt = PROMPT_FINAL_RESPONSE.format(query=query, combined_content=combined_content)
# Use LLM to generate summary
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3-0324",
messages=[
{"role": "system", "content": SYSTEM_FINAL_RESPONSE},
{"role": "user", "content": summary_prompt}
],
temperature=0.3
)
summary = response.choices[0].message.content if response and response.choices else MSG_SUMMARY_FAILED
return {
"summary": summary,
"sources": sources,
"query": query
}
@mcp.tool()
def read_doc(
doc_url: str,
query: str,
depth: int = 2,
max_results: int = 3
) -> Dict[str, Any]:
"""
Complete the MCP workflow based on the document URL and user question, and return the final reply.
:param doc_url: Document URL
:param query: User question
:param depth: Crawl depth (1-5)
:param max_results: Number of documents to find (default 3)
"""
results = search_docs(doc_url, query, depth=depth, max_results=max_results)
if not results:
return {"error": MSG_NOT_FOUND}
for result in results:
extract_content(result['url'], query)
summary = summarize_findings(query)
if not summary:
return MSG_FINAL_SUMMARY_FAILED
return summary['summary']
if __name__ == "__main__":
mcp.run()