crewAI
crewAI copied to clipboard
IndexError: list index out of range with CrewAI
Hi guys, following is my code for crewai to read from a local document using RAG.
gemini = ChatGoogleGenerativeAI(model="gemini-pro",
verbose=True,
temperature=0.2,
google_api_key="",
max_output_tokens = 2048)
pdf_folder_path = "./pdf"
print(os.listdir(pdf_folder_path))
loaders = [UnstructuredPDFLoader(os.path.join(pdf_folder_path,fn)) for fn in os.listdir(pdf_folder_path)]
print(loaders)
all_documents = []
for loader in loaders:
print("Loading raw documents ..." + loader.file_path)
raw_documents = loader.load()
print("splitting text")
text_splitter = CharacterTextSplitter(
separator = "\n",
chunk_size = 1000,
chunk_overlap = 150,
length_function = len,
)
documents = text_splitter.split_documents(raw_documents)
all_documents.extend(documents)
print("Creating vectorstore...")
#vectorstore = FAISS.from_documents(all_documents,embeddings).as_retriever()
client = chromadb.Client()
if client.list_collections():
consent_collection = client.create_collection("consent_collection")
else:
print("Collection already exists")
vectordb = Chroma.from_documents(
documents = all_documents,
embedding = OpenAIEmbeddings(openai_api_key=""),
persist_directory = "C:\\Users\\CS01\\Documents\\crewAI-examples-main\\crewAI-examples-main\\stock_analysis\\tools\\chroma_store\\"
)
vectordb.persist()
@tool("Get Local Documents")
def search_trend(query):
"""
Useful to search for a given query from the local documents.
The input to this tool should be a query to ask.
"""
ask = query
print("ASK....")
print(ask)
answer = __embedding_search(ask)
return answer
def __embedding_search(ask):
retriever = vectordb.similarity_search(ask)
print("Retrieving info from vector database")
print(retriever)
return retriever
trend_list = ['Data and Machine Learning', 'Generative AI', 'Blockchain Technology']
func_list = []
for trend in trend_list:
func_1 = Task(description=dedent(f"""
Go step by step.
You should ALWAYS use the Get Local Documents Tool to search for CRIMES information on {trend} and only use the information retrieved from the tool and write an in-depth elaboration of search.
You DON'T NEED TO have access to the internet to get local documents.
Your final answer MUST be at least 500 words.
"""),
agent = writer_agent,
tools=[search_trend]
)
func_list.append(func_1)
# Form the crew with a hierarchical process
project_crew = Crew(
tasks=func_list, # Tasks that that manager will figure out how to complete
agents=[writer_agent]
)
result = project_crew.kickoff()
print(result)
I encountered the following Error:
IndexError Traceback (most recent call last)
Cell In[13], [line 1](vscode-notebook-cell:?execution_count=13&line=1)
----> [1](vscode-notebook-cell:?execution_count=13&line=1) result = project_crew.kickoff()
[2](vscode-notebook-cell:?execution_count=13&line=2) print(result)
File [c:\Users\CS01\miniconda3\lib\site-packages\crewai\crew.py:192](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:192), in Crew.kickoff(self)
[189](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:189) agent.create_agent_executor()
[191](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:191) if self.process == Process.sequential:
--> [192](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:192) return self._run_sequential_process()
[193](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:193) if self.process == Process.hierarchical:
[194](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:194) return self._run_hierarchical_process()
File [c:\Users\CS01\miniconda3\lib\site-packages\crewai\crew.py:214](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:214), in Crew._run_sequential_process(self)
[211](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:211) self._logger.log("debug", f"Working Agent: {role}")
[212](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:212) self._logger.log("info", f"Starting Task: {task.description}")
--> [214](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:214) output = task.execute(context=task_output)
[215](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:215) if not task.async_execution:
[216](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/crew.py:216) task_output = output
File [c:\Users\CS01\miniconda3\lib\site-packages\crewai\task.py:104](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/task.py:104), in Task.execute(self, agent, context, tools)
[102](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/task.py:102) self.thread.start()
[103](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/task.py:103) else:
--> [104](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/task.py:104) result = self._execute(
[105](file:///C:/Users/CS01/miniconda3/lib/site-packages/crewai/task.py:105) task=self,
...
File [c:\Users\CS01\miniconda3\lib\site-packages\proto\marshal\collections\repeated.py:125](file:///C:/Users/CS01/miniconda3/lib/site-packages/proto/marshal/collections/repeated.py:125), in RepeatedComposite.__getitem__(self, key)
[124](file:///C:/Users/CS01/miniconda3/lib/site-packages/proto/marshal/collections/repeated.py:124) def __getitem__(self, key):
--> [125](file:///C:/Users/CS01/miniconda3/lib/site-packages/proto/marshal/collections/repeated.py:125) return self._marshal.to_python(self._pb_type, self.pb[key])
IndexError: list index out of range
May I know how I can resolve this issue? Is this issue pertaining to the code or crewai or gemini pro or langchain? Appreciate any help I can get. Thank you!
Hi @Tttjjjlll, I guess the issue is with the creation of crew, for hierarchical process based crew use this syntax:
crew = Crew(
agents=[agents],
tasks=[tasks],
manager_llm = llm, #requires a llm to be assigned to the manager agent
verbose=2, # Crew verbose more will let you know what tasks are being worked on, you can set it to 1 or 2 to different logging levels
process=Process.hierarchical
)
also while using hierarchical process, you don't have to assign agent to tasks. for example:
task_manager = Task(description= dedent(f'''write a blog on Artificial General Intelligence'''))
also you can refer to this simple rag example if it helps:
Chroma db setup
import google.generativeai as genai
from chromadb import Documents, EmbeddingFunction, Embeddings
import chromadb
import pandas as pd
DOCUMENT1 = "Operating the Climate Control System Your Googlecar has a climate control system that allows you to adjust the temperature and airflow in the car. To operate the climate control system, use the buttons and knobs located on the center console. Temperature: The temperature knob controls the temperature inside the car. Turn the knob clockwise to increase the temperature or counterclockwise to decrease the temperature. Airflow: The airflow knob controls the amount of airflow inside the car. Turn the knob clockwise to increase the airflow or counterclockwise to decrease the airflow. Fan speed: The fan speed knob controls the speed of the fan. Turn the knob clockwise to increase the fan speed or counterclockwise to decrease the fan speed. Mode: The mode button allows you to select the desired mode. The available modes are: Auto: The car will automatically adjust the temperature and airflow to maintain a comfortable level. Cool: The car will blow cool air into the car. Heat: The car will blow warm air into the car. Defrost: The car will blow warm air onto the windshield to defrost it."
DOCUMENT2 = "Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the \"Navigation\" icon to get directions to your destination or touch the \"Music\" icon to play your favorite songs."
DOCUMENT3 = "Shifting Gears Your Googlecar has an automatic transmission. To shift gears, simply move the shift lever to the desired position. Park: This position is used when you are parked. The wheels are locked and the car cannot move. Reverse: This position is used to back up. Neutral: This position is used when you are stopped at a light or in traffic. The car is not in gear and will not move unless you press the gas pedal. Drive: This position is used to drive forward. Low: This position is used for driving in snow or other slippery conditions."
documents = [DOCUMENT1, DOCUMENT2, DOCUMENT3]
class GeminiEmbeddingFunction(EmbeddingFunction):
def __call__(self, input: Documents) -> Embeddings:
model = 'models/embedding-001'
title = "Custom query"
return genai.embed_content(model=model,
content=input,
task_type="retrieval_document",
title=title)["embedding"]
def create_chroma_db(documents: List[dict], name: str):
chroma_client = chromadb.Client()
db = chroma_client.create_collection(name=name, embedding_function=GeminiEmbeddingFunction())
# Assigning sequential IDs starting from 1
id_counter = 1
for doc in documents:
new_id = str(id_counter)
db.add(
documents=doc,
ids=new_id
)
id_counter += 1
return db
db = create_chroma_db(documents, "GoogleChromadbExample")
def get_relevant_passage(query):
passage = db.query(query_texts=[query], n_results=1)['documents'][0][0]
return passage
passage = get_relevant_passage("touch screen features")
print(passage)
##output
# Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, #entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can # touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.
tool and crew setup
@tool("Search the document")
def search_document(query):
"""Useful to search the document
about a a given topic and return relevant results"""
ques = query
print(ques)
ans = get_relevant_passage(ques)
return str(ans)
rag_agent = Agent(
role='Salesman',
goal=f'To answer the queries usings documents',
backstory="You are an experienced salesman",
verbose=True,
allow_delegation=True,
llm = llm,
tools=[
search_document
],
# Task = researcher_prompt
)
task1 = Task(description= 'Search the documents and answer this question, Question tell me about google car touch screen features?',agent = rag_agent)
task1.execute()
Thought: Do I need to use a tool? Yes
Action: Search the document
Action Input: touchscreen features in google cartouchscreen features in google car
Tool search the document has been used.
Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.
Do I need to use a tool? No
Final Answer: Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.
> Finished chain.
Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.
crew setup
crew = Crew(
agents=[rag_agent],
tasks=[task1],
# manager_llm = llm,
verbose=2, # Crew verbose more will let you know what tasks are being worked on, you can set it to 1 or 2 to different logging levels
process=Process.sequential
)
result = crew.kickoff()
output:
[DEBUG]: Working Agent: Salesman
[INFO]: Starting Task: Search the documents and answer this question, Question tell me about google car touch screen features?
> Entering new CrewAgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Search the document
Action Input: google car touch screen featuresgoogle car touch screen features
Tool search the document has been used.
Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.
Do I need to use a tool? No
Final Answer: The touchscreen display in the Google car provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.
> Finished chain.
[DEBUG]: [Salesman] Task output: The touchscreen display in the Google car provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.
Also Ensure that you hide any sensitive information, such as API keys, before sharing the code.
Get started tutorial from official documents of CrewAI is generating error.
https://docs.crewai.com/how-to/Creating-a-Crew-and-kick-it-off/
Thanks for flagging folks, looking into that today
Get started tutorial from official documents of CrewAI is generating error.
https://docs.crewai.com/how-to/Creating-a-Crew-and-kick-it-off/
Did you fixed this error?
Thank you @Tttjjjlll for submitting this issue along with detailed code sample! We were able to resolve the underlying issue in PR #844 .
If you update to the latest version of crewAI, you won't run into this issue anymore.
Please let me know if you have any questions or need any additional help!
Closed by #844