LangChain 27 BabyAGI编写旧金山的天气预报

2023-12-17 17:52

本文主要是介绍LangChain 27 BabyAGI编写旧金山的天气预报,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

LangChain系列文章

  1. LangChain 实现给动物取名字,
  2. LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字
  3. LangChain 3使用Agent访问Wikipedia和llm-math计算狗的平均年龄
  4. LangChain 4用向量数据库Faiss存储,读取YouTube的视频文本搜索Indexes for information retrieve
  5. LangChain 5易速鲜花内部问答系统
  6. LangChain 6根据图片生成推广文案HuggingFace中的image-caption模型
  7. LangChain 7 文本模型TextLangChain和聊天模型ChatLangChain
  8. LangChain 8 模型Model I/O:输入提示、调用模型、解析输出
  9. LangChain 9 模型Model I/O 聊天提示词ChatPromptTemplate, 少量样本提示词FewShotPrompt
  10. LangChain 10思维链Chain of Thought一步一步的思考 think step by step
  11. LangChain 11实现思维树Implementing the Tree of Thoughts in LangChain’s Chain
  12. LangChain 12调用模型HuggingFace中的Llama2和Google Flan t5
  13. LangChain 13输出解析Output Parsers 自动修复解析器
  14. LangChain 14 SequencialChain链接不同的组件
  15. LangChain 15根据问题自动路由Router Chain确定用户的意图
  16. LangChain 16 通过Memory记住历史对话的内容
  17. LangChain 17 LangSmith调试、测试、评估和监视基于任何LLM框架构建的链和智能代理
  18. LangChain 18 LangSmith监控评估Agent并创建对应的数据库
  19. LangChain 19 Agents Reason+Action自定义agent处理OpenAI的计算缺陷
  20. LangChain 20 Agents调用google搜索API搜索市场价格 Reason Action:在语言模型中协同推理和行动
  21. LangChain 21 Agents自问自答与搜索 Self-ask with search
  22. LangChain 22 LangServe用于一键部署LangChain应用程序
  23. LangChain 23 Agents中的Tools用于增强和扩展智能代理agent的功能
  24. LangChain 24 对本地文档的搜索RAG检索增强生成Retrieval-augmented generation
  25. LangChain 25: SQL Agent通过自然语言查询数据库sqlite
  26. LangChain 26: 回调函数callbacks打印prompt verbose调用
  27. LangChain 27 AI Agents角色扮演多轮对话解决问题CAMEL

1. Agents

Agents的核心思想是使用语言模型选择一系列需要采取的行动。在链式结构中,一系列的行动是硬编码的(在代码中)。在Agents中,语言模型被用作推理引擎,以确定需要采取哪些行动,以及采取这些行动的顺序。

这是负责决定下一步该采取什么行动的链条。这是由语言模型和提示驱动的。这个链条的输入包括:

  1. Tools工具:可用工具的描述
  2. User input用户输入:高层目标
  3. Intermediate steps中间步骤:为了实现用户输入而先前执行的任何(action动作,tool output工具输出)对

输出是下一步要采取的行动或发送给用户的最终响应(AgentActions或AgentFinish)。一个行动指定一个工具和该工具的输入。

不同的代理有不同的推理提示风格,不同的编码输入方式以及不同的解析输出方式。有关内置代理的完整列表,请参见代理类型。您还可以轻松构建自定义代理,我们将在下面的入门部分中展示如何做到这一点。

2. BabyAGI

BabyAGI这个 Python 脚本是一个使用人工智能技术的任务管理系统的示例。该系统使用 OpenAI 和矢量数据库,比如 Chroma 或 Weaviate,来创建、排序和执行任务。这个系统的主要思想是根据先前任务的结果和预定义的目标来创建任务。然后,脚本利用 OpenAI 的自然语言处理(NLP)能力根据目标创建新任务,并利用 Chroma/Weaviate 来存储和检索任务结果以获取上下文信息。这是原始任务驱动自主代理的简化版本。

2.1 它是如何工作的

该脚本通过运行一个无限循环来完成以下步骤:

  1. 从任务列表中获取第一个任务。
  2. 将任务发送给执行代理,该代理使用OpenAI的API根据上下文完成任务。
  3. 丰富结果并将其存储在Chroma/Weaviate中。
  4. 根据目标和先前任务的结果创建新任务并重新设置任务列表的优先级。
    在这里插入图片描述
    execution_agent()函数是使用OpenAI API的地方。它接受两个参数:目标和任务。然后它向OpenAI的API发送提示,返回任务的结果。提示包括AI系统任务的描述、目标和任务本身。然后将结果作为字符串返回。

