langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.8k stars 15.12k forks source link

list index out of range for agents #25964

Open KaifAhmad1 opened 1 month ago

KaifAhmad1 commented 1 month ago

Checked other resources

Example Code

# agents/base_agent.py

from abc import ABC, abstractmethod
from langchain.agents import AgentExecutor, create_react_agent
from langchain_groq import ChatGroq
from langchain.prompts import ChatPromptTemplate
from config.settings import GROQ_API_KEY, LLM_MODEL, LLM_TEMPERATURE
from langchain.tools import Tool
from typing import List
import logging
import traceback

class BaseAgent(ABC):
    def __init__(self, system_message: str, tools: List[dict]):
        self.llm = ChatGroq(temperature=LLM_TEMPERATURE, model=LLM_MODEL, api_key=GROQ_API_KEY)
        self.tools = tools
        self.prompt = ChatPromptTemplate.from_messages([
            ("system", system_message),
            ("human", "{input}"),
            ("human", "Use these tools to help you: {tool_names}"),
            ("assistant", "{tools}"),
            ("assistant", "{agent_scratchpad}")
        ])
        self.prompt = self.prompt.partial(tool_names=", ".join([tool["name"] for tool in tools]))
        self.agent = create_react_agent(self.llm, [Tool(name=tool["name"], description=tool["description"], func=tool["func"]) for tool in tools], self.prompt)
        self.agent_executor = AgentExecutor.from_agent_and_tools(agent=self.agent, tools=[Tool(name=tool["name"], description=tool["description"], func=tool["func"]) for tool in tools], verbose=True)

    def run(self, query: str) -> str:
        try:
            logging.info(f"{self.__class__.__name__} agent processing query: {query}")
            result = self.agent_executor.invoke(query)
            logging.info(f"{self.__class__.__name__} agent result: {result}")
            return result
        except Exception as e:
            logging.error(f"Error in {self.__class__.__name__} agent: {str(e)}")
            logging.error(f"Traceback: {traceback.format_exc()}")
            return f"Error in agent {self.__class__.__name__}: {str(e)}"

Error Message and Stack Trace (if applicable)

INFO:root:Researcher agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Researcher agent: list index out of range
INFO:root:Analyst agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Analyst agent: list index out of range
INFO:root:Advisor agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Advisor agent: list index out of range
INFO:root:IncidentResponder agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in IncidentResponder agent: list index out of range
INFO:root:Researcher agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Researcher agent: list index out of range
INFO:root:Analyst agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Analyst agent: list index out of range
INFO:root:Advisor agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Advisor agent: list index out of range
INFO:root:IncidentResponder agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in IncidentResponder agent: list index out of range
INFO:root:Researcher agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Researcher agent: list index out of range
INFO:root:Analyst agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Analyst agent: list index out of range
INFO:root:Advisor agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in Advisor agent: list index out of range
INFO:root:IncidentResponder agent processing query: List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global
ERROR:root:Error in IncidentResponder agent: list index out of range
INFO:root:Researcher agent processing query: List all details on BFSI security incidents in India. | Type: Threat Intel | Time: Past Month | Region: Asia
ERROR:root:Error in Researcher agent: list index out of range
INFO:root:Analyst agent processing query: List all details on BFSI security incidents in India. | Type: Threat Intel | Time: Past Month | Region: Asia
ERROR:root:Error in Analyst agent: list index out of range
INFO:root:Advisor agent processing query: List all details on BFSI security incidents in India. | Type: Threat Intel | Time: Past Month | Region: Asia
ERROR:root:Error in Advisor agent: list index out of range
INFO:root:IncidentResponder agent processing query: List all details on BFSI security incidents in India. | Type: Threat Intel | Time: Past Month | Region: Asia
ERROR:root:Error in IncidentResponder agent: list index out of range

Description

Getting List out of range error when passing query to research engine. It is working fine on google colab but when I am running streamlit version of this research engine I am getting list index out of range error.

System Info

pip ==24.2 python == 3.10.10 OS == Windows 11 x64

shamssab commented 1 week ago

Hello! We're a group of students from the University of Toronto Scarborough, and we're excited to contribute to LangChain. We'd love the opportunity to investigate this bug further.

e-than-c commented 16 hours ago

Hi @KaifAhmad1, we are a group of students that took a look at this issue, and it appears that the list index out of range issue could be from the format of the input being passed to the agent. The agent is currently designed to accept a dictionary format for the query, specifically accepting an input like{"input": "Your query here"} . If you are currently passing a plain string (e.g query = "List all details on BFSI security incidents in India. | Type: General | Time: All Time | Region: Global"), try changing it to the dictionary format to see if that resolves the issue. For example, query = {"input": "List all details on BFSI security incidents in India."} Please let us know if this was helpful!