langchain-ai / langchain

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

TypeError Due to Duplicate 'auth' Argument in aiohttp Request when provide header to APIChain #8842

Closed bugramatik closed 1 year ago

bugramatik commented 1 year ago

System Info

Langchain version: 0.0.253 Python:3.11

Who can help?

@agola11 @hwchase17 @eyurtsev

Information

Related Components

Reproduction

  1. Environment Setup:

Ensure you're using Python 3.11. Install the necessary libraries and dependencies:

pip install fastapi uvicorn aiohttp langchai
  1. APIChain Initialization:

Set up the APIChain utility using the provided API documentation and the chosen language model:

from langchain import APIChain

chain = APIChain.from_llm_and_api_docs(api_docs=openapi.MY_API_DOCS, llm=choosen_llm, verbose=True, headers=headers)
  1. Run the FastAPI application:

Use a tool like Uvicorn to start your FastAPI app:

uvicorn your_app_name:app --reload
  1. Trigger the API Endpoint:

Make a request to the FastAPI endpoint that uses the APIChain utility. This could be through tools like curl, Postman, or directly from a browser, depending on how your API is set up. Execute the Callback:

Inside the relevant endpoint, ensure you have the following snippet:


with get_openai_callback() as cb:
    response = await chain.arun(user_query)
  1. Observe the Error:

You should encounter a TypeError indicating a conflict with the auth argument in the aiohttp.client.ClientSession.request() method. Because of providing header to APIChain and running it with arun method.

Expected behavior

Request Execution:

The chain.arun(user_query) method should interact with the intended external service or API without any issues. The auth parameter, when used in the underlying request to the external service (in aiohttp), should be correctly applied without conflicts or multiple definitions.

bugramatik commented 1 year ago

If langchain/utilities/requests.py line 60 is changed like this:

if self.auth:
    async with session.request(
            method, url, headers=self.headers, auth=self.auth, **kwargs
    ) as response:
        yield response
else:
    async with session.request(
            method, url, headers=self.headers, **kwargs
    ) as response:
        yield response

problem can be solved.

nfcampos commented 1 year ago

Thanks for reporting, this is fixed in #11010