BabyAGI: Error storing results in vdb
Labels: name: auto:bug

### System Info

LangChain ==0.0.228, watchdog==3.0.0, streamlit==1.24.0, databutton==0.34.0, ipykernel==6.23.3

### Who can help?

@hwchase17

### Information

- [X] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [X] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

Steps to reproduce the error:

1. Tried to use from langchain.experimental import BabyAGI with a FAISS db, got the error: ValueError: Tried to add ids that already exist: {'result_1'}
2. Tried the code directly from the Langchain Docs: https://python.langchain.com/docs/use_cases/agents/baby_agi, I got the same error.

Code:
import os
from langchain.chat_models import AzureChatOpenAI
from langchain.embeddings.cohere import CohereEmbeddings
import faiss
from langchain.vectorstores import FAISS
from langchain.docstore import InMemoryDocstore
from langchain import OpenAI
from langchain.experimental import BabyAGI

BASE_URL = "https://openaielle.openai.azure.com/"
API_KEY = db.secrets.get("AZURE_OPENAI_KEY")
DEPLOYMENT_NAME = "GPT35turbo"
llm = AzureChatOpenAI(
    openai_api_base=BASE_URL,
    openai_api_version="2023-03-15-preview",
    deployment_name=DEPLOYMENT_NAME,
    openai_api_key=API_KEY,
    openai_api_type="azure",
    streaming=True,
    verbose=True,
    temperature=0,
    max_tokens=1500,
    top_p=0.95)


embeddings_model = CohereEmbeddings(model = "embed-english-v2.0")
index = faiss.IndexFlatL2(4096)

vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})

# set the goal
goal = "Plan a trip to the Grand Canyon"

# create thebabyagi agent
# If max_iterations is None, the agent may go on forever if stuck in loops
baby_agi = BabyAGI.from_llm(
    llm=llm,
    vectorstore=vectorstore,
    verbose=False,
    max_iterations=3
)
response = baby_agi({"objective": goal})
print(response)


Error:
ValueError: Tried to add ids that already exist: {'result_1'}

Traceback:
File "/user-venvs-build/14abfc95cf32/.venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "/app/run/multipage/pages/8_Exp_Baby_AGI.py", line 61, in <module>
    response = baby_agi({"objective": goal})
File "/user-venvs-build/14abfc95cf32/.venv/lib/python3.10/site-packages/langchain/chains/base.py", line 243, in __call__
    raise e
File "/user-venvs-build/14abfc95cf32/.venv/lib/python3.10/site-packages/langchain/chains/base.py", line 237, in __call__
    self._call(inputs, run_manager=run_manager)
File "/user-venvs-build/14abfc95cf32/.venv/lib/python3.10/site-packages/langchain/experimental/autonomous_agents/baby_agi/baby_agi.py", line 142, in _call
    self.vectorstore.add_texts(
File "/user-venvs-build/14abfc95cf32/.venv/lib/python3.10/site-packages/langchain/vectorstores/faiss.py", line 150, in add_texts
    return self.__add(texts, embeddings, metadatas=metadatas, ids=ids, **kwargs)
File "/user-venvs-build/14abfc95cf32/.venv/lib/python3.10/site-packages/langchain/vectorstores/faiss.py", line 121, in __add
    self.docstore.add({_id: doc for _, _id, doc in full_info})
File "/user-venvs-build/14abfc95cf32/.venv/lib/python3.10/site-packages/langchain/docstore/in_memory.py", line 19, in add
    raise ValueError(f"Tried to add ids that already exist: {overlapping}")

      

### Expected behavior

I would expect the agent to run and generate the desired output instead of the error: ValueError: Tried to add ids that already exist: {'result_1'}

I seems that the error is happening in this class: BabyAGI > _call > # Step 3: Store the result in Pinecone

I was able to fix this by assigning a random number to each iteration of result_id, here is the fix, however this is not working in the experimental BabyAGI instance.

Fix:
import random
                # Step 3: Store the result in Pinecone
                result_id = f"result_{task['task_id']}_{random.randint(0, 1000)}"
                self.vectorstore.add_texts(
                    texts=[result],
                    metadatas=[{"task": task["task_name"]}],
                    ids=[result_id],
                )

Thank you :)