Upload free_ask_internet.py
Browse files- free_ask_internet.py +48 -5
free_ask_internet.py
CHANGED
@@ -279,7 +279,7 @@ def ask_internet(query:str, model:str, debug=False):
|
|
279 |
citation_map = {f"citation:{i+1}": (chr(0x245F + i+1), content_list[i].get('url'))
|
280 |
for i in range(len(content_list))}
|
281 |
|
282 |
-
#
|
283 |
modified_content = response_content
|
284 |
for citation, (symbol, url) in citation_map.items():
|
285 |
if url:
|
@@ -287,12 +287,55 @@ def ask_internet(query:str, model:str, debug=False):
|
|
287 |
f"[{citation}]",
|
288 |
f"[{symbol}]({url} target=\"_blank\")"
|
289 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
291 |
yield modified_content
|
292 |
-
|
|
|
293 |
if True:
|
294 |
-
yield "---\n
|
|
|
|
|
|
|
|
|
|
|
295 |
for idx, url_content in enumerate(content_list):
|
296 |
url = url_content.get('url')
|
297 |
-
|
298 |
-
yield f"* {chr(0x245F + idx+1)} [{url}]({url} target=\"_blank\")*\n"
|
|
|
279 |
citation_map = {f"citation:{i+1}": (chr(0x245F + i+1), content_list[i].get('url'))
|
280 |
for i in range(len(content_list))}
|
281 |
|
282 |
+
# 替换引用标记
|
283 |
modified_content = response_content
|
284 |
for citation, (symbol, url) in citation_map.items():
|
285 |
if url:
|
|
|
287 |
f"[{citation}]",
|
288 |
f"[{symbol}]({url} target=\"_blank\")"
|
289 |
)
|
290 |
+
|
291 |
+
# 输出修改后的内容
|
292 |
+
yield modified_content
|
293 |
+
|
294 |
+
yield "\n\n"
|
295 |
+
# 是否返回参考资料
|
296 |
+
if True:
|
297 |
+
yield "---"
|
298 |
+
yield "\n### 参考资料\n"def ask_internet(query:str, model:str, debug=False):
|
299 |
+
content_list = search_web_ref(query,debug=debug)
|
300 |
+
if debug:
|
301 |
+
print(content_list)
|
302 |
+
prompt = gen_prompt(query,content_list,context_length_limit=6000,debug=debug)
|
303 |
+
total_token = ""
|
304 |
+
|
305 |
+
# 收集所有回答内容
|
306 |
+
response_content = ""
|
307 |
+
for token in chat(prompt=prompt, model=model):
|
308 |
+
if token:
|
309 |
+
total_token += token
|
310 |
+
response_content += token
|
311 |
+
|
312 |
+
# 处理引用链接
|
313 |
+
if content_list:
|
314 |
+
# 创建符号到URL的映射(①对应第一个结果,②对应第二个...)
|
315 |
+
symbol_url_map = {
|
316 |
+
chr(0x2460 + i): content_list[i].get('url')
|
317 |
+
for i in range(len(content_list))
|
318 |
+
}
|
319 |
|
320 |
+
# 替换所有符号为带新标签页打开的链接
|
321 |
+
modified_content = response_content
|
322 |
+
for symbol, url in symbol_url_map.items():
|
323 |
+
if url:
|
324 |
+
# 使用 HTML 语法实现新标签页打开
|
325 |
+
modified_content = modified_content.replace(
|
326 |
+
symbol,
|
327 |
+
f'<a href="{url}" target="_blank">{symbol}</a>'
|
328 |
+
)
|
329 |
yield modified_content
|
330 |
+
yield "\n\n"
|
331 |
+
# 参考资料部分
|
332 |
if True:
|
333 |
+
yield "---\n### 参考资料\n"
|
334 |
+
for idx, url_content in enumerate(content_list):
|
335 |
+
url = url_content.get('url')
|
336 |
+
symbol = chr(0x2460 + idx)
|
337 |
+
# 使用带符号的链接列表
|
338 |
+
yield f"* {symbol} <a href=\"{url}\" target=\"_blank\">{url}</a>\n"
|
339 |
for idx, url_content in enumerate(content_list):
|
340 |
url = url_content.get('url')
|
341 |
+
yield f"* {chr(0x245F + idx+1)} [{url}]({url})\n"
|
|