From 957da731dbfd3468bb90a3ea858207e01a16d98b Mon Sep 17 00:00:00 2001 From: binary-husky Date: Thu, 13 Feb 2025 00:19:43 +0800 Subject: [PATCH] upgrade internet gpt plugin --- README.md | 2 +- config.py | 2 + crazy_functional.py | 33 +----- crazy_functions/Internet_GPT.py | 74 ++++++++++-- crazy_functions/Internet_GPT_Wrap.py | 7 +- tests/test_markdown.py | 167 ++++++++++++++++++++++++--- tests/test_media.py | 2 - tests/test_plugins.py | 2 - themes/common.css | 9 ++ 9 files changed, 233 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index a3d004b0..8520738a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ > [!IMPORTANT] -> `master主分支`最新动态(2025.2.4): 增加deepseek-r1支持 +> `master主分支`最新动态(2025.2.13): 联网组件支持Jina的api / 增加deepseek-r1支持 > `frontier开发分支`最新动态(2024.12.9): 更新对话时间线功能,优化xelatex论文翻译 > `wiki文档`最新动态(2024.12.5): 更新ollama接入指南 > diff --git a/config.py b/config.py index 297ccbf2..709d4455 100644 --- a/config.py +++ b/config.py @@ -344,6 +344,8 @@ NUM_CUSTOM_BASIC_BTN = 4 DAAS_SERVER_URLS = [ f"https://niuziniu-biligpt{i}.hf.space/stream" for i in range(1,5) ] +# 在互联网搜索组件中,负责将搜索结果整理成干净的Markdown +JINA_API_KEY = "" """ --------------- 配置关联关系说明 --------------- diff --git a/crazy_functional.py b/crazy_functional.py index 975f61b8..2d26754b 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -434,36 +434,6 @@ def get_crazy_functions(): logger.error(trimmed_format_exc()) logger.error("Load function plugin failed") - # try: - # from crazy_functions.联网的ChatGPT import 连接网络回答问题 - - # function_plugins.update( - # { - # "连接网络回答问题(输入问题后点击该插件,需要访问谷歌)": { - # "Group": "对话", - # "Color": "stop", - # "AsButton": False, # 加入下拉菜单中 - # # "Info": "连接网络回答问题(需要访问谷歌)| 输入参数是一个问题", - # "Function": HotReload(连接网络回答问题), - # } - # } - # ) - # from crazy_functions.联网的ChatGPT_bing版 import 连接bing搜索回答问题 - - # function_plugins.update( - # { - # "连接网络回答问题(中文Bing版,输入问题后点击该插件)": { - # "Group": "对话", - # "Color": "stop", - # "AsButton": False, # 加入下拉菜单中 - # "Info": "连接网络回答问题(需要访问中文Bing)| 输入参数是一个问题", - # "Function": HotReload(连接bing搜索回答问题), - # } - # } - # ) - # except: - # logger.error(trimmed_format_exc()) - # logger.error("Load function plugin failed") try: from crazy_functions.SourceCode_Analyse import 解析任意code项目 @@ -771,6 +741,9 @@ def get_multiplex_button_functions(): "常规对话": "", + "查互联网后回答": + "查互联网后回答", + "多模型对话": "询问多个GPT模型", # 映射到上面的 `询问多个GPT模型` 插件 diff --git a/crazy_functions/Internet_GPT.py b/crazy_functions/Internet_GPT.py index 56856f68..12c22f41 100644 --- a/crazy_functions/Internet_GPT.py +++ b/crazy_functions/Internet_GPT.py @@ -175,10 +175,17 @@ def scrape_text(url, proxies) -> str: Returns: str: The scraped text """ + from loguru import logger headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36', 'Content-Type': 'text/plain', } + + # 首先采用Jina进行文本提取 + if get_conf("JINA_API_KEY"): + try: return jina_scrape_text(url) + except: logger.debug("Jina API 请求失败,回到旧方法") + try: response = requests.get(url, headers=headers, proxies=proxies, timeout=8) if response.encoding == "ISO-8859-1": response.encoding = response.apparent_encoding @@ -193,6 +200,24 @@ def scrape_text(url, proxies) -> str: text = "\n".join(chunk for chunk in chunks if chunk) return text + +def jina_scrape_text(url) -> str: + "jina_39727421c8fa4e4fa9bd698e5211feaaDyGeVFESNrRaepWiLT0wmHYJSh-d" + headers = { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36', + 'Content-Type': 'text/plain', + "X-Retain-Images": "none", + "Authorization": f'Bearer {get_conf("JINA_API_KEY")}' + } + response = requests.get("https://r.jina.ai/" + url, headers=headers, proxies=None, timeout=8) + if response.status_code != 200: + raise ValueError("Jina API 请求失败,开始尝试旧方法!" + response.text) + if response.encoding == "ISO-8859-1": response.encoding = response.apparent_encoding + result = response.text + result = result.replace("\\[", "[").replace("\\]", "]").replace("\\(", "(").replace("\\)", ")") + return response.text + + def internet_search_with_analysis_prompt(prompt, analysis_prompt, llm_kwargs, chatbot): from toolbox import get_conf proxies = get_conf('proxies') @@ -246,23 +271,52 @@ def 连接网络回答问题(txt, llm_kwargs, plugin_kwargs, chatbot, history, s urls = search_optimizer(txt, proxies, optimizer_history, llm_kwargs, optimizer, categories, searxng_url, engines) history = [] if len(urls) == 0: - chatbot.append((f"结论:{txt}", - "[Local Message] 受到限制,无法从searxng获取信息!请尝试更换搜索引擎。")) + chatbot.append((f"结论:{txt}", "[Local Message] 受到限制,无法从searxng获取信息!请尝试更换搜索引擎。")) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return # ------------- < 第2步:依次访问网页 > ------------- + from concurrent.futures import ThreadPoolExecutor + from textwrap import dedent max_search_result = 5 # 最多收纳多少个网页的结果 if optimizer == "开启(增强)": max_search_result = 8 - chatbot.append(["联网检索中 ...", None]) - for index, url in enumerate(urls[:max_search_result]): - res = scrape_text(url['link'], proxies) - prefix = f"第{index}份搜索结果 [源自{url['source'][0]}搜索] ({url['title'][:25]}):" - history.extend([prefix, res]) - res_squeeze = res.replace('\n', '...') - chatbot[-1] = [prefix + "\n\n" + res_squeeze[:500] + "......", None] - yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + template = dedent(""" +
+ {TITLE} +
{URL}
+
{CONTENT}
+
+ """) + + buffer = "" + + # 创建线程池 + with ThreadPoolExecutor(max_workers=5) as executor: + # 提交任务到线程池 + futures = [] + for index, url in enumerate(urls[:max_search_result]): + future = executor.submit(scrape_text, url['link'], proxies) + futures.append((index, future, url)) + + # 处理完成的任务 + for index, future, url in futures: + # 开始 + prefix = f"正在加载 第{index+1}份搜索结果 [源自{url['source'][0]}搜索] ({url['title'][:25]}):" + string_structure = template.format(TITLE=prefix, URL=url['link'], CONTENT="正在加载,请稍后 ......") + yield from update_ui_lastest_msg(lastmsg=(buffer + string_structure), chatbot=chatbot, history=history, delay=0.1) # 刷新界面 + + # 获取结果 + res = future.result() + + # 显示结果 + prefix = f"第{index+1}份搜索结果 [源自{url['source'][0]}搜索] ({url['title'][:25]}):" + string_structure = template.format(TITLE=prefix, URL=url['link'], CONTENT=res[:1000] + "......") + buffer += string_structure + + # 更新历史 + history.extend([prefix, res]) + yield from update_ui_lastest_msg(lastmsg=buffer, chatbot=chatbot, history=history, delay=0.1) # 刷新界面 # ------------- < 第3步:ChatGPT综合 > ------------- if (optimizer != "开启(增强)"): diff --git a/crazy_functions/Internet_GPT_Wrap.py b/crazy_functions/Internet_GPT_Wrap.py index 3534a3cb..3cf92a9c 100644 --- a/crazy_functions/Internet_GPT_Wrap.py +++ b/crazy_functions/Internet_GPT_Wrap.py @@ -38,11 +38,12 @@ class NetworkGPT_Wrap(GptAcademicPluginTemplate): } return gui_definition - def execute(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request): + def execute(txt, llm_kwargs, plugin_kwargs:dict, chatbot, history, system_prompt, user_request): """ 执行插件 """ - if plugin_kwargs["categories"] == "网页": plugin_kwargs["categories"] = "general" - if plugin_kwargs["categories"] == "学术论文": plugin_kwargs["categories"] = "science" + if plugin_kwargs.get("categories", None) == "网页": plugin_kwargs["categories"] = "general" + elif plugin_kwargs.get("categories", None) == "学术论文": plugin_kwargs["categories"] = "science" + else: plugin_kwargs["categories"] = "general" yield from 连接网络回答问题(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request) diff --git a/tests/test_markdown.py b/tests/test_markdown.py index b372154c..85f1534e 100644 --- a/tests/test_markdown.py +++ b/tests/test_markdown.py @@ -45,28 +45,161 @@ Any folded content here. It requires an empty line just above it. md =""" -在这种场景中,您希望机器 B 能够通过轮询机制来间接地“请求”机器 A,而实际上机器 A 只能主动向机器 B 发出请求。这是一种典型的客户端-服务器轮询模式。下面是如何实现这种机制的详细步骤: +
+第0份搜索结果 [源自google搜索] (汤姆·赫兰德): +
https://baike.baidu.com/item/%E6%B1%A4%E5%A7%86%C2%B7%E8%B5%AB%E5%85%B0%E5%BE%B7/3687216
+
Title: 汤姆·赫兰德 -### 机器 B 的实现 +URL Source: https://baike.baidu.com/item/%E6%B1%A4%E5%A7%86%C2%B7%E8%B5%AB%E5%85%B0%E5%BE%B7/3687216 -1. **安装 FastAPI 和必要的依赖库**: - ```bash - pip install fastapi uvicorn - ``` +Markdown Content: +网页新闻贴吧知道网盘图片视频地图文库资讯采购百科 +百度首页 +登录 +注册 +进入词条 +全站搜索 +帮助 +首页 +秒懂百科 +特色百科 +知识专题 +加入百科 +百科团队 +权威合作 +个人中心 +汤姆·赫兰德 +播报 +讨论 +上传视频 +英国男演员 +汤姆·赫兰德(Tom Holland),1996年6月1日出生于英国英格兰泰晤士河畔金斯顿,英国男演员。2008年,出演音乐剧《跳出我天地》而崭露头角。2010年,作为主演参加音乐剧《跳出我天地》的五周年特别演出。2012年10月11日,主演的个人首部电影《海啸奇迹》上映,并凭该电影获得第84届美国国家评论协会奖最具突破男演员奖。2016年10月15日,与查理·汉纳姆、西耶娜·米勒合作出演的电影《 ... >>> -2. **创建 FastAPI 服务**: - ```python - from fastapi import FastAPI - from fastapi.responses import JSONResponse - from uuid import uuid4 - from threading import Lock - import time +目录 +1早年经历 +2演艺经历 +▪影坛新星 +▪角色多变 +▪跨界翘楚 +3个人生活 +▪家庭 +▪恋情 +▪社交 +4主要作品 +▪参演电影 +▪参演电视剧 +▪配音作品 +▪导演作品 +▪杂志写真 +5社会活动 +6获奖记录 +7人物评价 +基本信息 +汤姆·赫兰德(Tom Holland),1996年6月1日出生于英国英格兰泰晤士河畔金斯顿,英国男演员。 [67] +2008年,出演音乐剧《跳出我天地》而崭露头角 [1]。2010年,作为主演参加音乐剧《跳出我天地》的五周年特别演出 [2]。2012年10月11日,主演的个人首部电影《海啸奇迹》上映,并凭该电影获得第84届美国国家评论协会奖最具突破男演员奖 [3]。2016年10月15日,与查理·汉纳姆、西耶娜·米勒合作出演的电影《迷失Z城》在纽约电影节首映 [17];2017年,主演的《蜘蛛侠:英雄归来》上映,他凭该电影获得第19届青少年选择奖最佳暑期电影男演员奖,以及第70届英国电影和电视艺术学院奖最佳新星奖。 [72]2019年,主演的电影《蜘蛛侠:英雄远征》上映 [5];同年,凭借该电影获得第21届青少年选择奖最佳夏日电影男演员奖 [6]。2024年4月,汤姆·霍兰德主演的伦敦西区新版舞台剧《罗密欧与朱丽叶》公布演员名单。 [66] +2024年,......
+
- app = FastAPI() +
+第1份搜索结果 [源自google搜索] (汤姆·霍兰德): +
https://zh.wikipedia.org/zh-hans/%E6%B1%A4%E5%A7%86%C2%B7%E8%B5%AB%E5%85%B0%E5%BE%B7
+
Title: 汤姆·赫兰德 - # 字典用于存储请求和状态 - requests = {} - process_lock = Lock() +URL Source: https://zh.wikipedia.org/zh-hans/%E6%B1%A4%E5%A7%86%C2%B7%E8%B5%AB%E5%85%B0%E5%BE%B7 + +Published Time: 2015-06-24T01:08:01Z + +Markdown Content: +| 汤姆·霍兰德 +Tom Holland | +| --- | +| [![Image 19](https://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Tom_Holland_by_Gage_Skidmore.jpg/220px-Tom_Holland_by_Gage_Skidmore.jpg)](https://zh.wikipedia.org/wiki/File:Tom_Holland_by_Gage_Skidmore.jpg) + +2016年在[圣地牙哥国际漫画展](https://zh.wikipedia.org/wiki/%E8%81%96%E5%9C%B0%E7%89%99%E5%93%A5%E5%9C%8B%E9%9A%9B%E6%BC%AB%E7%95%AB%E5%B1%95 "圣地牙哥国际漫画展")的霍兰德 + + + + | +| 男演员 | +| 昵称 | 荷兰弟[\[1\]](https://zh.wikipedia.org/zh-hans/%E6%B1%A4%E5%A7%86%C2%B7%E8%B5%AB%E5%85%B0%E5%BE%B7#cite_note-1) | +| 出生 | 汤玛斯·史丹利·霍兰德 +(Thomas Stanley Holland)[\[2\]](https://zh.wikipedia.org/zh-hans/%E6%B1%A4%E5%A7%86%C2%B7%E8%B5%AB%E5%85%B0%E5%BE%B7#cite_note-2) + + +1996年6月1日(28岁) + +英国[英格兰](https://zh.wikipedia.org/wiki/%E8%8B%B1%E6%A0%BC%E8%98%AD "英格兰")[泰晤士河畔金斯顿](https://zh.wikipedia.org/wiki/%E6%B3%B0%E6%99%A4......
+
+ +
+第2份搜索结果 [源自google搜索] (为什么汤姆赫兰德被称为荷兰弟?): +
https://www.zhihu.com/question/363988307
+
Title: 为什么汤姆赫兰德被称为荷兰弟? - 知乎 + +URL Source: https://www.zhihu.com/question/363988307 + +Markdown Content: +要说漫威演员里面,谁是最牛的存在,不好说,各有各的看法,但要说谁是最能剧透的,毫无疑问,是我们的汤姆赫兰德荷兰弟,可以说,他算得上是把剧透给玩明白了,先后剧透了不少的电影桥段,以至于漫威后面像防贼一样防着人家荷兰弟,可大家知道吗?你永远想象不到荷兰弟的嘴巴到底有多能漏风? + +![Image 9](https://pica.zhimg.com/50/v2-a0aa9972315519ec4975f974f01fc6ca_720w.jpg?source=1def8aca) + +故事要回到《侏罗纪世界2》的筹备期间,当时,荷兰弟也参与了面试,计划在剧中饰演一个角色,原本,这也没啥,这都是好莱坞的传统了,可是,当时的导演胡安根本不知道荷兰弟的“风光伟绩”,于是乎,人家便屁颠屁颠把侏罗纪世界2的资料拿过来给荷兰弟,虽然,后面没有让荷兰弟出演这部电影,但导演似乎忘了他的嘴巴是开过光的。 + +![Image 10](https://picx.zhimg.com/50/v2-1da72b482c6a44e1826abb430d95a062_720w.jpg?source=1def8aca) + +荷兰弟把剧情刻在了脑子 +......
+
+ +
+第3份搜索结果 [源自google搜索] (爱戴名表被喷配不上赞达亚,荷兰弟曝近照气质大变,2): +
https://www.sohu.com/a/580380519_120702487
+
Title: 爱戴名表被喷配不上赞达亚,荷兰弟曝近照气质大变,26岁资产惊人_蜘蛛侠_手表_罗伯特·唐尼 + +URL Source: https://www.sohu.com/a/580380519_120702487 + +Markdown Content: +2022-08-27 19:00 来源: [BEGEEL宾爵表](https://www.sohu.com/a/580380519_120702487?spm=smpc.content-abroad.content.1.1739375950559fBhgNpP) + +发布于:广东省 + +近日,大家熟悉的荷兰弟,也就演漫威超级英雄“蜘蛛侠”而走红的英国男星汤姆·赫兰德(Tom Holland),最近在没有任何预警的情况下宣布自己暂停使用社交媒体,原因网络暴力已经严重影响到他的心理健康了。虽然自出演蜘蛛侠以来,对荷兰弟的骂声就没停过,但不可否认他确实是一位才貌双全的好演员,同时也是一位拥有高雅品味的地道英伦绅士,从他近年名表收藏的趋势也能略知一二。 + +![Image 37](https://p5.itc.cn/q_70/images03/20220827/86aca867047b4119ba96a59e33d2d387.jpeg) + +2016年,《美国队长3:内战》上映,汤姆·赫兰德扮演的“史上最嫩”蜘蛛侠也正式登场。这个美国普通学生,由于意外被一只受过放射性感染的蜘蛛咬到,并因此获得超能力,化身邻居英雄蜘蛛侠警恶惩奸。和蜘蛛侠彼得·帕克一样,当时年仅20岁的荷兰弟无论戏里戏外的穿搭都是少年感十足,走的阳光邻家大男孩路线,手上戴的最多的就是来自卡西欧的电子表,还有来自Nixon sentry的手表,千元级别甚至是百元级。 + +20岁的荷兰弟走的是邻家大男孩路线 + +![Image 38](https://p3.itc.cn/q_70/images03/20220827/aded82ecfb1d439a8fd4741b49a8eb9b.png) + +随着荷兰弟主演的《蜘蛛侠:英雄归来》上演,第三代蜘蛛侠的话痨性格和年轻活力的形象瞬间圈粉无数。荷兰弟的知名度和演艺收入都大幅度增长,他的穿衣品味也渐渐从稚嫩少年风转变成轻熟绅士风。从简单的T恤短袖搭配牛仔裤,开始向更加丰富的造型发展,其中变化最明显的就是他手腕上的表。 + +荷兰弟的衣品日......
+
+ +
+第4份搜索结果 [源自google搜索] (荷兰弟居然要休息一年,因演戏演到精神分裂…): +
https://www.sohu.com/a/683718058_544020
+
Title: 荷兰弟居然要休息一年,因演戏演到精神分裂…_Holland_Tom_工作 + +URL Source: https://www.sohu.com/a/683718058_544020 + +Markdown Content: +荷兰弟居然要休息一年,因演戏演到精神分裂…\_Holland\_Tom\_工作 +=============== + +* [](http://www.sohu.com/?spm=smpc.content-abroad.nav.1.1739375954055TcEvWsY) +* [新闻](http://news.sohu.com/?spm=smpc.content-abroad.nav.2.1739375954055TcEvWsY) +* [体育](http://sports.sohu.com/?spm=smpc.content-abroad.nav.3.1739375954055TcEvWsY) +* [汽车](http://auto.sohu.com/?spm=smpc.content-abroad.nav.4.1739375954055TcEvWsY) +* [房产](http://www.focus.cn/?spm=smpc.content-abroad.nav.5.1739375954055TcEvWsY) +* [旅游](http://travel.sohu.com/?spm=smpc.content-abroad.nav.6.1739375954055TcEvWsY) +* [教育](http://learning.sohu.com/?spm=smpc.content-abroad.nav.7.1739375954055TcEvWsY) +* [时尚](http://fashion.sohu.com/?spm=smpc.content-abroad.nav.8.1739375954055TcEvWsY) +* [科技](http://it.sohu.com/?spm=smpc.content-abroad.nav.9.1739375954055TcEvWsY) +* [财经](http://business.sohu.com/?spm=smpc.content-abroad.nav.10.17393759......
+
""" def validate_path(): diff --git a/tests/test_media.py b/tests/test_media.py index a0e7dfb7..1b89fdbe 100644 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -48,8 +48,6 @@ if __name__ == "__main__": # plugin_test(plugin='crazy_functions.下载arxiv论文翻译摘要->下载arxiv论文并翻译摘要', main_input="1812.10695") - # plugin_test(plugin='crazy_functions.联网的ChatGPT->连接网络回答问题', main_input="谁是应急食品?") - # plugin_test(plugin='crazy_functions.解析JupyterNotebook->解析ipynb文件', main_input="crazy_functions/test_samples") # plugin_test(plugin='crazy_functions.数学动画生成manim->动画生成', main_input="A ball split into 2, and then split into 4, and finally split into 8.") diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9d544ba0..6faa4579 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -48,8 +48,6 @@ if __name__ == "__main__": # plugin_test(plugin='crazy_functions.下载arxiv论文翻译摘要->下载arxiv论文并翻译摘要', main_input="1812.10695") - # plugin_test(plugin='crazy_functions.联网的ChatGPT->连接网络回答问题', main_input="谁是应急食品?") - # plugin_test(plugin='crazy_functions.解析JupyterNotebook->解析ipynb文件', main_input="crazy_functions/test_samples") # plugin_test(plugin='crazy_functions.数学动画生成manim->动画生成', main_input="A ball split into 2, and then split into 4, and finally split into 8.") diff --git a/themes/common.css b/themes/common.css index 20bf3e8a..7f9ca318 100644 --- a/themes/common.css +++ b/themes/common.css @@ -323,3 +323,12 @@ opacity: 0.8; } +.search_result { + font-size: smaller; + font-style: italic; + margin: 0px; + padding: 1em; + line-height: 1.5; + text-wrap: wrap; + opacity: 0.8; +}