0

I am trying to use langchain Agents in order to get answers for the questions asked using API, but facing error "AttributeError: 'tuple' object has no attribute 'is_single_input'". Following is the code and error. Open for solution and suggestions.

from langchain.tools import StructuredTool
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
import requests

Step 1: Implement your API function or class

def process_document(document):
    # Process the document using your API logic
    url = 'api'

    data = {'file': open(document, 'rb')}

    response = requests.post(url, auth=requests.auth.HTTPBasicAuth('dfg', ''), files=data)
    return response

Step 2: Create a Tool

tool = StructuredTool.from_function(process_document,description="Process the document using the API")

Step 3: Initialize the Language Model

llm = OpenAI(temperature=0, model_name="text-davinci-003", openai_api_key="key")

Step 4: Initialize the Agent

agent = initialize_agent(tool, llm)

Step 5: Use the Agent

document = ""  # Provide the document to be processed
result = agent.process(document)  # Process the document using the agent and the API
question = "What is Registration number and registration date?"  # Provide the question to ask about    the processed result
answer = agent.generate(question)  # Generate an answer to the question using the agent

While implementing this I am facing following error :

AttributeError                            Traceback (most recent call last)
<ipython-input-3-dea540cbc5b0> in <cell line: 28>()
     26 
     27 # Step 4: Initialize the Agent
---> 28 agent = initialize_agent(tool, llm)
     29 
     30 # Step 5: Use the Agent

3 frames
/usr/local/lib/python3.10/dist-packages/langchain/agents/utils.py in validate_tools_single_input(class_name, tools)
      7     """Validate tools for single input."""
      8     for tool in tools:
----> 9         if not tool.is_single_input:
     10             raise ValueError(
     11                 f"{class_name} does not support multi-input tool {tool.name}."

AttributeError: 'tuple' object has no attribute 'is_single_input'
 

2 Answers2

1

The problem is here

agent = initialize_agent(tool, llm)

You have to pass it ass a list. Not a tuple.

Change it to

agent = initialize_agent([tool] , llm)

documentation

desertnaut
  • 57,590
  • 26
  • 140
  • 166
0

I'm not super familiar with langchain but in process_document you should return a string as described here.

so your code should look like

def process_document(document) -> str:
    # Process the document using your API logic
    url = 'api'

    data = {'file': open(document, 'rb')}

    response = requests.post(url, auth=requests.auth.HTTPBasicAuth('dfg', ''), files=data)
    return response.json()['YOUR_FIELD']
Fucio
  • 475
  • 3
  • 11