当前位置: 首页 > news >正文

240912-通过Ollama实现网站知识总结

A. 最终效果

在这里插入图片描述

B. 准备工作

  • 报错: USER_AGENT environment variable not set, consider setting it to identify your requests.-CSDN博客
    在这里插入图片描述
    在这里插入图片描述

C. 完整代码

# https://coreyclip.github.io/Ollama-Web-Summaries/import os
os.environ['USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'import argparse
import os
import datetime
from langchain_community.llms import Ollama
from langchain_community.document_loaders import WebBaseLoader
from langchain.chains.summarize import load_summarize_chain
from dotenv import load_dotenv# set OLLAMA_MODEL env var or create a .env file with OLLAMA_MODEL set to the model of your choice
load_dotenv()ollama_model = os.getenv("OLLAMA_MODEL","qwen2:7b")def save_to_markdown(title, content, url, filename):timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")word_count = len(content["output_text"].split())with open(filename, "w", encoding="utf-8") as f:f.write(f"# {title}\n\n")f.write(f"**Source URL:** {url}\n\n")f.write(f"**Timestamp:** {timestamp}\n\n")f.write(f"**Word Count:** {word_count}\n\n")f.write(f"---\n\n")f.write(content["output_text"])def main():# setting up commandline argumentsparser = argparse.ArgumentParser(description="Summarize a webpage via a llm model available via ollama")parser.add_argument("website", type=str, help="The URL of the website to summarize.")parser.add_argument("-o", "--output", type=str, help="Output markdown file to save the summary. If not provided, output will be printed to stdout.")args = parser.parse_args()# load into langchainloader = WebBaseLoader(args.website)docs = loader.load()# invoke langchain llm = Ollama(model=ollama_model)chain = load_summarize_chain(llm, chain_type="stuff")result = chain.invoke(docs)# Extract webpage title and other metadatatitle = "Webpage Summary"  # Default title if none is foundif docs and docs[0].metadata and "title" in docs[0].metadata:title = docs[0].metadata["title"]if args.output:save_to_markdown(title, result, args.website,args.output)else:word_count = len(result["output_text"].split())timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")print(f"# {title}\n")print(f"**Source URL:** {args.website}\n")print(f"**Timestamp:** {timestamp}\n")print(f"**Word Count:** {word_count}\n")print(f"---\n")print(result)if __name__ == "__main__":main()

D. 参考文献

  • Summarization with LangChain. Stuff — Map_reduce — Refine | by Abonia Sojasingarayar | Medium
  • python-agents-tutorial/2_local_agent.py at main · run-llama/python-agents-tutorial
  • Summarizing Web Pages with Ollama – The Blog of Corey Kiyoshi Clippinger – Think pieces, tutorials, and other content about Data Science, Web Development, and the Big Wide World
  • Integrating LangGraph with Ollama for Advanced LLM Applications | by Aleksandr Lifanov | Medium
  • 华中科技大学_百度百科
  • Langchain-实战篇-搭建本地问答机器人-01_webbaseloader-CSDN博客
http://www.lryc.cn/news/435737.html

相关文章:

  • Debian 包管理工具apt使用
  • 如何模拟一个小程序项目打包的流程
  • 设计模式七大原则详解
  • Navicat On-Prem Server 2.0 | MySQL与MariaDB基础管理功能正式上云
  • 汽车车门的美观与功能:矫平工艺的精细修复
  • 【Python】05.Python 中的列表与元组
  • 【Go】十五、分布式系统、Consul服务注册发现、Nacos配置中心搭建
  • [Linux#48][网络] 令牌环网 | IPv4 | socket 套接字 | TCP | UDP | 网络字节序列
  • Mac OS14外接显示器字体过小和放大字体模糊问题的简单解决
  • Python-pptx:如何在幻灯片中轻松插入与填充表格
  • ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘ (10061)
  • MySQL优化策略(大数据量)
  • 在Excel里制作简单游戏界面
  • 火语言RPA流程组件介绍--鼠标拖拽元素
  • 计算机三级网络技术总结 第十一章网络管理技术
  • 「豆包 Marscode 体验官」AI 加持的云端 IDE——三种方法高效开发前后端聊天交互功能
  • 基于Linux文件编程实现处理Excel表格的数据
  • make 程序规定的 makefile 文件的书写语法(2)
  • 容器化安装jenkins稳定版长期维护版本LTS
  • 如何让人工智能训练更快
  • linux/ubuntu国内镜像安装gitleaks敏感信息扫描工具教程及避坑点
  • JavaScript高级程序设计基础(二)
  • 使用Spring Boot开发自习室预定系统
  • 最近读书总结
  • python列表判断是否为空的三种方式
  • 二十三种模式之原型模式(类比制作陶器更好理解一些)
  • 9.9日记录
  • 鸿蒙交互事件开发04——手势事件
  • 研1日记9
  • HAL库学习目录查询表