task_creation_agent()函数是使用OpenAI的API基于目标和先前任务的结果创建新任务的地方。该函数接受四个参数:目标、先前任务的结果、任务描述和当前任务列表。然后向OpenAI的API发送提示,返回一组新任务作为字符串列表。然后该函数将新任务作为字典列表返回,其中每个字典包含任务的名称。

prioritization_agent()函数是使用OpenAI的API重新设置任务列表的地方。该函数接受一个参数,即当前任务的ID。它向OpenAI的API发送提示,返回重新设置优先级的任务列表作为编号列表。

最后,脚本使用Chroma/Weaviate来存储和检索任务结果以获取上下文。脚本根据TABLE_NAME变量中指定的表名创建一个Chroma/Weaviate集合。然后使用Chroma/Weaviate将任务的结果以及任务名称和任何附加元数据存储在集合中。

3. 代码实现 BabyAGI编写旧金山的天气预报

本指南将帮助您了解创建自己的递归代理的组件。

尽管 BabyAGI 使用特定的向量库/模型提供程序(Pinecone、OpenAI),但使用 LangChain 实现它的好处之一是您可以轻松地将它们换成不同的选项。在这个实现中,我们使用FAISS向量存储(因为它在本地运行并且是免费的)。

3.1 安装和导入所需的模块

from typing import Optionalfrom langchain.embeddings import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain_experimental.autonomous_agents import BabyAGI

3.2 连接到 Vector Store

from langchain.docstore import InMemoryDocstore
from langchain.vectorstores import FAISS
# Define your embedding model
embeddings_model = OpenAIEmbeddings()
# Initialize the vectorstore as empty
import faissembedding_size = 1536
index = faiss.IndexFlatL2(embedding_size)
vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})

3.3 现在是时候创建 BabyAGI 控制器并观察它尝试实现您的目标了。

OBJECTIVE = "Write a weather report for SF today"
llm = OpenAI(temperature=0)
# Logging of LLMChains
verbose = False
# If None, will keep on going forever
max_iterations: Optional[int] = 3
baby_agi = BabyAGI.from_llm(llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations
)
baby_agi({"objective": OBJECTIVE})

3.4 输出过程

*****TASK LIST*****1: Make a todo list*****NEXT TASK*****1: Make a todo list*****TASK RESULT*****1. Check the weather forecast for San Francisco today
2. Make note of the temperature, humidity, wind speed, and other relevant weather conditions
3. Write a weather report summarizing the forecast
4. Check for any weather alerts or warnings
5. Share the report with the relevant stakeholders*****TASK LIST*****2: Check the current temperature in San Francisco
3: Check the current humidity in San Francisco
4: Check the current wind speed in San Francisco
5: Check for any weather alerts or warnings in San Francisco
6: Check the forecast for the next 24 hours in San Francisco
7: Check the forecast for the next 48 hours in San Francisco
8: Check the forecast for the next 72 hours in San Francisco
9: Check the forecast for the next week in San Francisco
10: Check the forecast for the next month in San Francisco
11: Check the forecast for the next 3 months in San Francisco
1: Write a weather report for SF today*****NEXT TASK*****2: Check the current temperature in San Francisco*****TASK RESULT*****I will check the current temperature in San Francisco. I will use an online weather service to get the most up-to-date information.*****TASK LIST*****3: Check the current UV index in San Francisco.
4: Check the current air quality in San Francisco.
5: Check the current precipitation levels in San Francisco.
6: Check the current cloud cover in San Francisco.
7: Check the current barometric pressure in San Francisco.
8: Check the current dew point in San Francisco.
9: Check the current wind direction in San Francisco.
10: Check the current humidity levels in San Francisco.
1: Check the current temperature in San Francisco to the average temperature for this time of year.
2: Check the current visibility in San Francisco.
11: Write a weather report for SF today.*****NEXT TASK*****3: Check the current UV index in San Francisco.*****TASK RESULT*****The current UV index in San Francisco is moderate. The UV index is expected to remain at moderate levels throughout the day. It is recommended to wear sunscreen and protective clothing when outdoors.*****TASK ENDING*****

代码

