diff --git a/my_information_collector.py b/my_information_collector.py new file mode 100644 index 0000000000000000000000000000000000000000..f1924a2fad74c9c2e92d9293469e66aa9f2e0deb --- /dev/null +++ b/my_information_collector.py @@ -0,0 +1,31 @@ +import lazyllm +from my_web_scraper import MyWebScraper + +# --- 主程序 --- +if __name__ == '__main__': + # 1. 实例化我们自定义的网页抓取工具 + # 这个工具提供了实际的网页内容获取能力 + web_scraper_tool = MyWebScraper() + print("--- 自定义工具 [MyWebScraper] 加载成功 ---") + + # 2. 创建聊天 Agent 核心 + # 直接使用您提供的千问 API Key 和模型参数 + # 通过 `tools` 参数,我们将网页抓取的能力赋予了 Agent + chat_agent = lazyllm.OnlineChatModule( + source="qwen", + model="qwen-turbo", + api_key="sk-8d96c43a26cc4049b72876c345b8da85", # 您的 API Key + tools=[web_scraper_tool] + ) + print("--- 聊天 Agent [OnlineChatModule] 创建成功,已配置千问模型并装备工具 ---") + + # 3. 将 Agent 包装进 WebModule 以创建 Gradio 界面 + print("--- 正在准备启动 Gradio Web 界面... ---") + web_ui = lazyllm.WebModule(chat_agent, port=range(23466, 23470)) + + # 4. 启动 Web 服务并等待 + # 这会阻塞命令行,直到您手动停止程序或关闭Gradio窗口 + print("--- 服务启动中,请在浏览器中打开给出的地址 ---") + web_ui.start().wait() + + print("--- Web 界面已关闭 ---") \ No newline at end of file diff --git a/my_web_scraper.py b/my_web_scraper.py new file mode 100644 index 0000000000000000000000000000000000000000..b69bde850c359050d6b3456e948b298c136f8a68 --- /dev/null +++ b/my_web_scraper.py @@ -0,0 +1,51 @@ +import requests +from bs4 import BeautifulSoup + +class MyWebScraper: + """ + 一个简单的网页抓取工具,用于获取指定URL的文本内容。 + """ + def __init__(self): + pass + + def __call__(self, url: str) -> str: + """ + 使得这个类的实例可以像函数一样被调用。 + Agent会调用这个方法来执行工具。 + + Args: + url (str): 需要抓取的网页URL。 + + Returns: + str: 网页的纯文本内容,或者一条错误信息。 + """ + try: + headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' + } + response = requests.get(url, headers=headers, timeout=15) + response.raise_for_status() # 如果请求失败(如404, 500等),则抛出异常 + + # 使用BeautifulSoup解析HTML + soup = BeautifulSoup(response.text, 'html.parser') + + # 提取所有文本内容 + # 你可以根据需要进一步处理,比如只提取

标签等 + text = soup.get_text(separator='\\n', strip=True) + + # 为了防止内容过长,可以截取一部分 + max_length = 5000 + if len(text) > max_length: + text = text[:max_length] + "... (内容过长,已截断)" + + return text + except requests.exceptions.RequestException as e: + return f"抓取网页失败: {e}" + except Exception as e: + return f"处理网页时发生未知错误: {e}" + +# 你可以在这里添加一个简单的测试 +if __name__ == '__main__': + scraper = MyWebScraper() + content = scraper("https://www.lazyllm.com/") + print(content) \ No newline at end of file diff --git "a/\345\207\272\346\265\267\351\200\232AI \350\265\204\350\256\257\346\224\266\351\233\206 Agent \346\212\200\346\234\257\346\226\207\346\241\243.docx" "b/\345\207\272\346\265\267\351\200\232AI \350\265\204\350\256\257\346\224\266\351\233\206 Agent \346\212\200\346\234\257\346\226\207\346\241\243.docx" new file mode 100644 index 0000000000000000000000000000000000000000..7e38c89c3f92a558c63fe3dfa256ac2fcb99a7ac Binary files /dev/null and "b/\345\207\272\346\265\267\351\200\232AI \350\265\204\350\256\257\346\224\266\351\233\206 Agent \346\212\200\346\234\257\346\226\207\346\241\243.docx" differ diff --git "a/\346\265\213\350\257\225\350\257\264\346\230\216" "b/\346\265\213\350\257\225\350\257\264\346\230\216" new file mode 100644 index 0000000000000000000000000000000000000000..d23843b31b69d5aab5c3c7f4560c234e60435f21 --- /dev/null +++ "b/\346\265\213\350\257\225\350\257\264\346\230\216" @@ -0,0 +1 @@ +对应API需更换阿里云百炼大模型的API即可进行测试 \ No newline at end of file