用例(User Case)
Baby Agi

LangChain

BabyAGI用户指南#

本手册演示了如何通过Yohei Nakajima (opens in a new tab)提供的BabyAGI (opens in a new tab)实现AI代理,该代理可以基于给定的目标生成并模拟执行任务。

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

虽然BabyAGI使用特定的矢量存储库/模型提供程序(Pinecone, OpenAI),,但使用LangChain实现的好处之一是可以轻松地将其与不同的选项交换。 在此实现中,我们使用FAISS矢量存储库(因为它在本地运行并且免费)。

安装和导入所需模块#

import os
 
from collections import deque
 
from typing import Dict, List, Optional, Any
 
 
 
from langchain import LLMChain, OpenAI, PromptTemplate
 
from langchain.embeddings import OpenAIEmbeddings
 
from langchain.llms import BaseLLM
 
from langchain.vectorstores.base import VectorStore
 
from pydantic import BaseModel, Field
 
from langchain.chains.base import Chain
 
from langchain.experimental import BabyAGI
 
 
 

连接到矢量存储库#

根据您使用的矢量存储库,此步骤的操作可能有所不同。

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

运行BabyAGI#

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

注意:请确保Python文件名与模型名称相同。如:model_name = 'mymodel'

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})
 
 
 
 
 
*****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*****
 
 
 
 
 
{'objective': 'Write a weather report for SF today'}