https://github.com/zgpeace/pets-name-langchain/tree/develop

参考

  • https://github.com/yoheinakajima/babyagi/tree/main
  • https://github.com/langchain-ai/langchain/blob/master/cookbook/baby_agi.ipynb

这篇关于LangChain 27 BabyAGI编写旧金山的天气预报的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/505290

相关文章

如何编写Linux PCIe设备驱动器 之二

如何编写Linux PCIe设备驱动器 之二 功能(capability)集功能(capability)APIs通过pci_bus_read_config完成功能存取功能APIs参数pos常量值PCI功能结构 PCI功能IDMSI功能电源功率管理功能 功能(capability)集 功能(capability)APIs int pcie_capability_read_wo

树莓派5_opencv笔记27:Opencv录制视频(无声音)

今日继续学习树莓派5 8G:(Raspberry Pi,简称RPi或RasPi)  本人所用树莓派5 装载的系统与版本如下:  版本可用命令 (lsb_release -a) 查询: Opencv 与 python 版本如下: 今天就水一篇文章,用树莓派摄像头,Opencv录制一段视频保存在指定目录... 文章提供测试代码讲解,整体代码贴出、测试效果图 目录 阶段一:录制一段

Wondows dos下怎么编写bat批处理文件

最近搞php,在运行时,以Nginx+php-cgi.exe方式运行Wordpress项目 打开dos,先cd到php-cgi.exe文件当前目录下执行启动命令:php-cgi.exe -b 127.0.0.1:9001再打开一个dos,再cd到nginx.exe文件当前目录下执行启动命令:start nginx 大概过程要经过这些步骤,觉得很麻烦,就学下怎么编写一个bat文件,以双击运行代替

用Python编写倒计时程序:详细教程

目录 引言 环境准备 基本概念 代码实现 步骤一:导入必要的库 步骤二:获取用户输入 步骤三:实现倒计时逻辑 步骤四:整合代码 运行程序 高级功能 扩展功能示例:支持分钟和小时输入 扩展功能示例:图形用户界面 (GUI) 总结 引言 倒计时程序是一个非常常见的小工具,广泛用于各种应用场景中,例如考试时间提醒、烹饪计时器、会议倒计时等。Python 作为一种

【Spring boot】编写代码及测试用例入门之 Hello Spring boot _踩坑记

先贴下目录: 这是我从 start.spring.io 里下载的依赖Web的模板 // DemoApplication.javapackage com.abloume.springboot.blog.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autocon

windows下使用vscode编写运行以及调试C/C++

vscode支持类似于vs的断点调试c/c++,也可以直接编译&运行c/c++ 先是编译运行 c/c++的方法                              微软官方起初设定的科学做法(这也是现在的科学做法)是通过在vscode集成控制台写命令行的方式来实现编译运行程序的,但也可以通过code runner插件来简化步骤,实现一键编译执行 但无论是什么方法,因为vscod

Python编写简单登录系统的完整指南

在现代应用中,用户认证和登录系统是一个非常重要的功能。通过登录系统,应用能够识别用户的身份,并为其提供相应的权限和服务。本文将介绍如何使用Python编写一个简单的登录系统,包括用户注册、登录验证、密码加密等功能。通过这一教程,将学习如何构建一个基本的用户登录系统,并理解其中的关键技术。 这里插播一条粉丝福利,如果你在学习Python或者有计划学习Python,想在未来人工智能领域吃上一口饭的,

springboot项目编写发送异常日志到企微工具包

1.创建基础Bean public final class ThreadFactory implements java.util.concurrent.ThreadFactory {private static final AtomicInteger poolNumber = new AtomicInteger(1);private final ThreadGroup group;priva

python网络爬虫(五)——爬取天气预报

1.注册高德天气key   点击高德天气,然后按照开发者文档完成key注册;作为爬虫练习项目之一。从高德地图json数据接口获取天气,可以获取某省的所有城市天气,高德地图的这个接口还能获取县城的天气。其天气查询API服务地址为https://restapi.amap.com/v3/weather/weatherInfo?parameters,若要获取某城市的天气推荐 2.安装MongoDB

LLM大模型教程:langchain 教程

软件安装 pip install pymupdfpip install langchainpip install langchain-cliconda install -c pytorch -c nvidia faiss-gpu=1.7.4 mkl=2021 blas=1.0=mkl 由于langchain不支持qwen模型,我们需要自定义模型 from typing import A