diff --git a/README.md b/README.md index 2b62e68..75fd6c4 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ # AstrBot -*✨ 2024 - 希望成为一个跨平台、极易上手、稳定安全的机器人项目。✨* - [![GitHub release (latest by date)](https://img.shields.io/github/v/release/Soulter/AstrBot)](https://github.com/Soulter/AstrBot/releases/latest) wakatime python @@ -17,10 +15,9 @@ Static Badge -项目主页(开发中) | -部署文档 | +项目部署问题提交 | -插件开发(最少只需 25 行,真不难!) +插件开发(最少只需 25 行) ## 🤔您可能想了解的 diff --git a/model/command/command.py b/model/command/command.py index fecf336..a518e34 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -81,7 +81,7 @@ def check_command(self, if self.command_start_with(message, "plugin"): return True, self.plugin_oper(message, role, cached_plugins, platform) if self.command_start_with(message, "myid") or self.command_start_with(message, "!myid"): - return True, self.get_my_id(message_obj) + return True, self.get_my_id(message_obj, platform) if self.command_start_with(message, "nconf") or self.command_start_with(message, "newconf"): return True, self.get_new_conf(message, role) if self.command_start_with(message, "web"): # 网页搜索 @@ -104,8 +104,14 @@ def web_search(self, message): return True, "已关闭网页搜索", "web" return True, f"网页搜索功能当前状态: {self.global_object.web_search}", "web" - def get_my_id(self, message_obj): - return True, f"你的ID:{str(message_obj.sender.tiny_id)}", "plugin" + def get_my_id(self, message_obj, platform): + user_id = "Unknown" + if platform == PLATFORM_QQCHAN: + user_id = str(message_obj.sender.tiny_id) + elif platform == PLATFORM_GOCQ: + user_id = str(message_obj.user_id) + + return True, f"你的ID:{user_id}", "plugin" def get_new_conf(self, message, role): if role != "admin": diff --git a/model/platform/qq_gocq.py b/model/platform/qq_gocq.py index 80b5207..85eac2d 100644 --- a/model/platform/qq_gocq.py +++ b/model/platform/qq_gocq.py @@ -8,7 +8,8 @@ GroupMessage, FriendMessage, GroupMemberIncrease, - Notify + Notify, + Member ) from typing import Union import time @@ -38,12 +39,10 @@ def __init__(self, cfg: dict, message_handler: callable) -> None: try: self.nick_qq = cfg['nick_qq'] except: - self.nick_qq = ("ai","!","!") + self.nick_qq = ["ai","!","!"] nick_qq = self.nick_qq if isinstance(nick_qq, str): - nick_qq = (nick_qq,) - if isinstance(nick_qq, list): - nick_qq = tuple(nick_qq) + nick_qq = [nick_qq] self.unique_session = cfg['uniqueSessionMode'] self.pic_mode = cfg['qq_pic_mode'] @@ -61,11 +60,9 @@ def __init__(self, cfg: dict, message_handler: callable) -> None: async def _(app: CQHTTP, source: GroupMessage): if self.cc.get("gocq_react_group", True): if isinstance(source.message[0], Plain): - # await self.handle_msg(source, True) self.new_sub_thread(self.handle_msg, (source, True)) elif isinstance(source.message[0], At): if source.message[0].qq == source.self_id: - # await self.handle_msg(source, True) self.new_sub_thread(self.handle_msg, (source, True)) else: return @@ -74,7 +71,6 @@ async def _(app: CQHTTP, source: GroupMessage): async def _(app: CQHTTP, source: FriendMessage): if self.cc.get("gocq_react_friend", True): if isinstance(source.message[0], Plain): - # await self.handle_msg(source, False) self.new_sub_thread(self.handle_msg, (source, False)) else: return @@ -113,22 +109,25 @@ def run(self): async def handle_msg(self, message: Union[GroupMessage, FriendMessage, GuildMessage, Notify], is_group: bool): # 判断是否响应消息 resp = False - for i in message.message: - if isinstance(i, At): - if message.type == "GuildMessage": - if i.qq == message.user_id or i.qq == message.self_tiny_id: - resp = True - if message.type == "FriendMessage": - if i.qq == message.self_id: - resp = True - if message.type == "GroupMessage": - if i.qq == message.self_id: - resp = True - elif isinstance(i, Plain): - for nick in self.nick_qq: - if nick != '' and i.text.strip().startswith(nick): - resp = True - break + if not is_group: + resp = True + else: + for i in message.message: + if isinstance(i, At): + if message.type == "GuildMessage": + if i.qq == message.user_id or i.qq == message.self_tiny_id: + resp = True + if message.type == "FriendMessage": + if i.qq == message.self_id: + resp = True + if message.type == "GroupMessage": + if i.qq == message.self_id: + resp = True + elif isinstance(i, Plain): + for nick in self.nick_qq: + if nick != '' and i.text.strip().startswith(nick): + resp = True + break if not resp: return diff --git a/model/provider/openai_official.py b/model/provider/openai_official.py index aedf0bb..2a1ade6 100644 --- a/model/provider/openai_official.py +++ b/model/provider/openai_official.py @@ -31,7 +31,7 @@ def __init__(self, cfg): if cfg['key'] != '' and cfg['key'] != None: self.key_list = cfg['key'] else: - input("[System] 请先去完善ChatGPT的Key。详情请前往https://beta.openai.com/account/api-keys") + input("[System] 请先填写 Key。详情请前往 https://beta.openai.com/account/api-keys 或使用中转 Key 方案。") if len(self.key_list) == 0: raise Exception("您打开了 OpenAI 模型服务,但是未填写 key。请前往填写。") diff --git a/util/function_calling/gplugin.py b/util/function_calling/gplugin.py index d6a4f4f..8cba715 100644 --- a/util/function_calling/gplugin.py +++ b/util/function_calling/gplugin.py @@ -53,6 +53,7 @@ def google_web_search(keyword) -> str: for i in ls: desc = i.description try: + gu.log(f"搜索网页: {i.url}", tag="网页搜索", level=gu.LEVEL_INFO) desc = fetch_website_content(i.url) except BaseException as e: print(f"(google) fetch_website_content err: {str(e)}") @@ -74,51 +75,54 @@ def web_keyword_search_via_bing(keyword) -> str: } url = "https://www.bing.com/search?q="+keyword _cnt = 0 - _detail_store = [] + # _detail_store = [] while _cnt < 5: try: response = requests.get(url, headers=headers) response.encoding = "utf-8" gu.log(f"bing response: {response.text}", tag="bing", level=gu.LEVEL_DEBUG, max_len=9999) soup = BeautifulSoup(response.text, "html.parser") - res = [] + res = "" + result_cnt = 0 ols = soup.find(id="b_results") for i in ols.find_all("li", class_="b_algo"): try: title = i.find("h2").text desc = i.find("p").text link = i.find("h2").find("a").get("href") - res.append({ - "title": title, - "desc": desc, - "link": link, - }) - if len(res) >= 5: # 限制5条 - break - if len(_detail_store) >= 3: - continue + # res.append({ + # "title": title, + # "desc": desc, + # "link": link, + # }) + try: + gu.log(f"搜索网页: {link}", tag="网页搜索", level=gu.LEVEL_INFO) + desc = fetch_website_content(link) + except BaseException as e: + print(f"(bing) fetch_website_content err: {str(e)}") + + res += f"# No.{str(result_cnt + 1)}\ntitle: {title}\nurl: {link}\ncontent: {desc}\n\n" + result_cnt += 1 + if result_cnt > 5: break - # 爬取前两条的网页内容 - if "zhihu.com" in link: - try: - _detail_store.append(special_fetch_zhihu(link)) - except BaseException as e: - print(f"zhihu parse err: {str(e)}") - else: - try: - _detail_store.append(fetch_website_content(link)) - except BaseException as e: - print(f"fetch_website_content err: {str(e)}") + # if len(_detail_store) >= 3: + # continue + # # 爬取前两条的网页内容 + # if "zhihu.com" in link: + # try: + # _detail_store.append(special_fetch_zhihu(link)) + # except BaseException as e: + # print(f"zhihu parse err: {str(e)}") + # else: + # try: + # _detail_store.append(fetch_website_content(link)) + # except BaseException as e: + # print(f"fetch_website_content err: {str(e)}") except Exception as e: print(f"bing parse err: {str(e)}") - if len(res) == 0: - break - if len(_detail_store) > 0: - ret = f"{str(res)} \n具体网页内容: {str(_detail_store)}" - else: - ret = f"{str(res)}" - return str(ret) + if result_cnt == 0: break + return res except Exception as e: gu.log(f"bing fetch err: {str(e)}") _cnt += 1 @@ -175,26 +179,6 @@ def fetch_website_content(url): } response = requests.get(url, headers=headers, timeout=3) response.encoding = "utf-8" - # soup = BeautifulSoup(response.text, "html.parser") - # # 如果有container / content / main等的话,就只取这些部分 - # has = False - # beleive_ls = ["container", "content", "main"] - # res = "" - # for cls in beleive_ls: - # for i in soup.find_all(class_=cls): - # has = True - # res += i.text - # if not has: - # res = soup.text - # res = res.replace("\n", "").replace(" ", " ").replace("\r", "").replace("\t", "") - # if not has: - # res = res[300:1100] - # else: - # res = res[100:800] - # # with open(f"temp_{time.time()}.html", "w", encoding="utf-8") as f: - # # f.write(res) - # gu.log(f"fetch_website_content: end", tag="fetch_website_content", level=gu.LEVEL_DEBUG) - # return res doc = Document(response.content) # print('title:', doc.title()) ret = doc.summary(html_partial=True) @@ -213,7 +197,7 @@ def web_search(question, provider: Provider, session_id, official_fc=False): "description": "google search query (分词,尽量保留所有信息)" }], "通过搜索引擎搜索。如果问题需要在网页上搜索(如天气、新闻或任何需要通过网页获取信息的问题),则调用此函数;如果没有,不要调用此函数。", - google_web_search + web_keyword_search_via_bing ) new_func_call.add_func("fetch_website_content", [{ "type": "string", @@ -259,13 +243,20 @@ def web_search(question, provider: Provider, session_id, official_fc=False): if has_func: provider.forget(session_id) - question3 = f"""请你用活泼的语气回答`{question}`问题。\n以下是相关材料,请直接拿此材料针对问题进行总结回答。在文章末尾加上各参考链接,如`[1] <url>`;不要提到任何函数调用的信息;在总结的末尾加上1或2个相关的emoji。```\n{function_invoked_ret}\n```\n""" + question3 = f""" +以下是相关材料,你的任务是: +1. 根据材料对问题`{question}`做切题的总结回答; +2. 发表你对这个问题的看法. +你的总结末尾应当有对材料的引用, 如果有链接, 请在末尾附上引用网页链接。引用格式严格按照 `\n[1] title url \n`。 +不要提到任何函数调用的信息。以下是相关材料: +""" + gu.log(f"web_search: {question3}", tag="web_search", level=gu.LEVEL_DEBUG, max_len=99999) _c = 0 while _c < 3: try: print('text chat') - final_ret = provider.text_chat(question3) + final_ret = provider.text_chat(question3 + "```" + function_invoked_ret + "```", session_id) return final_ret except Exception as e: print(e) @@ -275,5 +266,4 @@ def web_search(question, provider: Provider, session_id, official_fc=False): provider.forget(session_id) function_invoked_ret = function_invoked_ret[:int(len(function_invoked_ret) / 2)] time.sleep(3) - question3 = f"""请回答`{question}`问题。\n以下是相关材料,请直接拿此材料针对问题进行回答,再给参考链接, 参考链接首末有空格。```\n{function_invoked_ret}\n```\n""" return function_invoked_ret